public static void CheckList() { Console.Clear(); Console.WriteLine("You are working in the \"{0}.xml\" file\n\n", fileName); Console.WriteLine("1) List all books."); Console.WriteLine("2) Add a book."); Console.WriteLine("3) Save and exit."); int userValue = Vaildation.IsInt(Console.ReadLine(), "Number please"); switch (userValue) { case (int)(UserOptions.listOfBooks): CardCatalog.ListBooks(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//" + fileName + ".xml", CardCatalog.myBooks); break; case (int)(UserOptions.addBook): CardCatalog.AddBook(CardCatalog.myBooks); break; case (int)(UserOptions.saveAndExit): CardCatalog.SaveAndExit(); break; default: Console.Clear(); Console.WriteLine("Invalid number. Please enter 1, 2, or 3."); CheckList(); break; } }
static void Main(string[] args) { Console.WriteLine("Enter File Name: "); string fileName = Console.ReadLine(); CardCatalog cc = new CardCatalog(fileName); string selection = "0"; while (selection != "3") { Console.Clear(); Console.WriteLine("\n****************************"); Console.WriteLine("Welcome to the our Library!"); Console.WriteLine("****************************\n"); Console.WriteLine("What would you like to do?"); Console.WriteLine("\t1 - List all books"); Console.WriteLine("\t2 - Add a book"); Console.WriteLine("\t3 - Save and Exit"); selection = Console.ReadLine(); switch (selection) { case "1": cc.ListBooks(); break; case "2": Console.Clear(); Console.WriteLine("Enter Title of New Book"); string title = Console.ReadLine(); Console.WriteLine("Enter Author of '{0}'", title); string author = Console.ReadLine(); Console.WriteLine("Enter Genre of '{0}'", title); string genre = Console.ReadLine(); cc.AddBook(title, author, genre); Console.Clear(); Console.WriteLine("Book Added. Press any key to continue..."); break; case "3": cc.SaveAndExit(); break; default: Console.WriteLine("Invalid Input. Restarting Program."); Console.WriteLine("**********************************\n"); break; } } }
public static void Main(string[] args) { int userChoice, year, pages; string title, author, genre, fileName; // Stores the current path string into the path variable string path = Directory.GetCurrentDirectory() + "\\"; //Ask user for a file name Console.Write("Please enter the filename: "); // Read user input and store into the string variable 'fileName' fileName = Console.ReadLine(); //Create a new instance of the CardCatalog class called 'currentCatalog' CardCatalog currentCatalog = new CardCatalog(fileName); // Creat a new instance of a BinaryFormatter called 'formatter' IFormatter formatter = new BinaryFormatter(); //Pass the path and the file name to the CardCatalog's Load method currentCatalog.Load(path + fileName, formatter); while (true) { //Ask User to input a number 1 - 3 based on which option they choose Console.WriteLine("\nWhich option would you like (choose by number 1 - 3):"); Console.Write("1: List All Books\n2: Add A Book\n3: Remove Book\n4: Save & Exit\nChoice: "); string c = Console.ReadLine(); //Parse the string to an integer and if successful returns the parsed number to the variable userChoice int.TryParse(c, out userChoice); switch (userChoice) { case 0: break; case 1: currentCatalog.ListBooks(); break; case 2: //ADD A BOOK Console.Write("Please enter the title: "); title = Console.ReadLine(); Console.Write("Please enter the author: "); author = Console.ReadLine(); Console.Write("Please enter the genre: "); genre = Console.ReadLine(); Console.Write("Please enter the # of pages: "); int.TryParse(Console.ReadLine(), out pages); Console.Write("Please enter the year published: "); int.TryParse(Console.ReadLine(), out year); //call AddBook from the Card Catalog class and pass a new Book object to store in the list currentCatalog.AddBook(new Book(title, author, genre, pages, year)); break; case 3: // CHOICE 3: REMOVE BOOK string inputTitle; // Loop ends when user exits or when book is removed while (true) { Console.Write("Enter the title of the book to remove (enter 'e' for exit): "); inputTitle = Console.ReadLine(); if (String.IsNullOrWhiteSpace(inputTitle)) { Console.WriteLine("Please Enter a title: "); } else if (inputTitle == "e" || inputTitle == "exit") { break; } else { // Call RemoveBook, a mehtod of the CardCatalog class. currentCatalog.RemoveBook(inputTitle); break; } } break; default: break; } if (userChoice == 4) { // SAVE AND EXIT // The SaveAndExit method is called and then the program is exited currentCatalog.SaveAndExit(path + fileName, formatter); break; } } }