public static List <Book> getData() { List <Book> books = new List <Book>(); var lines = File.ReadAllLines("BookData.txt"); foreach (var line in lines) { var bookArray = line.Split(':'); if (bookArray[3].Trim().Equals("HardCopy", StringComparison.OrdinalIgnoreCase)) { Hardcopy hardBook = new Hardcopy(); hardBook.setName(bookArray[0].Trim()); hardBook.setAuthor(bookArray[1].Trim()); hardBook.setGenre(bookArray[2].Trim()); hardBook.setType(bookArray[3].Trim()); hardBook.setYear(bookArray[4].Trim()); books.Add(hardBook); } else if (bookArray[3].Trim().Equals("SoftCopy", StringComparison.OrdinalIgnoreCase)) { Softcopy softBook = new Softcopy(); softBook.setName(bookArray[0].Trim()); softBook.setAuthor(bookArray[1].Trim()); softBook.setGenre(bookArray[2].Trim()); softBook.setType(bookArray[3].Trim()); softBook.setVersion(bookArray[4].Trim()); books.Add(softBook); } } books.Sort(); return(books); }
public static void addBook() { var externalFile = new ExternalFile(); Console.WriteLine("Enter Book Type - hardcopy or softcopy"); string givenType = Console.ReadLine(); if (string.Equals(givenType, "hardcopy", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("Enter Book Name"); string givenName = Console.ReadLine(); Console.WriteLine("Enter Author"); string givenAuthor = Console.ReadLine(); Console.WriteLine("Enter Genre"); string givenGenre = Console.ReadLine(); Console.WriteLine("Enter Year"); var givenYear = Console.ReadLine(); Hardcopy book = new Hardcopy(givenName, givenAuthor, givenGenre, givenYear, givenType); externalFile.writeData(book); Console.WriteLine("Book has been Added"); Console.WriteLine("*******************"); } else if (string.Equals(givenType, "softcopy", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("Enter Book Name"); string givenName = Console.ReadLine(); Console.WriteLine("Enter Author"); string givenAuthor = Console.ReadLine(); Console.WriteLine("Enter Genre"); string givenGenre = Console.ReadLine(); Console.WriteLine("Enter Version"); var givenVersion = Console.ReadLine(); Softcopy book = new Softcopy(givenName, givenAuthor, givenGenre, givenVersion, givenType); externalFile.writeData(book); Console.WriteLine("Book has been Added"); Console.WriteLine("*******************"); } else { Console.WriteLine(givenType + " is not a valid type."); } }
public void writeData(Book book) { if (book.getType().Equals("HardCopy")) { Hardcopy b = (Hardcopy)book; string bookData = b.getName() + ":" + b.getAuthor() + ":" + b.getGenre() + ":" + b.getType() + ":" + b.getYear(); File.AppendAllText("BookData.txt", bookData + Environment.NewLine); } else { Softcopy b = (Softcopy)book; string bookData = b.getName() + ":" + b.getAuthor() + ":" + b.getGenre() + ":" + b.getType() + ":" + b.getVersion(); File.AppendAllText("BookData.txt", bookData + Environment.NewLine); } }
public void writeAllBooks(List <Book> books) { File.WriteAllText("BookData.txt", String.Empty); foreach (var book in books) { if (book.getType().Equals("HardCopy", StringComparison.OrdinalIgnoreCase)) { Hardcopy b = (Hardcopy)book; string bookData = b.getName() + ":" + b.getAuthor() + ":" + b.getGenre() + ":" + b.getType() + ":" + b.getYear(); File.AppendAllText("BookData.txt", bookData + Environment.NewLine); } else { Softcopy b = (Softcopy)book; string bookData = b.getName() + ":" + b.getAuthor() + ":" + b.getGenre() + ":" + b.getType() + ":" + b.getVersion(); File.AppendAllText("BookData.txt", bookData + Environment.NewLine); } } }