Java Fast Food Menu (Using Methods)

Menu with multiple options java

Building off of the comment, you are not adding into the total correctly.
The correct syntax is:

total += Cookie; 

or

total = total + Cookie;

Also since you mentioned that the code is supposed to allow multiple selections you must implement code to allow more than one selection. There are a few ways to do this:

A nice structure to use would be the structure similar to the one recommended for getopt_long() in C but for Java obviously. Can read the man page here https://linux.die.net/man/3/getopt_long

double option;
while(true)
{
option = input.nextDouble();

if(option == 0) //If option is 0 then you quit asking the user
{
break; //session terminated
}

switch (option) {
case 1.0:
total += spaghetti;
break;

case 2.0:
total = total + hotdog;
break;
case 3.0:
total += Cookie;
break;
case 4.0:
total = total + ChickenNuggets;
break;

default:
System.out.println("Wrong input, try again");
break;
}

}

Another good solution to this problem, if you don't want to use the switch statement, is the loop-and-a-half.
https://codehs.gitbooks.io/introcs/content/Basic-JavaScript-and-Graphics/loop-and-a-half.html

The structure goes like this:

double option;
while(true)
{
option = input.nextDouble();

if(option == 1)
{
// Do something
}
else if(option == 2)
{
// Do something
}
// Add rest of options
else if(option == 0) //If user types a 0 we quit
{
break;
}
else
{
System.out.println("Invalid Option");
}
}

Feel free to change and manipulate the code to suit your needs. This is merely a suggested way to perform the function and is best suited to be treated as a skeleton.

Java Restaurant Menu, calculation issue

Take a look at your switch statement (you're never really using it).

You should have something like this:

double price = 0.0;  //You will reassign this

switch (orderNumber) {

case 1: price = Chicken ;
break;
case 2: price = Pizza;
break;
case 3: price = Veal;
break;
case 4: price = Alfredo;
break;
case 5: price = Garlic;
break;
case 6: price = Veggie;
break;
case 7: price = Spaghetti;
break;
case 8: price = Raviolli;
break;
case 9: price = Meat;
break;
case 10: price = Canolli;
break;

double total_price = price * orderAmount; //You currently have this as an addition

According to basic oop conventions, should the menu code be in the main method or in a class?

If you want to strictly follow the OOP convention you could have your own class like

abstract class MenuItem {
int index;

protected MenuItem(int index) {
this.index = index;
}

abstract void action();
}

class ExitMenuItem {
ExitMenuItem() {
super(3); // the index value of the menu item
}

void action() {
System.exit(0);
}
}

So that you can have a subclass specific for every menu item that has its behavior.

Then you could have a menu container, something like:

class Menu {
ArrayList<MenuItem> items;

void executeAction(String input) {
..
}
}

that should understand which voice has been chosen according to the stdin input and execute it. Of course you can enrich everything by having a default text for every menu item and whatever. Then in your main method you just instantiate the menu and take care of forwarding stdin input to it.

Mind that this is usually overkill in a simple menu but if you want to understand OOP then it's a nice way to go.

Dynamically building a menu

You can mix some different method calls and type checking to accomplish this task.

The following code dynamically builds the menu, discarding duplicates.

public static void main(String[] args) {
// just example input
String[][] menuHierarchies = {
{"New", "Job", "Item A"},
{"New", "Job", "Item B"},
{"New", "Search", "Item C"},
{"New", "Item D"},
{"Other", "Item E"},
{"New", "Job", "Item B"},
{"Other", "Job", "Item B"}
};

JMenuBar menuBar = new JMenuBar();

// relevant code from here. It builds the menu removing redundancies
for (int rootIndex = 0; rootIndex < menuHierarchies.length; ++rootIndex) {
JMenu parentMenu = null;

for(int menuLevel = 0; menuLevel < menuHierarchies[rootIndex].length; ++menuLevel) {
// if it is root menu
if (menuLevel == 0) {
// checks if the root menu already exists
for (int i = 0; i < menuBar.getMenuCount(); ++i) {
if (menuBar.getMenu(i).getText().equals(menuHierarchies[rootIndex][menuLevel])) {
parentMenu = menuBar.getMenu(i);
break;
}
}

if (parentMenu == null) {
parentMenu = new JMenu(menuHierarchies[rootIndex][menuLevel]);
menuBar.add(parentMenu);
}
} else if (menuLevel < menuHierarchies[rootIndex].length - 1) { // if it is a non-leaf (and, of course, not a root menu)
Component[] menuComponents = parentMenu.getMenuComponents();
JMenu currentMenu = null;

// checks if the menu already exists
for (Component component : menuComponents) {
if (component instanceof JMenu) {
if (((JMenu) component).getText().equals(menuHierarchies[rootIndex][menuLevel])) {
currentMenu = (JMenu) component;
break;
}
}
}

if (currentMenu == null) {
currentMenu = new JMenu(menuHierarchies[rootIndex][menuLevel]);
parentMenu.add(currentMenu);
}

parentMenu = currentMenu;
} else { // if it is a leaf
Component[] menuComponents = parentMenu.getMenuComponents();
JMenuItem currentItem = null;

for (Component component : menuComponents) {
if (component instanceof JMenuItem) {
if (((JMenuItem) component).getText().equals(menuHierarchies[rootIndex][menuLevel])) {
currentItem = (JMenuItem) component;
break;
}
}
}

if (currentItem == null) {
parentMenu.add(new JMenuItem(menuHierarchies[rootIndex][menuLevel]));
}
}
}
}
}

The following methods prints the menu hierarchy built, for testing purposes:

private static void printMenu(JMenuBar menuBar) {
for (int i = 0; i < menuBar.getMenuCount(); ++i) {
printMenu(menuBar.getMenu(i), 0);
}
}

private static void printMenu(JMenuItem menuItem, int level) {
for (int i = 0; i < level; ++i) {
System.out.print("\t");
}

System.out.println("\\" + menuItem.getText());

if (menuItem instanceof JMenu) {
JMenu menu = (JMenu) menuItem;

Component[] menuComponents = menu.getMenuComponents();

for (Component component : menuComponents) {
if (component instanceof JMenuItem) {
printMenu((JMenuItem) component, level+1);
}
}
}
}


Related Topics



Leave a reply



Submit