static void Main(string[] args) { Console.WriteLine("Please Enter the Name of a File: "); string fileToUse = Console.ReadLine(); CardCatalog currentCardCatalog = new CardCatalog(fileToUse); Console.Clear(); bool stillGoing = true; while (stillGoing == true) { Console.WriteLine("CHOOSE AN OPTION 1 - 3 for {0}\n\n1. List All books\n2. Add A Book\n3. Save and Exit", fileToUse); int userInput = MakeItTheRightInt(Console.ReadKey(true).KeyChar.ToString()); switch (userInput) { case 1: currentCardCatalog.ListBooks(); break; case 2: currentCardCatalog.AddBook(); break; case 3: currentCardCatalog.Save(); stillGoing = false; break; default: break; } } }
static void Main(string[] args) { Console.WriteLine("enter complete file path that you wish to access the books for"); string fileName = Console.ReadLine(); CardCatalog myCatalog = new CardCatalog(fileName); }
static void Main(string[] args) { Console.WriteLine("Welcome to the Library Card Catalog!"); // Prompt user for filename // Re-prompt if user doesn't enter a valid alphanumeric string string FileName; do { Console.WriteLine("Please enter a new or existing file name:"); FileName = Console.ReadLine(); if (!System.Text.RegularExpressions.Regex.IsMatch(FileName, @"^[a-zA-Z0-9]+$")) { Console.Clear(); Console.WriteLine("You must specify a file name to continue.\n" + "File name can only contain letters and numbers.\n"); } }while (!System.Text.RegularExpressions.Regex.IsMatch(FileName, @"^[a-zA-Z0-9]+$")); // if FileName exists, we must De-Serialize it // That is, create a new CardCatalog object // If it doesn't exist, then we proceed as usual // and save to that file once the user selects the Save option CardCatalog c = (File.Exists(FileName)) ? Program.Deserialize(FileName) : new CardCatalog(FileName); RouteUserRequest(c, GetValidUserInput()); }
public static bool MainMenu(CardCatalog cc) { Console.WriteLine("1) List all books."); Console.WriteLine("2) Add a book."); Console.WriteLine("3) Save and Exit."); string result = Console.ReadLine(); if (result == "1") { cc.ListAllBooks(); return(true); } else if (result == "2") { cc.AddABook(); return(true); } else if (result == "3") { cc.Save(); return(false); } else { return(true); } }
static void Main(string[] args) { string fileName = SetFileName(); CardCatalog cc = new CardCatalog(fileName); string result = ""; while (result != "3") { Console.Clear(); Console.WriteLine("Greetings, \n\n"); Console.WriteLine("Please select an option:"); Console.WriteLine("1) List All Books"); Console.WriteLine("2) Add a Book"); Console.WriteLine("3) Save and Exit"); result = Console.ReadLine(); if (result == "1") { cc.ListBooks(); } if (result == "2") { cc.AddBook(""); } if (result == "3") { cc.Save(); } } Console.ReadLine(); }
public static bool MainMenu(List <Book> myBooks, string FileName)//all of the methods now are given access to the list myBooks through the argument of the method { Console.WriteLine("Welcome to the digital library, choose an option:"); Console.WriteLine("1) List Current Books"); Console.WriteLine("2) To Add A Book"); Console.WriteLine("3) To Save and Exit"); Console.WriteLine("4) To Retrieve List"); string a = Console.ReadLine(); int x; int.TryParse(a, out x); if (x == 1) { Console.Clear(); CardCatalog.ListBooks(myBooks); //when invoking the method ListBooks I need to pass myBooks into the argument Console.WriteLine("Press enter to continue."); Console.ReadLine(); Console.Clear(); return(true); } else if (x == 2) { Console.Clear(); CardCatalog.AddBook(myBooks); // Same deal as ListBooks we need to pass myBooks in the argument Console.WriteLine("Press enter to continue."); Console.ReadLine(); Console.Clear(); return(true); } else if (x == 3) { Console.Clear(); CardCatalog.BookSave(myBooks, FileName);//calling the Save method Console.Clear(); return(false); } else if (x == 4) { Console.Clear(); myBooks = CardCatalog.CallList(FileName); Console.WriteLine("Press enter to continue."); Console.ReadLine(); Console.Clear(); return(true); } else { Console.Clear(); Console.WriteLine("Option not available."); Console.WriteLine("Press enter to continue."); // Added this option so if the user puts a random number it won't crash. Console.ReadLine(); Console.Clear(); return(true); } }
static void Main(string[] args) { Console.WriteLine("Enter a file: "); string filename = Console.ReadLine(); CardCatalog cc = new CardCatalog(filename); DisplayMenu(cc); }
public static void DisplayMenu(CardCatalog cc) { bool displayMenu = true; while (displayMenu) { displayMenu = MainMenu(cc); Console.WriteLine(); } }
static void Main(string[] args) { //File.Create(c:\LibraryCatalog\fileName.txt); //Books b1 = new Books("Dictionary", "Oxford", "Education", "0001"); //List<Books> booksList = new List<Books>(); // create list to store books in CardCatalog cc = new CardCatalog(); cc.CardCatalogMethod(); }
static private void UserNameInput() { Console.WriteLine("PLEASE REMEMBER THE NAME OF THE FILE!"); Console.Write("Please input the name of a file: "); string fileName = Console.ReadLine(); CardCatalog cardCatalog = new CardCatalog(fileName); PromptMenuForUser(cardCatalog); }
static void Main(string[] args) { //this is a test from Khristine's mac.. CardCatalog Catalog = new CardCatalog(); char selection = '\0'; Catalog.ManageTheFile(); while ((selection != '3') || (selection != '4')) { // Clears the screen and creates the menu. Prompts for a selection and calls // the appropriate method Console.Clear(); Console.Write("\n\n\t\t\t\tCard Catalog\n\n"); Console.Write("\n\n\t\t\t\t1. List all books "); Console.Write("\n\n\t\t\t\t2. Add a book "); Console.Write("\n\n\t\t\t\t3. Save and Exit"); Console.Write("\n\n\t\t\t\t4. Exit without saving \n"); Console.Write("\n\n\t\t\t\tPease enter a selection: "); selection = Console.ReadKey().KeyChar; if (selection == '1') { // call list all books method Catalog.listBooks(); } else if (selection == '2') { // call add a book method Catalog.addBook(); } else if (selection == '3') { // call Save and exit method Catalog.SaveAndExit(); } else if (selection == '4') { Environment.Exit(0); } else { Console.Write("\n\t\t\t\tInvalid Selection"); Console.ReadLine(); } } Console.ReadLine(); }
//Link to relevant MSDN documenation //https://docs.microsoft.com/en-us/dotnet/api/system.serializableattribute?view=netframework-4.7 public static void Serialize(string path, CardCatalog c) { BinaryFormatter formatter = new BinaryFormatter(); try { FileStream stream = File.Create(path); formatter.Serialize(stream, c); stream.Close(); } catch (Exception e) { Console.WriteLine(e.Message); return; } }
public static CardCatalog Deserialize(string path) { FileStream stream = File.Open(path, FileMode.Open); BinaryFormatter formatter = new BinaryFormatter(); try { CardCatalog c = (CardCatalog)formatter.Deserialize(stream); stream.Close(); return(c); } catch (Exception e) { Console.WriteLine(e.Message); return(new CardCatalog(path)); } }
static private void PromptMenuForUser(CardCatalog cardCatalog) { int userChoice; while (true) { Console.Clear(); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("1. List All Books\n" + "2. Add A Book\n" + "3. Save And Exit"); Console.Write("Input your choice: "); if (int.TryParse(Console.ReadLine(), out userChoice)) { if (userChoice == 1) { //List all the books //replay the menu cardCatalog.ListBooks(); } else if (userChoice == 2) { //Add a book to the userfile //replay the menu cardCatalog.AddBook(); } else if (userChoice == 3) { //Save all the books added to the userfile //close the menu cardCatalog.Save(); break; } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Please choose from the above!\n" + "Thank you!"); } } } }
/// <summary> /// Reads the user's selection and calls the appropriate CardCatalog action /// </summary> /// <param name="c">current CardCatalog object</param> /// <param name="userSelection">User's menu choice (could be 1, 2, or 3.</param> private static void RouteUserRequest(CardCatalog c, int userSelection) { while (userSelection != 3) { if (userSelection == 1) { c.ListBooks(); } else if (userSelection == 2) { c.AddBook(c.CreateBook()); } userSelection = GetValidUserInput(); } if (userSelection == 3) { c.Save(); } }
static void Main(string[] args) { string catalogName = "thecatalog"; string openingOption; int choice; bool keepGoing = true; string stringChoice; string bookTitle; string bookAuthor; string bookSubject; // Book[] allBooks; CardCatalog MainCatalog = new CardCatalog(); List <Book> BookList = new List <Book>(); Console.WriteLine("Do you want to open a catalog or do you want to create a new one? (type 'new' or 'open')"); openingOption = Console.ReadLine(); if (openingOption == "new") { Console.WriteLine("Type the name of the Book Catalog:"); catalogName = Console.ReadLine(); // Program theProgram = new Program(); // theProgram.happyness = "Singing"; } if (openingOption == "open") { Console.WriteLine("Enter the catalog name correctly: "); catalogName = Console.ReadLine(); DataContractSerializer ds = new DataContractSerializer(typeof(CardCatalog)); FileStream fs = new FileStream(catalogName + ".xml", FileMode.Open); XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas()); MainCatalog = (CardCatalog)ds.ReadObject(reader); reader.Close(); // currentScreen = OpeningMainScreen; } do { Console.WriteLine("1. List All Books"); Console.WriteLine("2. Add A Book"); Console.WriteLine("3. Save and Exit"); Console.WriteLine("Choose One of these by pressing the number and enter:"); stringChoice = Console.ReadLine(); choice = Convert.ToInt32(stringChoice); if (choice == 1) { BookList = MainCatalog.BookListHolder; int i = 1; foreach (Book loopVar in BookList) { Console.WriteLine("Book {0}", i); i++; Console.WriteLine("Book Title: {0}", loopVar.title); Console.WriteLine("Book Subject: {0}", loopVar.subject); Console.WriteLine("Book Author: {0}", loopVar.author); } } else if (choice == 2) { Console.WriteLine("What is the book title?"); bookTitle = Console.ReadLine(); Console.WriteLine("Who is the author of the book?"); bookAuthor = Console.ReadLine(); Console.WriteLine("What is the subject of the book?"); bookSubject = Console.ReadLine(); BookList.Add(new Book() { title = bookTitle, author = bookAuthor, subject = bookSubject }); } else if (choice == 3) { MainCatalog.BookListHolder = BookList; MainCatalog.filename = catalogName; var writer = new FileStream(catalogName + ".xml", FileMode.Create); DataContractSerializer ser = new DataContractSerializer(typeof(CardCatalog)); ser.WriteObject(writer, MainCatalog); writer.Close(); keepGoing = false; } } while (keepGoing == true); //Program theProgram = new Program(); //theProgram.happyness = "Singing"; //var writer = new FileStream(theProgram.happyness + ".xml", FileMode.Create); //DataContractSerializer ser = new DataContractSerializer(typeof(Program)); //ser.WriteObject(writer, theProgram); //writer.Close(); }
static void Main(string[] args) { Console.WriteLine("Enter the name of the file: "); string fileName = Console.ReadLine(); CardCatalog newCatalog = new CardCatalog(fileName); Console.Clear(); int option = 0; do { Console.Clear(); Console.WriteLine("1. List All books"); Console.WriteLine("2. Add a book "); Console.WriteLine("3. Save and Exit"); string inputChoice = Console.ReadLine(); int.TryParse(inputChoice, out option); switch (option) { case 1: Console.WriteLine("List of books in Card Catalog: "); if (File.Exists(fileName)) { XDocument doc = XDocument.Load(fileName); var books = doc.Descendants("Title"); foreach (var book in books) { Console.WriteLine(book.Value); } } else { Console.WriteLine("File name does not exist. Select option number 2 to add books or 3 to exit. "); } Console.ReadLine(); break; case 2: Console.WriteLine("Enter the book author name"); string author = Console.ReadLine(); Console.WriteLine("Enter the book title name"); string title = Console.ReadLine(); Console.WriteLine("Enter the book year"); string year = Console.ReadLine(); newCatalog.books.Add(new Book { Author = author, Title = title, Year = year }); break; case 3: newCatalog.Save(fileName); break; } } while (option != 3); }