Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Web;
- namespace LB4_V2
- {
- public class InOut
- {
- /// <summary>
- /// Reads the starting data
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public static List<LibraryRegister> ReadData(string fileName)
- {
- if (Directory.GetFiles(fileName, "*.txt").Count() == 0)
- {
- throw new Exception(string.Format("Nėra .txt duomenų failų nurodytame aplankale"));
- }
- List<LibraryRegister> registerList = new List<LibraryRegister>();
- foreach (string txtName in Directory.GetFiles(fileName, "*.txt"))
- {
- LibraryRegister register = new LibraryRegister();
- string[] lines = File.ReadAllLines(txtName);
- string lName = lines[0];
- string address = lines[1];
- string phoneNumber = lines[2];
- register.name = lName;
- register.address = address;
- register.phoneNumber = phoneNumber;
- for (int i = 3; i < lines.Length; i++)
- {
- var values = Regex.Split(lines[i], "; ");
- string name = values[0];
- string type = values[1];
- string publishingPlace = values[2];
- int yearOfRelease = int.Parse(values[3]);
- int numberOfPages = int.Parse(values[4]);
- int edition = int.Parse(values[5]);
- if (type.Equals("Book"))
- {
- string ISBN = values[6];
- string author = values[7];
- Book book = new Book(name, type, publishingPlace, yearOfRelease, numberOfPages, edition, ISBN, author);
- register.Add(book);
- }
- else
- {
- if (type.Equals("Journal"))
- {
- string ISBN = values[6];
- int number = int.Parse(values[7]);
- int monthOfRelease = int.Parse(values[8]);
- Journal journal = new Journal(name, type, publishingPlace, yearOfRelease, numberOfPages, edition,
- ISBN, number, monthOfRelease);
- register.Add(journal);
- }
- else
- {
- DateTime date = DateTime.Parse(values[6]);
- int number = int.Parse(values[7]);
- string monthAndDayOfRelease = values[8];
- Newspaper newspaper = new Newspaper(name, type, publishingPlace, yearOfRelease, numberOfPages,
- edition, date, number, monthAndDayOfRelease);
- register.Add(newspaper);
- }
- }
- }
- registerList.Add(register);
- }
- return registerList;
- }
- /// <summary>
- /// Prints data to a CSV file.
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="header"></param>
- /// <param name="publications"></param>
- public static void PrintToCSV(string fileName, string header, List<Publication> publications)
- {
- StreamWriter sw = new StreamWriter(fileName, true);
- sw.WriteLine(header);
- for (int i = 0; i < publications.Count(); i++)
- {
- sw.WriteLine(publications[i].ToString());
- }
- sw.Close();
- }
- /// <summary>
- /// Prints data to a CSV file
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="header"></param>
- /// <param name="publications"></param>
- public static void PrintToCSV(string fileName, string header, LibraryRegister publications)
- {
- StreamWriter sw = new StreamWriter(fileName, true);
- sw.WriteLine(header);
- for (int i = 0; i < publications.Count(); i++)
- {
- Publication p = publications.Get(i);
- sw.WriteLine(p.ToString());
- }
- sw.Close();
- }
- /// <summary>
- /// Prints starting data to a file
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="header"></param>
- /// <param name="registerList"></param>
- public static void PrintStartData(string fileName, string header, List<LibraryRegister> registerList)
- {
- StreamWriter sw = new StreamWriter(fileName, true);
- sw.WriteLine(header);
- sw.WriteLine();
- for (int i = 0; i < registerList.Count(); i++)
- {
- sw.WriteLine(registerList[i].name);
- sw.WriteLine(registerList[i].address);
- sw.WriteLine(registerList[i].phoneNumber);
- for (int j = 0; j < registerList[i].Count(); j++)
- {
- sw.WriteLine(registerList[i].Get(j).ToString());
- }
- sw.WriteLine();
- }
- sw.Close();
- }
- /// <summary>
- /// Prints data to a file
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="header"></param>
- /// <param name="register"></param>
- public static void PrintData(string fileName, string header, List<LibraryRegister> register)
- {
- StreamWriter sw = new StreamWriter(fileName, true);
- sw.WriteLine(header);
- sw.WriteLine();
- for (int i = 0; i < register.Count(); i++)
- {
- sw.WriteLine(register[i].name);
- for (int j = 0; j < register[i].Count(); j++)
- {
- sw.WriteLine(register[i].Get(j).name + " " + register[i].Get(j).edition);
- }
- }
- sw.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement