public class TestProject { public static void main(String[] args) { List<Product> products = new ArrayList<Product>(); Scanner scan = new Scanner(System.in); boolean isExit = false; while (!isExit) { System.out.println("Please make your selection from the following options"); System.out.println("Press 1 : Search for a Product"); System.out.println("Press 2 : Rate a Product"); System.out.println("Press 3 : Compare with other products"); System.out.println("Press 4 : Add Product"); System.out.println("Press 5 : Exit"); System.out.print("Please select appropriate option : "); int option = scan.nextInt(); int rateOption = 0; ArrayList<Integer> ratings = new ArrayList<Integer>(); scan.nextLine(); switch (option) { case 1: System.out.print("Please enter Product name :"); String productName = scan.nextLine(); for (Product product : products) { if (product.getName().equals(productName)) System.out.println(product.toString()); } break; case 2: System.out.print("Please enter Product name :"); String prodName = scan.nextLine(); for (Product product : products) { if (product.getName().equals(prodName)) { System.out.println("How would you rate this product?); System.out.println("Enter (1) if you would not recommend the product"); System.out.println("Enter (2) if you found the product to be satisfactory"); System.out.println("Enter (3) if you would recommend the product"); } switch (rateOption) { case 1: ratings.add(1); break; case 2: ratings.add(2); break; case 3: ratings.add(3); break; default: System.out.println("You entered an invalid option"); } } break; case 3: System.out.println("Products printed in sorted order"); Collections.sort(products); for (Product product : products) { System.out.println(product.toString()); } break; case 4: System.out.print("Which product you want to add? (F)ood or (N)onFood : "); String type = scan.nextLine(); System.out.print("Please enter product Name : "); String proName = scan.nextLine(); System.out.print("Please enter image url : "); String image = scan.nextLine(); Product product = null; if (type.equals("F")) { System.out.print("Please enter recipe file :"); String recipeFile = scan.nextLine(); product = new Food(proName, image); try { ((Food) product).setRecipeFile(recipeFile); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } } else { product = new NonFood(proName, image); } products.add(product); break; case 5: isExit = true; break; default: isExit = true; break; } System.out.println(); } scan.close(); }
Scanner rateScan = new Scanner(System.in) rateOption = rateScan.nextInt();
System.out.println("How would you rate this product?);
System.out.println("How would you rate this product?");
default: System.out.println("You entered an invalid option");
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)