public List<Book> GetBooks() { Book book = new Book(); try { string[] read = File.ReadAllLines(fileName); List<Book> bookinfo = new List<Book>(); foreach (string x in read) { string[] list = x.Split(','); // System.Diagnostics.Debug.WriteLine(list[0]); bookinfo.Add(new Book() { ID = list[0].Replace("[comma]", ","), name = list[1].Replace("[comma]", ","), author = list[2].Replace("[comma]", ","), year = Convert.ToInt32(list[3]), price = Convert.ToSingle(list[4].Substring(1)), stock = Convert.ToInt32(list[5]) }); } return bookinfo; } catch (Exception) { return null; } }
public Boolean AddBook(string ID, string Name, string Author, int year, float price, int stock) { string lines; Book newbook = new Book(); ArrayList listOfStrings = new ArrayList(); try { //Check DUPLICATED ID -->Check in Add CK_Duplicate = true; NewID = ID; List<Book> linesList = GetAllBooks(); //Other INPUT validation if (CanAdd == true && (Name.Trim()).Length > 0 && (Author.Trim()).Length > 0 && year > 0 && price > 0 && stock > 0) { NewID = "";//Empty for reusing //Format string m = ","; newbook.ID = ID.Replace(",", "[comma]"); newbook.name = Name.Replace(",", "[comma]"); newbook.author = Author.Replace(",", "[comma]"); newbook.year = year; newbook.price = price; newbook.stock = stock; if (CanAdd == true) { //ADD NEW --> LIST linesList.Add(newbook); foreach (Book fb in linesList) { lines = fb.ID + m + fb.name + m + fb.author + m + fb.year + m + "$" + fb.price + m + fb.stock; Debug.WriteLine(lines); listOfStrings.Add(lines);//Format same as TEXT file } string[] bList2 = new string[listOfStrings.Count]; bList2 = (String[])listOfStrings.ToArray(typeof(string)); System.IO.File.WriteAllLines(fileName, bList2); return CanAdd; } else { return CanAdd; } } else { return false; } } catch (Exception) { return false; } }