Hi guys. I just having a little problem about creating a good console GUI for our homework. My program is just a simple shopping cart program where in we were asked to create a GUI that asks for the user if he is either a ADMIN user or a CLERK user. I made the features of it work already, but I don't know how to loop back again to my "main screen" after an action has been done by the user.

In other words, the user could only process one action per execution of my program. Here's a scenario for example, let's say the user is an ADMIN and he wants to ADD ITEM:
MAIN SCREEN->ADMIN USER->ADD ITEM->EXIT THE PROGRAM.
In this case, the user has no choice but to run the program again.

I want it to be:
MAIN SCREEN->ADMIN USER->ADD ITEM->MAIN SCREEN AGAIN.
In this case, the user is given a choice of adding another item or exiting the program after adding an item already

Here's part of the code from what I've done so far:

Admin ad = new Admin();
Scanner s = new Scanner(System.in);
//my "main screen"
System.out.println("Press [1] to CREATE an Item. Press [2] to REMOVE Item. Press [3] UPDATE Item. Press [4] to EXIT");
                int choice = s.nextInt();
                switch(choice){
                    case 1:
                        //add item
                        System.out.println("Enter item ID: ");
                        int itemId = s.nextInt();
                        System.out.println("Enter item Name: ");
                        String itemName = sc.nextLine();
                        System.out.println("Enter item Price: ");
                        int itemPrice = s.nextInt();

                        ad.AddItem(itemId, itemPrice, itemName);
                        
                        break;
                    case 2:
                        //remove item
                        break;
                    case 3:
                        //update item
                        break;
                    case 4:
                        //exit
                        break;
                    default:
                        break;
                }
                
                }

Hope you could help guys :)

P.S. I also did this in an if-else pattern. But I think doing it in a switch is better..

Recommended Answers

All 3 Replies

but I don't know how to loop back again to my "main screen" after an action has been done by the user.

You could simply put the whole menu in a while loop that runs while choice is not equal to 4(the exit in your code)

You could simply put the whole menu in a while loop that runs while choice is not equal to 4(the exit in your code)

Thanks! It's working now. I knew there had to be a simple solution. :)

i it solve your problem then please marked this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.