Beispiel #1
0
        public void run()
        {
            int choiceNum = 0;

            do
            {
                // Prints menu choices
                Console.WriteLine("Select the number of your choice:");
                Console.WriteLine("1. Add a book");
                Console.WriteLine("2. Remove a book");
                Console.WriteLine("3. List Books");
                Console.WriteLine("4. Save Books");
                Console.WriteLine("5. Read books from file");
                Console.WriteLine("6. Quit");
                Console.Write("Enter the number of your choice: ");
                string choice = Console.ReadLine();
                BookIO list   = new BookIO();
                BookIO ask    = new BookIO();

                // if the number entered is not a valid choice, asks user to re-enter choice
                // for each valid choice, calls the corresponding action for choice
                try
                {
                    choiceNum = int.Parse(choice);
                    if (choiceNum == 1)
                    {
                        Book book = ask.AskUser();
                        askerBook.Add(book);
                    }
                    else if (choiceNum == 2)
                    {
                        list.ListToScreen(askerBook);
                        Console.Write("Enter the title of the book to delete: ");
                        string delete = Console.ReadLine();
                        var    item   = askerBook.First(x => x.Title == delete); // finds matching title object from list and removes it
                        askerBook.Remove(item);
                    }
                    else if (choiceNum == 3)
                    {
                        list.ListToScreen(askerBook);
                    }
                    else if (choiceNum == 4)
                    {
                        Console.WriteLine("Enter the full path of the file, including .bin or .xml extension: ");
                        string fname = Console.ReadLine();

                        string type = fname.Substring(fname.Length - 4);
                        if (type == ".bin")
                        {
                            ask.WriteToBinary(askerBook, fname);
                        }
                        else if (type == ".xml")
                        {
                            ask.WriteXML(askerBook, fname);
                        }
                        else
                        {
                            Console.WriteLine("Invalid Type");
                        }
                    }
                    else if (choiceNum == 5)
                    {
                        Console.WriteLine("Enter the full path of the file, including .bin or .xml extension: ");
                        string fname = Console.ReadLine();
                        // @"C:\Users\guest no log in\Dropbox\CS 245\Collection\data.bin"
                        string type = fname.Substring(fname.Length - 4);
                        if (type == ".bin")
                        {
                            ask.ReadFromBinary(askerBook, fname);
                        }
                        else if (type == ".xml")
                        {
                            ask.ReadXML(askerBook, fname);
                        }
                        else
                        {
                            Console.WriteLine("Invalid Type");
                        }
                    }
                    else
                    {
                        Console.Write("Enter the number of your choice: ");
                        choice = Console.ReadLine();
                    }
                }
                catch (FormatException)
                {
                    Console.Write("Enter the number of your choice: ");
                    choice = Console.ReadLine();
                }
            } while (choiceNum != 6);
        }
Beispiel #2
0
 public Manager()
 {
     io        = new BookIO();
     askerBook = new List <Book>();
 }