import java.util.Scanner; class main { public static void main(String[] args) { // Create scanner object Scanner input = new Scanner(System.in); // Declare variables double bookPrice = 0.0; String bookName; int bookQuantity = 0; int menuOption = 0; int arrayLength = 0; String newBook; int arrayIndex=0; double purchasePrice = 0.0; // Declare empty arrays (30 spaces) - all contain NULL String bookNamesArray[] = new String[30]; double bookPricesArray[] = new double[30]; int bookQtysInStockArray[] = new int[30]; //Loop until user selects 5. Exit from the menu while(menuOption <5) { // Output the menu System.out.println("\nMenu"); System.out.println("1. Add Stock"); System.out.println("2. Update Stock"); System.out.println("3. Display Stock"); System.out.println("4. Sales"); System.out.println("5. Exit"); //Request menu option menuOption = input.nextInt(); if (menuOption==5) // exit program { System.out.print("Exit Program"); input.close(); // Close Scanner } else { if (menuOption == 1) // Add stock { System.out.print("Please enter the new book name: "); bookName = input.next(); // check if book already exists in stock newBook = "Yes"; // set default to true try { for(int i = 0; i < arrayLength; i++) // loop through the BookNames array { // compare the entered book name to the book names in the array if (bookNamesArray[i].trim().equals(bookName.trim())) { newBook = "No"; } } } catch (Exception e) { e.printStackTrace(); } if (newBook == "Yes") { // request entry of price and quantity to add to stock System.out.print("Please enter the new book price: £"); bookPrice = input.nextDouble(); System.out.print("Please enter the new book stock quantity: "); bookQuantity = input.nextInt(); // Store the new book name, price and quantity in arrays bookNamesArray[arrayLength] = bookName.trim(); bookPricesArray[arrayLength] = bookPrice; bookQtysInStockArray[arrayLength] = bookQuantity; arrayLength++; // Add 1 to array length System.out.println("Book " + bookName + " has been added to stock"); } else { System.out.println("Book " + bookName + " is already a stock item"); } } else if (menuOption == 2)// Update Stock (quantity only) { System.out.print("Please enter the book name: "); bookName = input.next(); // check if book already exists in stock newBook = "Yes"; // set default to true try { for(int i = 0; i < arrayLength; i++) // loop through the BookNames array { // compare the entered book name to the book names in the array if (bookNamesArray[i].trim().equals(bookName.trim())) { newBook = "No"; arrayIndex = i;//store the index of book so qty can be adjusted break; } } } catch (Exception e) { e.printStackTrace(); } if (newBook == "No") { // request entry of quantity to adjust stock System.out.print("Please enter the quantity to adjust stock: "); bookQuantity = input.nextInt(); // Update the quantity array for the book bookQtysInStockArray[arrayIndex] = bookQtysInStockArray[arrayIndex] + bookQuantity; System.out.println("The stock for book " + bookName + " has been updated"); } else { System.out.println("Book " + bookName + " does not exist"); } } else if (menuOption == 3) // Display Stock { System.out.println("\nStock List"); for(int i = 0; i < arrayLength; i++) // loop through the arrays { System.out.println((i + 1) + ". " + bookNamesArray[i] + " - £" + bookPricesArray[i] + " (" + bookQtysInStockArray[i] + " in stock)"); } } else if (menuOption == 4) { System.out.print("Please enter the book name: "); bookName = input.next(); // check if book already exists in stock newBook = "Yes"; // set default to true try { for(int i = 0; i < arrayLength; i++) // loop through the BookNames array { // compare the entered book name to the book names in the array if (bookNamesArray[i].trim().equals(bookName.trim())) { newBook = "No"; arrayIndex = i;//store the index of book so qty can be checked/adjusted } } } catch (Exception e) { e.printStackTrace(); } if (newBook == "No") // Book exists { // request entry of quantity to purchase System.out.print("Please enter the quantity to be purchased: "); bookQuantity = input.nextInt(); if (bookQuantity <= bookQtysInStockArray[arrayIndex]) // check if there's enough stock { // calculate the purchase price by multiplying the book price (in the Book prices array) by the quantity purchasd purchasePrice = ((double)bookPricesArray[arrayIndex] * bookQuantity); System.out.println("Purchase price= £" + purchasePrice); // Update the quantity array for the book to subtract what has been sold bookQtysInStockArray[arrayIndex] = bookQtysInStockArray[arrayIndex] - bookQuantity; } else // not enough stock { System.out.println("There is not enough in stock to purchase this quantity"); } } else { System.out.println("Book " + bookName + " does not exist"); } } else { System.out.println("Invalid menu option. Please try again!"); } } } } }