Esempio n. 1
0
        // method to manage menu and functions
        static void MenuDisplay()
        {
            Console.Clear();

            bool   runProgram = true;
            string userInput  = "";

            Console.WriteLine("[Audiobook Rental Menu]");
            Console.WriteLine("(A) -- Add New Book");
            Console.WriteLine("(B) -- Book Editor");
            Console.WriteLine("(C) -- Book Rentals");
            Console.WriteLine("(D) -- Book Returns");
            Console.WriteLine("(E) -- Reports");
            Console.WriteLine("(F) -- Exit");
            Console.WriteLine("Select an option to continue:");

            // while loop to run through until valid selection is chosen, or program is exited
            do
            {
                Console.Write("-- ");
                userInput = Console.ReadLine();
                switch (userInput.ToUpper())
                {
                case "A":
                    Console.Clear();
                    Console.WriteLine("[Audiobook Rental Menu - Add A Book]");
                    // calls myBooks object from BooksFile to populate / update array
                    BooksFile.AddBook();
                    myBooks = booksFile.GetAllBooks();
                    ContinueProgram();
                    return;

                case "B":
                    Console.Clear();
                    Console.WriteLine("[Audiobook Rental Menu - Book Editor]");
                    myBooks = booksFile.GetAllBooks();
                    // calls myBooks object and edit book method within object, as well as a way to update the arrays
                    BooksFile.EditBook();
                    myBooks = booksFile.GetAllBooks();
                    ContinueProgram();
                    return;

                case "C":
                    Console.Clear();
                    Console.WriteLine("[Audiobook Rental Menu - Book Rentals]");
                    myBooks = booksFile.GetAllBooks();
                    // transactionFile object to help populate the transactions file with rental's
                    myTransactions = transactionsFile.GetAllTransactions();
                    transactionsFile.RentBook();
                    ContinueProgram();
                    return;

                case "D":
                    Console.Clear();
                    Console.WriteLine("[Audiobook Rental Menu - Book Returns]");
                    // the correct and proper way to do all of this using object references and defined methods within other object files
                    // this method re-defines myTransactions via the transactionsFile object, and the return book method
                    myTransactions = transactionsFile.ReturnBook(myTransactions, transactionsFile, transactionsReports, transactionsUtility, myBooks, booksUtility, booksReports);
                    transactionsUtility.SetMyTransactions(myTransactions);
                    transactionsReports.SetMyTransactions(myTransactions);
                    ContinueProgram();
                    return;

                case "E":
                    Console.Clear();
                    Console.WriteLine("[Audiobook Rental Menu - Book Reports]");
                    // this calls the reportbook method from the transactions file, and references all the relevant object instances
                    transactionsFile.ReportBook(myTransactions, transactionsFile, transactionsReports, transactionsUtility, myBooks, booksUtility, booksReports);
                    ContinueProgram();
                    return;

                case "F":
                    Console.Clear();
                    // this exits the program and gives a goodbye
                    Console.WriteLine("Thank you, Goodbye!");
                    runProgram = false;
                    return;

                default:
                    // test case to ensure that the user input's a valid selection, and to let them know if they do not
                    Console.WriteLine($"Error: {userInput} is not a valid selection, Try Again");
                    runProgram = true;
                    break;
                }
            } while (runProgram == true);
        }
Esempio n. 2
0
        // the method that edits a book, this method prompts
        // the user for a specific value to edit, and it allows
        // them to enter in a new value. this includes error handling
        // as well as updating and re-reading the same original Books.TXT file.
        public static Books[] EditBook()
        {
            BooksFile booksFile = new BooksFile("books.txt");

            Books[]      myBooks      = booksFile.GetAllBooks();
            BooksReports booksReports = new BooksReports(myBooks);
            BooksUtility booksUtility = new BooksUtility(myBooks);

            string isbnInput       = "";
            string titleInput      = "";
            string authorInput     = "";
            string genreInput      = "";
            string runTimeInput    = "";
            string continueInput   = "";
            string editInput       = "";
            string copiesInput     = "";
            double isbnDoubleInput = 0;
            int    searchIndex     = 0;
            int    intTest         = 0;

            Console.WriteLine();
            booksReports.PrintAllBooks();
            Console.WriteLine();

            Console.WriteLine("Select an ISBN to Edit:");
            Console.WriteLine("'Cancel' to Cancel");
            do
            {
                Console.Write("-- ");
                isbnInput = Console.ReadLine();
                if (isbnInput.ToUpper() == "CANCEL")
                {
                    return(myBooks);
                }
                else if (isbnInput.Length > 10)
                {
                    Console.WriteLine($"Error: {isbnInput} is an Invalid ISBN, Try Again");
                }
                else if (isbnInput.Length < 10)
                {
                    Console.WriteLine($"Error: {isbnInput} is an Invalid ISBN, Try Again");
                }
                else if (isbnInput.Length == 10)
                {
                    isbnDoubleInput = Convert.ToDouble(isbnInput);
                    searchIndex     = booksUtility.SequentialSearch(isbnDoubleInput);
                    if (searchIndex == -1)
                    {
                        Console.WriteLine("Error: Book does not exist");
                    }
                    else
                    {
                        Console.Clear();
                    }
                }
            } while (isbnInput.ToUpper() != "CANCEL" && isbnInput.Length != 10);

            string isbnValue    = myBooks[searchIndex].GetIsbn().ToString();
            string titleValue   = myBooks[searchIndex].GetTitle().ToString();
            string authorValue  = myBooks[searchIndex].GetAuthor().ToString();
            string genreValue   = myBooks[searchIndex].GetGenre().ToString();
            string runTimeValue = myBooks[searchIndex].GetRuntime().ToString();
            string copiesValue  = myBooks[searchIndex].GetCopies().ToString();

            Console.WriteLine("Which value would you like to edit?");
            Console.WriteLine();
            Console.WriteLine($"ISBN : {isbnValue}");
            Console.WriteLine($"Title : {titleValue}");
            Console.WriteLine($"Author : {authorValue}");
            Console.WriteLine($"Genre : {genreValue}");
            Console.WriteLine($"Runtime : {runTimeValue}");
            Console.WriteLine($"Copies : {copiesValue}");
            Console.WriteLine();

            Console.WriteLine("'Cancel' to Cancel");
            Console.WriteLine("Enter either the category or specific value:");

            do
            {
                Console.Write("-- ");
                editInput = Console.ReadLine();

                if (editInput.ToUpper() == "CANCEL")
                {
                    // return myBooks;
                }
                else if (editInput.ToUpper() != "CANCEL" && editInput.ToUpper() != "ISBN" && editInput.ToUpper() != isbnValue.ToUpper() && editInput.ToUpper() != "TITLE" && editInput.ToUpper() != titleValue.ToUpper() && editInput.ToUpper() != "AUTHOR" && editInput.ToUpper() != authorValue.ToUpper() && editInput.ToUpper() != "GENRE" && editInput.ToUpper() != genreValue.ToUpper() && editInput.ToUpper() != "RUNTIME" && editInput.ToUpper() != runTimeValue.ToUpper() && editInput.ToUpper() != "COPIES" && editInput.ToUpper() != copiesValue.ToUpper())
                {
                    Console.WriteLine($"Error: {editInput} is not a valid selection, Try Again");
                }
            } while (editInput.ToUpper() != "CANCEL" && editInput.ToUpper() != "ISBN" && editInput.ToUpper() != isbnValue.ToUpper() && editInput.ToUpper() != "TITLE" && editInput.ToUpper() != titleValue.ToUpper() && editInput.ToUpper() != "AUTHOR" && editInput.ToUpper() != authorValue.ToUpper() && editInput.ToUpper() != "GENRE" && editInput.ToUpper() != genreValue.ToUpper() && editInput.ToUpper() != "RUNTIME" && editInput.ToUpper() != runTimeValue.ToUpper() && editInput.ToUpper() != "COPIES" && editInput.ToUpper() != copiesValue.ToUpper());

            if (editInput.ToUpper() == "ISBN" || editInput.ToUpper() == isbnValue.ToUpper())
            {
                Console.WriteLine($"Current ISBN: {isbnValue}");
                do
                {
                    Console.WriteLine("Enter the new ISBN:");
                    Console.WriteLine("'Cancel' to Cancel");
                    Console.Write("-- ");
                    isbnInput = Console.ReadLine();

                    if (isbnInput.Length == 10)
                    {
                        break;
                    }
                    else if (isbnInput.ToUpper() == "CANCEL")
                    {
                        return(myBooks);
                    }

                    // ISBN Error Handling messages
                    if (isbnInput.Length != 10)
                    {
                        if (!int.TryParse(isbnInput, out intTest))
                        {
                            Console.Clear();
                            Console.WriteLine($"Error: '{isbnInput}' is not a number, Try Again.");
                        }
                        else if (isbnInput.Length < 10)
                        {
                            Console.Clear();
                            Console.WriteLine($"Error: '{isbnInput}' is less than 10 digits, Try Again.");
                        }
                        else if (isbnInput.Length > 10)
                        {
                            Console.Clear();
                            Console.WriteLine($"Error: '{isbnInput}' is more than 10 digits, Try Again.");
                        }
                    }
                } while (isbnInput.Length != 10 || isbnInput.ToUpper() != "CANCEL");
                Console.Clear();
                Console.WriteLine($"Old ISBN: {isbnValue}");
                Console.WriteLine($"New ISBN: {isbnInput}");
                Console.WriteLine();
                Console.WriteLine("'Confirm' to Change");
                Console.WriteLine("'Cancel' to Cancel Change");
                Console.WriteLine("Enter your selection:");

                do
                {
                    Console.Write("-- ");
                    continueInput = Console.ReadLine();

                    switch (continueInput.ToUpper())
                    {
                    case "CONFIRM":
                        int      bookFileLength = File.ReadAllLines("books.txt").Length;
                        string[] tempBooks      = File.ReadAllLines("books.txt");
                        myBooks[searchIndex].SetIsbn(Convert.ToDouble(isbnInput));
                        tempBooks[searchIndex] = ($"{myBooks[searchIndex].GetIsbn()}#{myBooks[searchIndex].GetTitle()}#{myBooks[searchIndex].GetAuthor()}#{myBooks[searchIndex].GetGenre()}#{myBooks[searchIndex].GetRuntime()}#{myBooks[searchIndex].GetCopies()}");
                        File.Create("books.txt").Close();
                        using (StreamWriter sw = File.AppendText("books.txt"))
                        {
                            for (int i = 0; i != bookFileLength; i++)
                            {
                                sw.WriteLine(tempBooks[i]);
                            }
                        }
                        myBooks = booksFile.GetAllBooks();
                        Console.WriteLine();
                        Console.WriteLine("Book has been edited, new value is:");
                        Console.WriteLine(myBooks[searchIndex].ToString());
                        return(myBooks);

                    case "CANCEL":
                        break;

                    default:
                        Console.WriteLine("Error: Invalid Selection, Try Again");
                        break;
                    }
                } while (continueInput.ToUpper() != "CANCEL" && continueInput.ToUpper() != "CONFIRM");
            }
            else if (editInput.ToUpper() == "TITLE" || editInput.ToUpper() == titleValue.ToUpper())
            {
                Console.WriteLine($"Current Title: {titleValue}");

                Console.WriteLine("Enter the new Title:");
                Console.WriteLine("'Cancel' to Cancel");
                Console.Write("-- ");
                titleInput = Console.ReadLine();

                if (titleInput.ToUpper() == "CANCEL")
                {
                    return(myBooks);
                }

                Console.Clear();
                Console.WriteLine($"Old Title: {titleValue}");
                Console.WriteLine($"New Title: {titleInput}");
                Console.WriteLine();
                Console.WriteLine("'Confirm' to Change");
                Console.WriteLine("'Cancel' to Cancel Change");
                Console.WriteLine("Enter your selection:");

                do
                {
                    Console.Write("-- ");
                    continueInput = Console.ReadLine();

                    switch (continueInput.ToUpper())
                    {
                    case "CONFIRM":
                        int      bookFileLength = File.ReadAllLines("books.txt").Length;
                        string[] tempBooks      = File.ReadAllLines("books.txt");
                        myBooks[searchIndex].SetTitle(titleInput);
                        tempBooks[searchIndex] = ($"{myBooks[searchIndex].GetIsbn()}#{myBooks[searchIndex].GetTitle()}#{myBooks[searchIndex].GetAuthor()}#{myBooks[searchIndex].GetGenre()}#{myBooks[searchIndex].GetRuntime()}#{myBooks[searchIndex].GetCopies()}");
                        File.Create("books.txt").Close();
                        using (StreamWriter sw = File.AppendText("books.txt"))
                        {
                            for (int i = 0; i != bookFileLength; i++)
                            {
                                sw.WriteLine(tempBooks[i]);
                            }
                        }
                        myBooks = booksFile.GetAllBooks();
                        Console.WriteLine();
                        Console.WriteLine("Book has been edited, new value is:");
                        Console.WriteLine(myBooks[searchIndex].ToString());
                        return(myBooks);

                    case "CANCEL":
                        break;

                    default:
                        Console.WriteLine("Error: Invalid Selection, Try Again");
                        break;
                    }
                } while (continueInput.ToUpper() != "CANCEL" && continueInput.ToUpper() != "CONFIRM");
            }
            else if (editInput.ToUpper() == "AUTHOR" || editInput.ToUpper() == authorValue.ToUpper())
            {
                Console.WriteLine($"Current Author: {authorValue}");

                Console.WriteLine("Enter the new Author:");
                Console.WriteLine("'Cancel' to Cancel");
                Console.Write("-- ");
                authorInput = Console.ReadLine();

                if (authorInput.ToUpper() == "CANCEL")
                {
                    return(myBooks);
                }

                Console.Clear();
                Console.WriteLine($"Old Author: {authorValue}");
                Console.WriteLine($"New Author: {authorInput}");
                Console.WriteLine();
                Console.WriteLine("'Confirm' to Change");
                Console.WriteLine("'Cancel' to Cancel Change");
                Console.WriteLine("Enter your selection:");

                do
                {
                    Console.Write("-- ");
                    continueInput = Console.ReadLine();

                    switch (continueInput.ToUpper())
                    {
                    case "CONFIRM":
                        int      bookFileLength = File.ReadAllLines("books.txt").Length;
                        string[] tempBooks      = File.ReadAllLines("books.txt");
                        myBooks[searchIndex].SetAuthor(authorInput);
                        tempBooks[searchIndex] = ($"{myBooks[searchIndex].GetIsbn()}#{myBooks[searchIndex].GetTitle()}#{myBooks[searchIndex].GetAuthor()}#{myBooks[searchIndex].GetGenre()}#{myBooks[searchIndex].GetRuntime()}#{myBooks[searchIndex].GetCopies()}");
                        File.Create("books.txt").Close();
                        using (StreamWriter sw = File.AppendText("books.txt"))
                        {
                            for (int i = 0; i != bookFileLength; i++)
                            {
                                sw.WriteLine(tempBooks[i]);
                            }
                        }
                        myBooks = booksFile.GetAllBooks();
                        Console.WriteLine();
                        Console.WriteLine("Book has been edited, new value is:");
                        Console.WriteLine(myBooks[searchIndex].ToString());
                        return(myBooks);

                    case "CANCEL":
                        break;

                    default:
                        Console.WriteLine("Error: Invalid Selection, Try Again");
                        break;
                    }
                } while (continueInput.ToUpper() != "CANCEL" && continueInput.ToUpper() != "CONFIRM");
            }
            else if (editInput.ToUpper() == "GENRE" || editInput.ToUpper() == genreValue.ToUpper())
            {
                Console.WriteLine($"Current Genre: {genreValue}");

                Console.WriteLine("Enter the new Genre:");
                Console.WriteLine("'Cancel' to Cancel");
                Console.Write("-- ");
                genreInput = Console.ReadLine();

                if (genreInput.ToUpper() == "CANCEL")
                {
                    return(myBooks);
                }

                Console.Clear();
                Console.WriteLine($"Old Genre: {genreValue}");
                Console.WriteLine($"New Genre: {genreInput}");
                Console.WriteLine();
                Console.WriteLine("'Confirm' to Change");
                Console.WriteLine("'Cancel' to Cancel Change");
                Console.WriteLine("Enter your selection:");

                do
                {
                    Console.Write("-- ");
                    continueInput = Console.ReadLine();

                    switch (continueInput.ToUpper())
                    {
                    case "CONFIRM":
                        int      bookFileLength = File.ReadAllLines("books.txt").Length;
                        string[] tempBooks      = File.ReadAllLines("books.txt");
                        myBooks[searchIndex].SetGenre(genreInput);
                        tempBooks[searchIndex] = ($"{myBooks[searchIndex].GetIsbn()}#{myBooks[searchIndex].GetTitle()}#{myBooks[searchIndex].GetAuthor()}#{myBooks[searchIndex].GetGenre()}#{myBooks[searchIndex].GetRuntime()}#{myBooks[searchIndex].GetCopies()}");
                        File.Create("books.txt").Close();
                        using (StreamWriter sw = File.AppendText("books.txt"))
                        {
                            for (int i = 0; i != bookFileLength; i++)
                            {
                                sw.WriteLine(tempBooks[i]);
                            }
                        }
                        myBooks = booksFile.GetAllBooks();
                        Console.WriteLine();
                        Console.WriteLine("Book has been edited, new value is:");
                        Console.WriteLine(myBooks[searchIndex].ToString());
                        return(myBooks);

                    case "CANCEL":
                        break;

                    default:
                        Console.WriteLine("Error: Invalid Selection, Try Again");
                        break;
                    }
                } while (continueInput.ToUpper() != "CANCEL" && continueInput.ToUpper() != "CONFIRM");
            }
            else if (editInput.ToUpper() == "RUNTIME" || editInput.ToUpper() == runTimeValue.ToUpper())
            {
                Console.WriteLine($"Current Runtime: {runTimeValue}");

                Console.WriteLine("Enter the new Runtime:");
                Console.WriteLine("'Cancel' to Cancel");
                do
                {
                    Console.Write("-- ");
                    runTimeInput = Console.ReadLine();

                    if (genreInput.ToUpper() == "CANCEL")
                    {
                        return(myBooks);
                    }
                    else if (!int.TryParse(runTimeInput, out intTest))
                    {
                        Console.WriteLine($"Error: {runTimeInput} is not a number, Try Again");
                    }
                } while (runTimeInput != "CANCEL" && (!int.TryParse(runTimeInput, out intTest)));

                Console.Clear();
                Console.WriteLine($"Old Runtime: {runTimeValue}");
                Console.WriteLine($"New Runtime: {runTimeInput}");
                Console.WriteLine();
                Console.WriteLine("'Confirm' to Change");
                Console.WriteLine("'Cancel' to Cancel Change");
                Console.WriteLine("Enter your selection:");

                do
                {
                    Console.Write("-- ");
                    continueInput = Console.ReadLine();

                    switch (continueInput.ToUpper())
                    {
                    case "CONFIRM":
                        int      bookFileLength = File.ReadAllLines("books.txt").Length;
                        string[] tempBooks      = File.ReadAllLines("books.txt");
                        myBooks[searchIndex].SetRuntime(Convert.ToDouble(runTimeInput));
                        tempBooks[searchIndex] = ($"{myBooks[searchIndex].GetIsbn()}#{myBooks[searchIndex].GetTitle()}#{myBooks[searchIndex].GetAuthor()}#{myBooks[searchIndex].GetGenre()}#{myBooks[searchIndex].GetRuntime()}#{myBooks[searchIndex].GetCopies()}");
                        File.Create("books.txt").Close();
                        using (StreamWriter sw = File.AppendText("books.txt"))
                        {
                            for (int i = 0; i != bookFileLength; i++)
                            {
                                sw.WriteLine(tempBooks[i]);
                            }
                        }
                        myBooks = booksFile.GetAllBooks();
                        Console.WriteLine();
                        Console.WriteLine("Book has been edited, new value is:");
                        Console.WriteLine(myBooks[searchIndex].ToString());
                        return(myBooks);

                    case "CANCEL":
                        break;

                    default:
                        Console.WriteLine("Error: Invalid Selection, Try Again");
                        break;
                    }
                } while (continueInput.ToUpper() != "CANCEL" && continueInput.ToUpper() != "CONFIRM"); Console.WriteLine($"Current Genre: {genreValue}");
            }
            else if (editInput.ToUpper() == "COPIES" || editInput.ToUpper() == copiesValue.ToUpper())
            {
                Console.WriteLine($"Current Copies: {copiesValue}");

                Console.WriteLine("Enter the new amount of Copies:");
                Console.WriteLine("'Cancel' to Cancel");
                do
                {
                    Console.Write("-- ");
                    copiesInput = Console.ReadLine();

                    if (copiesInput.ToUpper() == "CANCEL")
                    {
                        return(myBooks);
                    }
                    else if (!int.TryParse(copiesInput, out intTest))
                    {
                        Console.WriteLine($"Error: {copiesInput} is not a number, Try Again");
                    }
                } while (copiesInput.ToUpper() != "CANCEL" && (!int.TryParse(copiesInput, out intTest)));

                Console.Clear();
                Console.WriteLine($"Old Copies: {copiesValue}");
                Console.WriteLine($"New Copies: {copiesInput}");
                Console.WriteLine();
                Console.WriteLine("'Confirm' to Change");
                Console.WriteLine("'Cancel' to Cancel Change");
                Console.WriteLine("Enter your selection:");

                do
                {
                    Console.Write("-- ");
                    continueInput = Console.ReadLine();

                    switch (continueInput.ToUpper())
                    {
                    case "CONFIRM":
                        int      bookFileLength = File.ReadAllLines("books.txt").Length;
                        string[] tempBooks      = File.ReadAllLines("books.txt");
                        myBooks[searchIndex].SetCopies(Convert.ToDouble(copiesInput));
                        tempBooks[searchIndex] = ($"{myBooks[searchIndex].GetIsbn()}#{myBooks[searchIndex].GetTitle()}#{myBooks[searchIndex].GetAuthor()}#{myBooks[searchIndex].GetGenre()}#{myBooks[searchIndex].GetRuntime()}#{myBooks[searchIndex].GetCopies()}");
                        File.Create("books.txt").Close();
                        using (StreamWriter sw = File.AppendText("books.txt"))
                        {
                            for (int i = 0; i != bookFileLength; i++)
                            {
                                sw.WriteLine(tempBooks[i]);
                            }
                        }
                        myBooks = booksFile.GetAllBooks();
                        Console.WriteLine();
                        Console.WriteLine("Book has been edited, new value is:");
                        Console.WriteLine(myBooks[searchIndex].ToString());
                        return(myBooks);

                    case "CANCEL":
                        break;

                    default:
                        Console.WriteLine("Error: Invalid Selection, Try Again");
                        break;
                    }
                } while (continueInput.ToUpper() != "CANCEL" && continueInput.ToUpper() != "CONFIRM");
            }

            return(myBooks);
        }
Esempio n. 3
0
        // this is the method that adds a new book, or just increases the copies
        // count if it already exists
        public static Books[] AddBook()
        {
            BooksFile booksFile = new BooksFile("books.txt");

            Books[] myBooks = booksFile.GetAllBooks();

            string isbnInput    = "";
            string titleInput   = "";
            string authorInput  = "";
            string genreInput   = "";
            string runTimeInput = "";
            string copiesInput  = "";
            string addBookInput = "";
            int    intTest      = 0;

            Console.WriteLine();

            do
            {
                Console.WriteLine("Enter the ISBN:");
                Console.WriteLine("'Cancel' to Cancel");
                Console.Write("-- ");
                isbnInput = Console.ReadLine();

                if (isbnInput.Length == 10)
                {
                    break;
                }
                else if (isbnInput.ToUpper() == "CANCEL")
                {
                    return(myBooks);
                }

                if (!int.TryParse(isbnInput, out intTest))
                {
                    Console.Clear();
                    Console.WriteLine($"Error: '{isbnInput}' is not a number, Try Again.");
                    continue;
                }

                // ISBN Error Handling messages
                if (isbnInput.Length != 10)
                {
                    if (isbnInput.Length < 10)
                    {
                        Console.Clear();
                        Console.WriteLine($"Error: '{isbnInput}' is less than 10 digits, Try Again.");
                    }
                    else if (isbnInput.Length > 10)
                    {
                        Console.Clear();
                        Console.WriteLine($"Error: '{isbnInput}' is more than 10 digits, Try Again.");
                    }
                }
            } while (isbnInput.Length != 10 || isbnInput.ToUpper() != "CANCEL");

            Console.Clear();
            Console.WriteLine("Book Information:");
            Console.WriteLine($"ISBN: {isbnInput}");
            Console.WriteLine();

            bool titleRun = true;

            do
            {
                Console.WriteLine("Enter the Title:");
                Console.WriteLine("'Cancel' to Cancel");
                Console.Write("-- ");
                titleInput = Console.ReadLine();

                if (titleInput.ToUpper() == "CANCEL")
                {
                    titleRun = false;
                    return(myBooks);
                }
                else
                {
                    titleRun = false;
                }
            } while (titleRun == true);

            Console.Clear();
            Console.WriteLine("Book Information:");
            Console.WriteLine($"ISBN: {isbnInput}");
            Console.WriteLine($"Title: {titleInput}");
            Console.WriteLine();

            bool authorRun = true;

            do
            {
                Console.WriteLine("Enter the Author's Name:");
                Console.WriteLine("'Cancel' to Cancel");
                Console.Write("-- ");
                authorInput = Console.ReadLine();

                if (authorInput.ToUpper() == "CANCEL")
                {
                    authorRun = false;
                    return(myBooks);
                }
                else
                {
                    authorRun = false;
                }
            } while (authorRun == true);

            Console.Clear();
            Console.WriteLine("Book Information:");
            Console.WriteLine($"ISBN: {isbnInput}");
            Console.WriteLine($"Title: {titleInput}");
            Console.WriteLine($"Author: {authorInput}");
            Console.WriteLine();

            bool genreRun = true;

            do
            {
                Console.WriteLine("Enter the Genre:");
                Console.WriteLine("'Cancel' to Cancel");
                Console.Write("-- ");
                genreInput = Console.ReadLine();

                if (genreInput.ToUpper() == "CANCEL")
                {
                    genreRun = false;
                    return(myBooks);
                }
                else
                {
                    genreRun = false;
                }
            } while (genreRun == true);

            Console.Clear();
            Console.WriteLine("Book Information:");
            Console.WriteLine($"ISBN: {isbnInput}");
            Console.WriteLine($"Title: {titleInput}");
            Console.WriteLine($"Author: {authorInput}");
            Console.WriteLine($"Genre: {genreInput}");
            Console.WriteLine();

            bool runTimeTest = true;

            do
            {
                Console.WriteLine("Enter the Total Runtime (in minutes):");
                Console.WriteLine("'Cancel' to Cancel");
                Console.Write("-- ");
                runTimeInput = Console.ReadLine();

                // Runtime error handling message / cancel input
                if (runTimeInput.ToUpper() == "CANCEL")
                {
                    return(myBooks);
                }

                if (!int.TryParse(runTimeInput, out intTest))
                {
                    Console.WriteLine($"Error: '{runTimeInput}' is not a number, Try Again.");
                    runTimeTest = true;
                }
                else
                {
                    runTimeTest = false;
                }
            } while (runTimeTest == true);

            Console.Clear();
            Console.WriteLine("Book Information:");
            Console.WriteLine($"ISBN: {isbnInput}");
            Console.WriteLine($"Title: {titleInput}");
            Console.WriteLine($"Author: {authorInput}");
            Console.WriteLine($"Genre: {genreInput}");
            Console.WriteLine($"Runtime: {runTimeInput} minutes");
            Console.WriteLine();

            bool copiesRun = true;

            do
            {
                Console.WriteLine("How many copies of this book are you adding?");
                Console.WriteLine("'Cancel' to Cancel");
                Console.Write("-- ");
                copiesInput = Console.ReadLine();

                // Runtime error handling message / cancel input
                if (copiesInput.ToUpper() == "CANCEL")
                {
                    return(myBooks);
                }
                else if (!int.TryParse(copiesInput, out intTest))
                {
                    Console.WriteLine($"Error: '{copiesInput}' is not a number, Try Again.");
                    copiesRun = true;
                }
                else
                {
                    copiesRun = false;
                }
            } while (copiesRun == true);

            Console.Clear();
            Console.WriteLine("Book Information:");
            Console.WriteLine($"ISBN: {isbnInput}");
            Console.WriteLine($"Title: {titleInput}");
            Console.WriteLine($"Author: {authorInput}");
            Console.WriteLine($"Genre: {genreInput}");
            Console.WriteLine($"Runtime: {runTimeInput} minutes");
            Console.WriteLine($"Copies: {copiesInput}");
            Console.WriteLine();

            Console.WriteLine("Would you like to add this book?:");
            Console.WriteLine("'Yes' to Confirm");
            Console.WriteLine("'No' to Cancel");
            do
            {
                Console.Write("-- ");
                addBookInput = Console.ReadLine();
                switch (addBookInput.ToUpper())
                {
                case "YES":
                    Console.WriteLine();
                    Console.WriteLine("Book Added:");
                    using (StreamWriter sw = File.AppendText("books.txt"))
                    {
                        sw.WriteLine($"{isbnInput}#{titleInput}#{authorInput}#{genreInput}#{runTimeInput}#{copiesInput}");
                    }
                    int lines = File.ReadAllLines("books.txt").Length;
                    myBooks = booksFile.GetAllBooks();
                    Console.WriteLine($"{myBooks[lines-1].ToString()}");
                    return(myBooks);

                case "NO":
                    Console.WriteLine("Thank you, Goodbye!");
                    return(myBooks);

                default:
                    Console.WriteLine($"Error {addBookInput} is not a valid selection, Try Again");
                    break;
                }
            } while(addBookInput.ToUpper() != "NO" && addBookInput.ToUpper() != "YES");
            return(myBooks);
        }
Esempio n. 4
0
        // method to rent the book, returns myTransactions value
        // this method recalls every object in itself multiple times
        // which I have actually learned not to necessarily do
        public Transactions[] RentBook()
        {
            TransactionsFile transactionsFile = new TransactionsFile("transactions.txt");

            Transactions[]      myTransactions      = transactionsFile.GetAllTransactions();
            TransactionsReports transactionsReports = new TransactionsReports(myTransactions);
            TransactionsUtility transactionsUtility = new TransactionsUtility(myTransactions);

            BooksFile booksFile = new BooksFile("books.txt");

            Books[]      myBooks      = booksFile.GetAllBooks();
            BooksReports booksReports = new BooksReports(myBooks);
            BooksUtility booksUtility = new BooksUtility(myBooks);

            string   customerNameInput  = "";
            string   customerEmailInput = "";
            string   isbnInput          = "";
            string   confirmInput       = "";
            string   defaultReturnDate  = "0/0/0000";
            double   isbnDoubleInput    = 0;
            int      searchIndex        = 0;
            int      transactionLength  = File.ReadAllLines("transactions.txt").Length;
            double   rentalId           = transactionLength + 1;
            DateTime currentDate        = DateTime.Today;

            Console.WriteLine();
            booksReports.PrintAllBooks();
            Console.WriteLine();

            Console.WriteLine("Select an ISBN to Rent:");
            Console.WriteLine("'Cancel' to Cancel");
            do
            {
                Console.Write("-- ");
                isbnInput = Console.ReadLine();
                if (isbnInput.ToUpper() == "CANCEL")
                {
                    return(myTransactions);
                }
                else if (isbnInput.Length > 10)
                {
                    Console.WriteLine($"Error: {isbnInput} is an Invalid ISBN, Try Again");
                }
                else if (isbnInput.Length < 10)
                {
                    Console.WriteLine($"Error: {isbnInput} is an Invalid ISBN, Try Again");
                }
                else if (isbnInput.Length == 10)
                {
                    isbnDoubleInput = Convert.ToDouble(isbnInput);
                    searchIndex     = booksUtility.SequentialSearch(isbnDoubleInput);
                    if (searchIndex == -1)
                    {
                        Console.WriteLine("Error: Book does not exist");
                    }
                    else
                    {
                        Console.Clear();
                    }
                }

                if (myBooks[searchIndex].GetCopies() == 0)
                {
                    Console.WriteLine("Error: There are no copies left");
                    return(myTransactions);
                }

                Console.WriteLine("Checking out:");
                Console.WriteLine($"Rental ID: {rentalId:0000}");
                Console.WriteLine($"Title: {myBooks[searchIndex].GetTitle()}");
                Console.WriteLine();

                Console.WriteLine("Enter Customer Email:");
                Console.WriteLine("'Cancel' to Cancel");
                Console.Write("-- ");
                customerEmailInput = Console.ReadLine();

                if (customerEmailInput.ToUpper() == "CANCEL")
                {
                    return(myTransactions);
                }

                string[] lines = File.ReadAllLines("transactions.txt");
                if (lines.Length != 0)
                {
                    foreach (string line in lines)
                    {
                        string[] transactionInfo = line.Split('#');
                        if (transactionInfo[3] == customerEmailInput)
                        {
                            if (transactionInfo[1] == isbnInput)
                            {
                                Console.WriteLine();
                                Console.WriteLine("Error: You already have a copy of this book");
                                return(myTransactions);
                            }
                        }
                    }
                }
            } while (isbnInput.ToUpper() != "CANCEL" && isbnInput.Length != 10);

            Console.WriteLine("Enter Customer Name:");
            Console.WriteLine("'Cancel' to Cancel");
            Console.Write("-- ");
            customerNameInput = Console.ReadLine();

            if (customerNameInput.ToUpper() == "CANCEL")
            {
                return(myTransactions);
            }

            Console.Clear();
            Console.WriteLine("Checking out:");
            Console.WriteLine($"Rental ID: {rentalId:0000}");
            Console.WriteLine($"Title: {myBooks[searchIndex].GetTitle()}");
            Console.WriteLine($"Customer Name: {customerNameInput}");
            Console.WriteLine($"Customer Email: {customerEmailInput}");
            Console.WriteLine($"Rental Date: {currentDate:d}");
            Console.WriteLine($"Return Date: {defaultReturnDate}");
            Console.WriteLine();

            Console.WriteLine("'Confirm' to Change");
            Console.WriteLine("'Cancel' to Cancel Change");
            Console.WriteLine("Enter your selection:");

            do
            {
                Console.Write("-- ");
                confirmInput = Console.ReadLine();

                switch (confirmInput.ToUpper())
                {
                case "CONFIRM":
                    int      bookFileLength = File.ReadAllLines("books.txt").Length;
                    string[] tempBooks      = File.ReadAllLines("books.txt");
                    myBooks[searchIndex].SetCopies(Convert.ToDouble(myBooks[searchIndex].GetCopies()) - 1);
                    tempBooks[searchIndex] = ($"{myBooks[searchIndex].GetIsbn()}#{myBooks[searchIndex].GetTitle()}#{myBooks[searchIndex].GetAuthor()}#{myBooks[searchIndex].GetGenre()}#{myBooks[searchIndex].GetRuntime()}#{myBooks[searchIndex].GetCopies()}");
                    File.Create("books.txt").Close();
                    using (StreamWriter sw = File.AppendText("books.txt"))
                    {
                        for (int i = 0; i != bookFileLength; i++)
                        {
                            sw.WriteLine(tempBooks[i]);
                        }
                    }
                    using (StreamWriter sw = File.AppendText("transactions.txt"))
                    {
                        sw.WriteLine($"{rentalId:0000}#{isbnInput}#{customerNameInput}#{customerEmailInput}#{currentDate:d}#{defaultReturnDate}");
                    }
                    Console.WriteLine();
                    Console.WriteLine("Transaction has been added.");
                    myBooks        = booksFile.GetAllBooks();
                    myTransactions = transactionsFile.GetAllTransactions();
                    return(myTransactions);

                case "CANCEL":
                    break;

                default:
                    Console.WriteLine("Error: Invalid Selection, Try Again");
                    break;
                }
            } while (confirmInput.ToUpper() != "CANCEL" && confirmInput.ToUpper() != "CONFIRM");

            return(myTransactions);
        }