Granskning och Råd om Java-program för Frukt- och Grönt-våg i Matbutiksuppgift

Permalänk
Medlem

Granskning och Råd om Java-program för Frukt- och Grönt-våg i Matbutiksuppgift

Jag arbetar med en Java-uppgift från min skola där jag ska simulera en konsolbaserad applikation för en frukt- och grönt-våg i en matbutik. Jag har skapat klassen 'Product' med olika egenskaper och metoder för att lägga till, ta bort och söka efter produkter. Jag undrar om någon kan hjälpa mig att granska min kod och se om det finns några potentiella fel eller förbättringar? Kan man se att programmet kraschar någonstans. Tack på förhand!

Min kod:

import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static Scanner input = new Scanner(System.in); public static ArrayList<Product> allFruitsandVegetables = new ArrayList<Product>(); public static ArrayList<String> allUniqueItems = new ArrayList<String>(); public static ArrayList<String> allUniqueItems2 = new ArrayList<String>(); public static boolean quitProgram = false; static String[] menuOptions = {"\n1. Add a product.", "2. Search the catalog.", "3. Browse the catalog.", "4. Remove a product.", "5. Print all products", "6. Exit the program"}; static String[] catalogOptions = {"1. Browse the catalog.", "2. Search the catalog.", "3. Go back to the main menu."}; public static void main(String[] args) { allFruitsandVegetables.add(new Product("Banan", 10.50, "kg", "Frukt", "Rund")); allFruitsandVegetables.add(new Product("Äpple", 5.64, "pcs", "Frukt", "Rund")); allFruitsandVegetables.add(new Product("Päron", 6.55, "pcs", "Frukt", "Rund")); allFruitsandVegetables.add(new Product("Jordgubb", 7.95, "st", "Frukt", "Bär")); allFruitsandVegetables.add(new Product("Mango", 5.77, "pcs", "Frukt", "Rund")); allFruitsandVegetables.add(new Product("Kiwi", 4.67, "pcs", "Frukt", "Rund")); allFruitsandVegetables.add(new Product("Gurka", 12.0, "pcs", "Grönsak", "Lång")); allFruitsandVegetables.add(new Product("Tomat", 7.54, "pcs", "Grönsak", "Rotfrukt")); allFruitsandVegetables.add(new Product("Broccoli", 6.33, "pcs", "Grönsak", "Rotfrukt")); allFruitsandVegetables.add(new Product("Vitkål", 45.65, "pcs", "Grönsak", "Rotfrukt")); allFruitsandVegetables.add(new Product("Morot", 6.70, "pcs", "Grönsak", "Rotfrukt")); allFruitsandVegetables.add(new Product("Kålrot", 3.55, "pcs", "Grönsak", "Rotfrukt")); allFruitsandVegetables.add(new Product("Champinjon", 4.22, "pcs", "Grönsak", "Svamp")); allFruitsandVegetables.add(new Product("Kantarell", 10.77, "pcs", "Grönsak", "Svamp")); allFruitsandVegetables.add(new Product("Karl-Johan", 3.56, "pcs", "Grönsak", "Svamp")); allFruitsandVegetables.add(new Product("Potatis", 3.99, "pcs", "Grönsak", "Rotfrukt")); allFruitsandVegetables.add(new Product("Aubergine", 13.44, "pcs", "Grönsak", "Svart")); allFruitsandVegetables.add(new Product("Zucchini", 11.98, "pcs", "Grönsak", "Svart")); allFruitsandVegetables.add(new Product("Gul lök", 2.33, "pcs", "Grönsak", "Lök")); allFruitsandVegetables.add(new Product("Röd lök", 2.55, "pcs", "Grönsak", "Lök")); showIntroduction(); runTheMainMenu(); printQuitProgramMessage(); } public static void showIntroduction () { System.out.println("Welcome to the Fruit and Vegetable program."); System.out.print("Please enter any key to continue to the meny."); String employeeInput = input.nextLine(); } public static void runTheMainMenu () { do { for (String listOfMenuOptions : menuOptions) { System.out.println(listOfMenuOptions); } System.out.print("Enter one of the following numbers to continue: "); try { int employeeInput = input.nextInt(); input.nextLine(); switch (employeeInput) { case 1 -> addFruitOrVegetable(); case 2 -> searchTheCatalog(); case 3 -> browseTheCatalog(); case 4 -> removeProduct(); case 5 -> printAllProducts(); case 6 -> quitProgram = true; default -> System.out.println("\nThe input is not valid, please enter a number between 1-6. "); } } catch (InputMismatchException e) { System.out.println("\nThe input is not valid, you cannot enter a letter, please enter a number."); input.nextLine(); } } while (!quitProgram); } public static void printQuitProgramMessage () { System.out.println("\nThank you for using the Fruit and Vegetable program."); System.out.println("All credit goes to Kerem Incorporated."); } public static void addFruitOrVegetable() { double userSetPrice = 0.0; String userSetPricePerPcsOrKg = ""; String userSetFruitOrVegetable = ""; String userSetProductType = ""; System.out.print("\nPlease enter the name of the product: "); String userSetName = input.nextLine(); Product createdUserProduct = new Product(userSetName, 0, "", "", ""); System.out.print("Do you want to add price? Please write yes or any key to continue: "); String userChoicePrice = input.nextLine(); if (userChoicePrice.equalsIgnoreCase("Yes")) { System.out.print("Please enter a price: "); userSetPrice = input.nextInt(); input.nextLine(); System.out.print("Enter if price is per piece (pcs) or per kilogram (kg). Write pcs or kg: "); userSetPricePerPcsOrKg = input.nextLine(); createdUserProduct = new Product(userSetName, userSetPrice, userSetPricePerPcsOrKg, "", ""); } System.out.print("Do you want add if product is a fruit or vegetable? Please write yes or any key to continue: "); String userChoiceFruitOrVegetable = input.nextLine(); if (userChoiceFruitOrVegetable.equalsIgnoreCase("Yes")) { System.out.print("Please enter if product is a fruit or vegetable: "); userSetFruitOrVegetable = input.nextLine(); createdUserProduct = new Product(userSetName, userSetPrice, userSetPricePerPcsOrKg, userSetFruitOrVegetable, ""); } System.out.print("Do you want to add the producttype? Please write yes or any key to continue: "); String userChoiceProductType = input.nextLine(); if (userChoiceProductType.equalsIgnoreCase("Yes")) { System.out.print("Please enter the producttype: "); userSetProductType = input.nextLine(); } createdUserProduct = new Product(userSetName, userSetPrice, userSetPricePerPcsOrKg, userSetFruitOrVegetable, userSetProductType); System.out.print("\n\033[1mYou added: \033[0m"); System.out.print(createdUserProduct.toString()); System.out.println(""); allFruitsandVegetables.add(createdUserProduct); } public static void searchTheCatalog() { boolean runSearchCatalog = false; do { System.out.print("\nPlease enter a keyword: "); String searchTerm = input.nextLine().trim(); if (searchTerm.isEmpty()) { System.out.println("\nPlease enter a valid keyword to search."); return; } boolean resultFoundInCatalog = false; for (Product catalog : allFruitsandVegetables) { if (catalog.getName().toLowerCase().contains(searchTerm.toLowerCase())) { System.out.println("\n\033[1mThe result of your search:\033[0m"); System.out.println(catalog.toString()); System.out.println("\nDo you want to calculate price for the product?"); System.out.print("Please enter yes to accept or any key to continue: "); String userChoiceYesOrNo = input.nextLine(); if (userChoiceYesOrNo.equalsIgnoreCase("yes")) { System.out.print("Please enter quantity: "); double userChoiceQuantity = input.nextDouble(); input.nextLine(); calculatePrice(userChoiceQuantity, catalog.getPrice()); } resultFoundInCatalog = true; } } if (!resultFoundInCatalog) { System.out.println("\nNo search results found."); } System.out.print("Do you want to exit to the main menu? Write Yes/Any key to exit: "); String exitSearchCatalog = input.nextLine(); if (exitSearchCatalog.equalsIgnoreCase("Yes")) { runSearchCatalog = true; } } while (!runSearchCatalog); } public static void browseTheCatalog() { boolean quitCatalog = false; System.out.println("\nWelcome to the product catalog, do you want to:"); do { for (String browsingCatalog : catalogOptions) { System.out.println(browsingCatalog); } System.out.print("Please enter a number: "); int userCatalogChoice = input.nextInt(); input.nextLine(); if (userCatalogChoice == 1) { findUniquePrimaryType(); System.out.print("Please choose from the options: "); findUniqueProductType(); System.out.print("Please choose from the options: "); printFullItemFromInput(); } else if (userCatalogChoice == 2) { searchTheCatalog(); } else if (userCatalogChoice == 3) { quitCatalog = true; } } while (!quitCatalog); } public static void removeProduct() { boolean runRemoveProduct = false; do { boolean doesProductExist = false; System.out.print("\nPlease enter the name of the product you want to remove: "); String userChoice = input.nextLine(); for (int i = 0; i < allFruitsandVegetables.size(); i++) { if (allFruitsandVegetables.get(i).getName().equalsIgnoreCase(userChoice)) { Product removedProduct = allFruitsandVegetables.remove(i); System.out.println("You removed " + removedProduct.getName() + "."); doesProductExist = true; } } if (!doesProductExist) { System.out.println("\nThis product does not exist."); } System.out.print("Type yes to exit or any key to begin again: "); String userChoiceToExit = input.nextLine(); if (userChoiceToExit.equalsIgnoreCase("Yes")) { runRemoveProduct = true; } } while (!runRemoveProduct); } public static void printAllProducts() { for (Product allProducts : allFruitsandVegetables) { System.out.println(allProducts); } } public static void calculatePrice(double quantity, double price) { double sum = quantity * price; System.out.println("The total price will be: " + sum + " kr."); } public static void findUniquePrimaryType() { allUniqueItems.clear(); for (Product uniqueItem : allFruitsandVegetables) { boolean doesItemExist = false; for (String uniqueProduct : allUniqueItems) { if (uniqueProduct.equals(uniqueItem.getFruitOrVegetable())) { doesItemExist = true; } } if (!doesItemExist) { allUniqueItems.add(uniqueItem.getFruitOrVegetable()); } doesItemExist = false; } System.out.println("\nCategory options: "); for (String primaryTypes : allUniqueItems) { System.out.println(primaryTypes); } } public static void findUniqueProductType() { String firstUserCatalogChoice = input.nextLine(); if (allUniqueItems.contains(firstUserCatalogChoice)) { for (Product uniqueItem2 : allFruitsandVegetables) { if (uniqueItem2.getFruitOrVegetable().equals(firstUserCatalogChoice)) { boolean doesItemExist = false; for (String uniqueItems2 : allUniqueItems2) { if (uniqueItems2.equals(uniqueItem2.getProductType())) { doesItemExist = true; } } if (!doesItemExist) { allUniqueItems2.add(uniqueItem2.getProductType()); } doesItemExist = false; } } System.out.println("\nCategory options: "); for (String secondaryTypes : allUniqueItems2) { System.out.println(secondaryTypes); } } } public static void printFullItemFromInput (){ String firstUserCatalogChoice = input.nextLine(); if (allUniqueItems2.contains(firstUserCatalogChoice)) { System.out.println("\n\033[1mFull products: \033[0m"); for (Product fullProduct : allFruitsandVegetables) { if (fullProduct.getProductType().equals(firstUserCatalogChoice)) { System.out.println(fullProduct.toString()); } } } } } public class Product { private String name; private double price; private String pricePerPcsOrKg; private String fruitOrVegetable; private String productType; public Product (String name){ this.name = name; } public Product (String name, double price){ this.name = name; this.price = price; } public Product (String name, double price, String pricePerPcsOrKg, String fruitOrVegetable, String productType){ this.name = name; this.price = price; this.pricePerPcsOrKg = pricePerPcsOrKg; this.fruitOrVegetable = fruitOrVegetable; this.productType = productType; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getPricePerPcsOrKg() { return pricePerPcsOrKg; } public void userSetPricePerPcsOrKg(String pricePerPcsOrKg) { this.pricePerPcsOrKg = pricePerPcsOrKg; } public String getFruitOrVegetable(){ return fruitOrVegetable; } public void setFruitOrVegetable(String fruitOrVegetable){ this.fruitOrVegetable = fruitOrVegetable; } public String getProductType() { return productType; } public void setProductType(String productType) { this.productType = productType; } @Override public String toString() { return "\nName of the product: " + name + "\nPrice: " + price + " kr per " + pricePerPcsOrKg + "\nPrimarytype: " + fruitOrVegetable + "\nSecondarytype: " + productType; } }

Permalänk
Medlem

Vänligen posta din kod med [code] taggar, annars orkar nog ingen läsa din kod