Example #1
0
        static void Main(string[] args)
        {
            context.Database.EnsureCreated();
            bool entering = true;

            while (entering)
            {
                Console.Clear();
                Console.WriteLine($"Please enter an option between 1-5:\n1: To Add a Book\n2: To Update a Book\n3: To Remove a Book\n4: To see Book inventory List:\n5: To see a single Book:");

                try
                {
                    int userInput = Convert.ToInt32(Console.ReadLine());

                    if (userInput < 1 || userInput > 5)
                    {
                        Console.WriteLine($"Invalid selection, please choose between 1-5");
                    }
                    else
                    {
                        switch (userInput)
                        {
                        case 1:
                            if (entering)
                            {
                                Console.Clear();
                                context.Print();
                                Console.WriteLine("Enter the Title:");
                                string title = Console.ReadLine();
                                Console.WriteLine("Enter the Author:");
                                string author = Console.ReadLine();
                                Books  book   = new Books(title, author);
                                context.Add(book);

                                context.SaveChanges();
                                Console.Clear();
                            }
                            break;


                        case 2:
                            Console.Clear();
                            Console.WriteLine("Please enter the ID of the Book you would like to update:");
                            int id = Convert.ToInt32(Console.ReadLine());
                            Console.WriteLine("Enter the new Title of the book you wish to update");
                            string updateTitle = Console.ReadLine();
                            Console.WriteLine("Enter the new Author of the book you wish to update");
                            string updateAuthor = Console.ReadLine();
                            var    updatingBook = GetBook(id);
                            updatingBook.Title  = updateTitle;
                            updatingBook.Author = updateAuthor;
                            context.Update(updatingBook);

                            context.SaveChanges();
                            Console.Clear();

                            break;

                        case 3:
                            Console.Clear();
                            Console.WriteLine("Please enter the ID of the Book you wish to Remove:");
                            int removeID     = Convert.ToInt32(Console.ReadLine());
                            var removingBook = GetBook(removeID);
                            context.Remove(removingBook);

                            context.SaveChanges();

                            Console.Clear();

                            break;

                        case 4:
                            Console.Clear();

                            Console.WriteLine("Here's a list of books in the inventory");

                            context.Print();

                            break;

                        case 5:
                            Console.Clear();
                            Console.WriteLine("Please enter the ID of the Book you wish to read:");
                            int singleBookId = Convert.ToInt32(Console.ReadLine());

                            context.Print(GetBook(singleBookId));

                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"You entered invalid data: {ex.Message}");
                    Console.WriteLine($"{ex.StackTrace}");
                }

                Console.WriteLine("Would you like to Add, Update or Remove any more Books? Y/N:");

                string endLoop = Console.ReadLine().ToUpper();
                if (endLoop == "Y")
                {
                    continue;
                }
                else if (endLoop == "N")
                {
                    entering = false;
                }
            }

            Console.ReadKey();
        }
Example #2
0
        static void Main(string[] args)
        {
            //variable to determine when the user is done entering books
            bool done = false;

            //instantiate an instance of the context
            BooksContext library = new BooksContext();

            //make sure that the table exists, and create it if it does not already exist.
            library.Database.EnsureCreated();

            while (!done)
            {
                //ask the user for a book to add
                Console.WriteLine("Do you want to 'review' the books, 'add' a book, 'update' a book, or 'delete' a book? Type 'done' when finished.");
                string action = Console.ReadLine();

                if (action.ToLower() != "done")
                {
                    if (action.ToLower() == "review" || action.ToLower() == "update" || action.ToLower() == "delete")
                    {
                        //display a listing of the books in the library
                        BooksContext.ReviewBooks(library.Books);

                        //what to do next if the user wants to update a book
                        if (action.ToLower() == "update")
                        {
                            //ask which book the user wishes to update
                            Console.WriteLine("Enter the ID of the book to update.");
                            string bookID      = Console.ReadLine();
                            Book   UpdatedBook = library.Books.Where(x => x.ID == int.Parse(bookID)).FirstOrDefault();
                            library.Update(BooksContext.GetBook(action, UpdatedBook));
                            library.SaveChanges();
                            BooksContext.ReviewBooks(library.Books);
                        }
                        //what to do next if the user wants to delete a book
                        else if (action.ToLower() == "delete")
                        {
                            string verify = "NO";
                            string bookID = "";
                            while (verify == "NO")
                            {
                                //ask which book the user wishes to delete
                                Console.WriteLine("Enter the ID of the book to delete or type 'CANCEL'.");
                                bookID = Console.ReadLine();
                                if (bookID == "CANCEL")
                                {
                                    break;
                                }
                                Console.WriteLine("You have chosen to delete book " + bookID + ".");
                                Console.WriteLine("Is the correct? YES or NO?");
                                verify = Console.ReadLine();
                            }
                            if (bookID != "CANCEL")
                            {
                                Book DeleteBook = library.Books.Where(x => x.ID == int.Parse(bookID)).FirstOrDefault();
                                library.Remove(DeleteBook);
                                library.SaveChanges();
                                BooksContext.ReviewBooks(library.Books);
                            }
                        }
                    }
                    else if (action.ToLower() == "add")
                    {
                        //call method to get the book object to add.
                        //add the newly created book instance to the context.
                        //notice how similar this is to adding an item to a list.
                        library.Add(BooksContext.GetBook(action, null));

                        //ask the context to save any changes to the database
                        library.SaveChanges();

                        //use a foreach loop to loop through the students in the context
                        //notice how similar this is to looping through a list
                        BooksContext.ReviewBooks(library.Books);
                    }
                }
                else
                {
                    done = true;
                }
            }
        }