Click here to Skip to main content
15,119,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing code for a project where users can see and rate products in a store (Like Yelp for products). I have never used a nested switch case and I keep breaking the code or it stops working all together. I was wondering if I could get some fresh eyes on it.

You will see the options when the program runs. If an object exists (I add a product first) then I choose the second option (which gives the user the option to rate the product). Inside that switch case, I want to have the user rate the product on a scale from 1-3, then add that rating into the ArrayList ratings to later calculate.

Am I doing something wrong or impossible? (New to programming, I apologize if this seems elementary)

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();
       }


What I have tried:

I have a scanner for the first switch cases. I have tried the nested switch with AND without it's own scanner, in which neither works.

for instance:
Scanner rateScan = new Scanner(System.in)
     rateOption = rateScan.nextInt();
Posted
Updated 5-May-21 9:15am

Missing the "break" after default: in your nested switch,

default: System.out.println("You entered an invalid
                              option");


(The extra indent threw you off).
   
v2
Try to replace
Java
System.out.println("How would you rate this product?);

with
Java
System.out.println("How would you rate this product?");

it may help.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900