//Add a record to the file, if the ISBN already exists, don't add it. private void btnAddNew_Click(object sender, EventArgs e) { UpdateDate(); string[] info = getInfo(); if (validateInfo()) { DialogResult dr = MessageBox.Show("Are you sure you'd like to add the book displayed to the inventory?", "Add Book", MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { Book addBook = new Book(info); if (!fh.bookSearch(ref addBook, info[0])) { if (fh.addBook(addBook.ToString())) { lastAction = 'a'; MessageBox.Show("The book has been succesfully added.", "Success!"); } else { MessageBox.Show("Error adding book", "File Error"); return; } } else { MessageBox.Show("Book with ISBN: " + info[0] + " already exists. Recheck your info.", "Duplicate Record"); return; } } else { MessageBox.Show("Action canceled.", "Canceled"); return; } } }
//Handles searching the book file for the specified book, as well as deleting/updating files. //Flags are "delete"/"update". Add is handled with a boolean and then calling the addBook method. //Doing it this way prevents having to re-search/scan the file for deleting/updating. //While you could do delete/update in similar ways it revolves around constantly keeping track //of the Reader/Writer which I feel is more difficult to follow than this method. //However, this method does kind of do a lot and is a pain to read through. public bool bookSearch(ref Book book, string isbn, string flag = "") { sw = new StreamWriter(tempFile); sr = new StreamReader(sourceFile); currentBook = book; //Store original book passed string line; bool found = false; flag.ToLower(); while ((line = sr.ReadLine()) != null) { string[] bookInfo = line.Split('|'); //ISBN ID Validatio(Two groups of 3 digits delimited by hyphen) if (Book.ValidateISBNFormat(bookInfo[0])) { if (bookInfo[0] == isbn && flag == "") //Just a search, return the book that was being looked for/dont modify file { book = new Book(bookInfo); found = true; sw.Close(); sr.Close(); return(found); } else if (bookInfo[0] == isbn && flag == "delete") //Record found, delete it? If yes, just skip writing. { Book deleteBook = new Book(bookInfo); DialogResult dr = MessageBox.Show("Book with ISBN: " + isbn + " found.\r\n" + String.Join(Environment.NewLine, deleteBook.BookInfo()) + "\r\nIs this the book you wish to delete?", "Delete Book", MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { found = true; } } else if (bookInfo[0] == isbn && flag == "update") //Update a record { sw.WriteLine(currentBook.ToString()); found = true; } else //This book doesn't match the book being searched for, safe to just write it. { if (!string.IsNullOrWhiteSpace(line)) { sw.WriteLine(line); } } } else //What to do if we have invalid entry in txt file/data corruption { if (!string.IsNullOrEmpty(line)) { MessageBox.Show("Inventory file has issue at ISBN: " + bookInfo[0], "Data Corruption");//Move to form code probably return(false); } } } sr.Close(); sw.Close(); return(found); }
public void ViewBooks(List <Book> books) { int sno = 1; Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.DarkBlue; Console.WriteLine(" Id\tPrice\t\tName\t\t\t\t\t "); Console.WriteLine("-----------------------------------------------------------------"); Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.White; foreach (Book bookItem in books) { if (Convert.ToInt32(bookItem.GetPrice()) == 0) { Console.WriteLine(" " + sno + "\t" + "Free" + "\t\t" + bookItem.GetName()); } else { Console.WriteLine(" " + sno + "\t" + bookItem.GetPrice() + "\t\t" + bookItem.GetName()); } Console.WriteLine("-----------------------------------------------------------------"); sno++; } while (true) { Console.Write("Enter Id to View Book or "); Console.BackgroundColor = ConsoleColor.DarkBlue; Console.WriteLine("'Press 0 to go back on main menu'"); Console.ResetColor(); try { int id = Convert.ToInt32(Console.ReadLine()); if (id == 0) { Console.Clear(); ShowMenu(); SelectOption(); break; } else { Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.DarkBlue; Console.WriteLine("\t\t\tBook Information\t\t\t "); Console.WriteLine("-----------------------------------------------------------------"); Console.ResetColor(); int bookId = --id; Book _book = books[bookId]; Console.WriteLine(_book.ToString()); Console.BackgroundColor = ConsoleColor.DarkYellow; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("\n Press 1 to add book to cart or 2 to view author(s) details"); Console.ResetColor(); int opt = Convert.ToInt32(Console.ReadLine()); if (opt == 1) { try { Console.Write("Enter Quantity : "); int qty = Convert.ToInt32(Console.ReadLine()); if (qty <= 0) { throw new Exception(); } AddItem(_book, qty); Console.WriteLine("\nYou have (" + user.GetShoppingCartBooks().Length + ") Items in your cart.\n"); } catch (Exception) { Console.WriteLine("Invalid qty"); } } else { ViewAuthors(_book.GetAuthors()); } } } catch (Exception) { Console.WriteLine("Invalid Input...Try again!!"); } } }