public void Display() { Console.Clear(); //Display menu options Console.WriteLine(@" _______ ______ ___ __ __ /__ __// / ____// | || / //________________________/ //__(_))___________________ / // / __// | || / // / _\\ / ___ \\ / __ // / // / ___ \\ / ___ `// / // / //___ | ||/ // / __// / // / // / //_/ // / // / // / // / //_/ // /_// /_____// |____// \___// /_// /_// \___,_// /_// /_// /_// \__, // /_____// ************************************************* ** Please choose from the following options: ** ** 1. Display Vending Machine Items ** ** 2. Purchase Menu ** ** 3. Exit ** ** ** *************************************************"); //Get user input int selection; Console.WriteLine(); Console.Write("Please enter your selection: "); bool isValid = int.TryParse(Console.ReadLine(), out selection); //Validate the user input while (!isValid || selection < 1 || selection > 4) { Console.WriteLine("You did not enter a valid choice please enter the number corresponding to your selection"); isValid = int.TryParse(Console.ReadLine(), out selection); } // Choose which methods to call based on user selection if (selection == 1) { // Clear the console and display Menu Items Console.Clear(); DisplayItems.Display(vm); Console.WriteLine(); Console.WriteLine("Press Enter to return to main menu"); Console.ReadLine(); Display(); } else if (selection == 2) { // Display Purchase Menu by creating a new purchase menu object and passing the vending machine reference to its contructor PurchaseMenu pm = new PurchaseMenu(vm); pm.Display(); } else if (selection == 3) { // Exit Program return; } else if (selection == 4) { // Generate a sales report and print it to file. vm.PrintSalesReport(); //Inform the user that a report was generated and return to main menu Console.WriteLine(); Console.WriteLine("Sales report generated, check the SalesReport folder to view the report."); Console.WriteLine("Press enter to continue..."); Console.ReadLine(); Display(); } }
//Method to display a submenu that allows the user to purchase an item by slot ID private void PurchaseItemSubMenu() { //Clear the console before displaying the purchase submenu Console.Clear(); //Display all items in the vending machine DisplayItems.Display(vm); //Ask the user to enter a slot number corresponding to the item they want to purchase Console.WriteLine(); Console.Write("Please enter an item to purchase by slot (A1, B3, etc): "); string input = Console.ReadLine().ToUpper(); //Add a ToUpper method to user input for case insensitivity //Pass user input to purchase item method which will determine if it is a valid purchase Item purchasedItem = null; try { purchasedItem = vm.PurchaseItem(input); } catch (PurchaseItemExceptionInvalidSlot) { Console.WriteLine(); Console.WriteLine("The slot entered does not exist, returning to the purchase menu"); } catch (PurchaseItemExceptionItemSoldOut) { Console.WriteLine(); Console.WriteLine("Sorry but the item you selected is sold out, returning to the purchase menu"); } catch (PurchaseItemExceptionInsufficientFunds) { Console.WriteLine(); Console.WriteLine("Sorry you do not have enough funds to purchase that item, returning to the purchase menu"); } catch (ItemStuckException) { Console.WriteLine(); Console.WriteLine($"The {purchasedItem.Name} you have purchased appears to be stuck, what would you like to do?"); Console.WriteLine(@" 1. Return to Purchase Menu. 2. Kick the Machine."); string input1 = Console.ReadLine(); int response = int.Parse(input1); if (response < 1 || response > 2) { Console.WriteLine("You have made an incorrect selection. Please try again."); return; } else { IsStuck(response); } } //If purchase is succesful display item info if (purchasedItem != null) { Console.WriteLine($@" You succesfully purchased {purchasedItem.Name}! {vm.saleMessage[purchasedItem.Type]} {purchasedItem.Price:c} has been deducted from your balance!"); } //After returning from purchase item wait for the user to press enter before going back to main purchase menu Console.WriteLine(); Console.WriteLine("Press Enter to continue.."); Console.ReadLine(); }