0

As you can see, the beginning of my code works perfectly, option one works fine until I get to options 2, 3, and 4. They are still 2D arrays and make my code have errors and not run.

import java.util.Scanner;

public class Menu {

    public static void main(String[] args) {
        int number = 0;
        String item;
        String item2;
        String item3;
        Double itemprice;
        double totalitems = 0;
        String itemprice2;
        String newitemname, newitemprice;
        int x;

        // array of menuitems
        MenuItem[] temp;

        MenuItem[] menuitem = new MenuItem[5];
        menuitem[0] = new MenuItem("Tacos: Steak, Chicken, Pastor, Lengua", 3.00);
        menuitem[1] = new MenuItem("Nachos: Steak, Chicken, Pastor, Lengua", 4.50);
        menuitem[2] = new MenuItem("Tortas: Steak, Chicken, Pastor, Lengua", 6.00);
        menuitem[3] = new MenuItem("Burritos: Steak, Chicken, Pastor, Lengua", 5.00);
        menuitem[4] = new MenuItem("Quesadillas: Steak, Chicken, Pastor, Lengua", 7.00);

        // welcoming the user to the restaurant and giving the menu after
        System.out.println("Welcome to Luis Restaurant!");
        System.out.println("This is the Restaurant Menu!");

        for (x = 0; x < menuitem.length; x++) {
            System.out.println(x + ")" + menuitem[x].toString());
        }
        Scanner input = new Scanner(System.in);
        int option = 1;
        String option2;
        int order;
        int k;

        // this makes a loop to continue and keep asking question until the user wants
        // to exit
        while (option != 0) {
            System.out.println(" What would you like to do ? ");
            System.out.println(" 1) Calculate the Total ");
            System.out.println(" 2) Add an item to the menu ");
            System.out.println(" 3) Delete an item from the menu ");
            System.out.println(" 4) Change the price of an item ");
            System.out.println(" 0) press 0 if you want to exit ");
            option = input.nextInt();

            // if the user wants to calculate a total
            // ask the user to allow this to calculate the total
            if (option == 1) {
                for (x = 0; x < menuitem.length; x++)
                    System.out.println(x + ")" + menuitem[x].toString());
                System.out.println(" Cacluating the Total >>> ");
                System.out.println("Select the item or type 999 for the total:");
                order = input.nextInt();
                while (order != 999) {
                    if (order < menuitem.length)
                        totalitems += menuitem[order].getPrice(); // if the user wants to enter multiple items this will make a loop for them
                    System.out.println("Select the item or type 999 for the total:"); // ask the user to enter "x" once you want the total
                    order = input.nextInt();
                }
                System.out.println("This is your total $" + totalitems);
            }

            // if the user wants to add an item:
            // ask the user to add the item name and price.
            else if (option == 2) {
                System.out.println(" Enter the name of the food item: ");
                input.nextLine();
                newitemname = input.nextLine();
                System.out.println(" Enter the price of the food item: ");
                newitemprice = input.nextLine();
                temp = new MenuItem[menuitem.length + 1];
                for (x = 0; x < menuitem.length; x++) {
                    temp[x] = menuitem[x][0];
                    temp[x][1] = menuitem[x][1];
                }
                temp[x][0] = newitemname;
                temp[x][1] = newitemprice;
                menuitem = temp;
            }

            // if the user wants to remove a item
            // ask the user what item to delete.
            else if (option == 3) {
                for (x = 0; x < menuitem.length; x++)
                    System.out.println(x + ")" + menuitem[x][0] + ":  $" + menuitem[x][1]);
                System.out.println(" What item would you like to Delete?");
                order = input.nextInt();
                temp = new MenuItem[menuitem.length - 1];
                for (x = 0, k = 0; x < menuitem.length; x++) {
                    if (x != order) {
                        temp[k].setPrice(menuitem[x].getPrice());
                        temp[k].setName(menuitem[x].getName());
                        k++;
                    }
                }
                menuitem = temp;
            }

            // If the user wants to change the price of an item:
            // ask the user what item and what the new price is
            else if (option == 4) {
                for (x = 0; x < menuitem.length; x++)
                    System.out.println(x + ")" + menuitem[x][0] + ":  $" + menuitem[x][1]);
                System.out.println(" what item would you like to Change?");
                System.out.println(" Enter the Number of the food item: ");
                order = input.nextInt();
                System.out.println(" Enter the food price of the item:  ");
                option2 = input.next();
                menuitem[order][1] = option2;
            }
        }
    }
}

These errors are located at bottom of option 2:

temp[x] = menuitem[x][0];
temp[x][1]= menuitem[x][1];

temp[x][0]=
temp[x][1] =

This error is in option 3:

System.out.println(x + ")" + menuitem[x][0] + ":  $" + menuitem[x][1]);

These errors are in option 4:

System.out.println(x + ")" +menuitem[x][0] + ":  $" + menuitem[x][1]);

menuitem[order][1] = option2;
Abra
  • 19,142
  • 7
  • 29
  • 41
  • I think you applied 2D array on a single dimension array `//array of menuitems MenuItem[] temp; MenuItem[] menuitem = new MenuItem[5];` either you create another variables or add new dimension to the existing ones – Erwin Apr 01 '23 at 05:56
  • Variable `menuitem` is **not** a two-dimensional array - it is a one-dimensional array. What value do you expect `menuitem[x][0]` to give you? Also, it may help me to answer your question if you post the code for class `MenuItem`. – Abra Apr 01 '23 at 07:15

1 Answers1

0

Explanations appear after the code.
(Changes to your code are indicated in the below code with the comment "CHANGE HERE".)

import java.util.Scanner;

public class Menu {

    public static void main(String[] args) {
        double totalitems = 0;
        String newitemname, newitemprice;
        int x;

        // array of menuitems
        MenuItem[] temp;

        MenuItem[] menuitem = new MenuItem[5];
        menuitem[0] = new MenuItem("Tacos: Steak, Chicken, Pastor, Lengua", 3.00);
        menuitem[1] = new MenuItem("Nachos: Steak, Chicken, Pastor, Lengua", 4.50);
        menuitem[2] = new MenuItem("Tortas: Steak, Chicken, Pastor, Lengua", 6.00);
        menuitem[3] = new MenuItem("Burritos: Steak, Chicken, Pastor, Lengua", 5.00);
        menuitem[4] = new MenuItem("Quesadillas: Steak, Chicken, Pastor, Lengua", 7.00);

        // welcoming the user to the restaurant and giving the menu after
        System.out.println("Welcome to Luis Restaurant!");
        System.out.println("This is the Restaurant Menu!");

        for (x = 0; x < menuitem.length; x++) {
            System.out.println(x + ")" + menuitem[x]); // CHANGE HERE
        }
        Scanner input = new Scanner(System.in);
        int option = 1;
        String option2;
        int order;
        int k;

        // this makes a loop to continue and keep asking question until the user wants
        // to exit
        while (option != 0) {
            System.out.println(" What would you like to do ? ");
            System.out.println(" 1) Calculate the Total ");
            System.out.println(" 2) Add an item to the menu ");
            System.out.println(" 3) Delete an item from the menu ");
            System.out.println(" 4) Change the price of an item ");
            System.out.println(" 0) press 0 if you want to exit ");
            option = input.nextInt();

            // if the user wants to calculate a total
            // ask the user to allow this to calculate the total
            if (option == 1) {
                for (x = 0; x < menuitem.length; x++)
                    System.out.println(x + ")" + menuitem[x].toString());
                System.out.println(" Cacluating the Total >>> ");
                System.out.println("Select the item or type 999 for the total:");
                order = input.nextInt();
                while (order != 999) {
                    if (order < menuitem.length)
                        totalitems += menuitem[order].getPrice(); // if the user wants to enter multiple items this will make a loop for them
                    System.out.println("Select the item or type 999 for the total:"); // ask the user to enter "x" once you want the total
                    order = input.nextInt();
                }
                System.out.println("This is your total $" + totalitems);
            }

            // if the user wants to add an item:
            // ask the user to add the item name and price.
            else if (option == 2) {
                System.out.println(" Enter the name of the food item: ");
                input.nextLine();
                newitemname = input.nextLine();
                System.out.println(" Enter the price of the food item: ");
                newitemprice = input.nextLine();
                temp = new MenuItem[menuitem.length + 1];
                for (x = 0; x < menuitem.length; x++) {
                    temp[x] = menuitem[x]; // CHANGE HERE
                }
                temp[x] = new MenuItem(newitemname, Double.parseDouble(newitemprice)); // CHANGE HERE
                menuitem = temp;
            }

            // if the user wants to remove a item
            // ask the user what item to delete.
            else if (option == 3) {
                for (x = 0; x < menuitem.length; x++)
                    System.out.println(x + ")" + menuitem[x]); // CHANGE HERE
                System.out.println(" What item would you like to Delete?");
                order = input.nextInt();
                temp = new MenuItem[menuitem.length - 1];
                for (x = 0, k = 0; x < menuitem.length; x++) {
                    if (x != order) {
                        temp[k] = menuitem[x]; // CHANGE HERE
                        k++;
                    }
                }
                menuitem = temp;
            }

            // If the user wants to change the price of an item:
            // ask the user what item and what the new price is
            else if (option == 4) {
                for (x = 0; x < menuitem.length; x++)
                    System.out.println(x + ")" + menuitem[x]); // CHANGE HERE
                System.out.println(" what item would you like to Change?");
                System.out.println(" Enter the Number of the food item: ");
                order = input.nextInt();
                input.nextLine();
                System.out.println(" Enter the food price of the item:  ");
                option2 = input.nextLine();
                menuitem[order].setPrice(Double.parseDouble(option2));
            }
        }
    }
}

class MenuItem {
    private String name;
    private double price;

    public MenuItem(String desc, double cost) {
        name = desc;
        price = cost;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double newPrice) {
        price = newPrice;
    }

    @Override // java.lang.Object
    public String toString() {
        return String.format("%s: $%.2f", name, price);
    }
}

Each element in array menuitem is an instance of class MenuItem and menuitem[x][0] does not give you an attribute of the class. You need to invoke a class method that returns the desired attribute – and that appears in your code:

totalitems += menuitem[order].getPrice(); // if the user wants to enter multiple items this will make a loop for them

Hence, in the above code, I replaced menuitem[x][0] with menuitem[x].getName() and menuitem[x][1] with menuitem[x].getPrice().

If you want to create a new MenuItem instance, you need to call the constructor of class MenuItem. Hence I replaced:

temp[x][0] = newitemname;
temp[x][1] = newitemprice;

with:

temp[x] = new MenuItem(newitemname, Double.parseDouble(newitemprice));

When copying elements from array menuitem to array temp, you just need a simple assignment – you don't need to assign each attribute separately. Hence I replaced:

temp[x] = menuitem[x][0];
temp[x][1] = menuitem[x][1];

with:

temp[x] = menuitem[x];

Note that the following code creates a new array but every element in that array is null:

temp = new MenuItem[menuitem.length + 1];

However you can combine creating a new array and copying elements by calling method copyOf in class java.util.Arrays:

temp = Arrays.copyOf(menuitem, menuitem.length + 1);

When printing a MenuItem instance, it is preferable to override method toString. This also appears in your code:

System.out.println(x + ")" + menuitem[x].toString());

Note that you can remove the .toString().

You need to be careful when calling different next* methods of class java.util.Scanner. In your code you are calling methods next and nextInt and nextLine. Refer to Scanner is skipping nextLine() after using next() or nextFoo()?.

I have not done so in the above code, but you should check whether the user enters valid values. What happens if she enters option 7, for example?

Output from a sample run of the above code:

Welcome to Luis Restaurant!
This is the Restaurant Menu!
0)Tacos: Steak, Chicken, Pastor, Lengua: $3.00
1)Nachos: Steak, Chicken, Pastor, Lengua: $4.50
2)Tortas: Steak, Chicken, Pastor, Lengua: $6.00
3)Burritos: Steak, Chicken, Pastor, Lengua: $5.00
4)Quesadillas: Steak, Chicken, Pastor, Lengua: $7.00
 What would you like to do ? 
 1) Calculate the Total 
 2) Add an item to the menu 
 3) Delete an item from the menu 
 4) Change the price of an item 
 0) press 0 if you want to exit 
1
0)Tacos: Steak, Chicken, Pastor, Lengua: $3.00
1)Nachos: Steak, Chicken, Pastor, Lengua: $4.50
2)Tortas: Steak, Chicken, Pastor, Lengua: $6.00
3)Burritos: Steak, Chicken, Pastor, Lengua: $5.00
4)Quesadillas: Steak, Chicken, Pastor, Lengua: $7.00
 Cacluating the Total >>> 
Select the item or type 999 for the total:
999
This is your total $0.0
 What would you like to do ? 
 1) Calculate the Total 
 2) Add an item to the menu 
 3) Delete an item from the menu 
 4) Change the price of an item 
 0) press 0 if you want to exit 
2
 Enter the name of the food item: 
George
 Enter the price of the food item: 
25
 What would you like to do ? 
 1) Calculate the Total 
 2) Add an item to the menu 
 3) Delete an item from the menu 
 4) Change the price of an item 
 0) press 0 if you want to exit 
4
0)Tacos: Steak, Chicken, Pastor, Lengua: $3.00
1)Nachos: Steak, Chicken, Pastor, Lengua: $4.50
2)Tortas: Steak, Chicken, Pastor, Lengua: $6.00
3)Burritos: Steak, Chicken, Pastor, Lengua: $5.00
4)Quesadillas: Steak, Chicken, Pastor, Lengua: $7.00
5)George: $25.00
 what item would you like to Change?
 Enter the Number of the food item: 
0
 Enter the food price of the item:  
3.25
 What would you like to do ? 
 1) Calculate the Total 
 2) Add an item to the menu 
 3) Delete an item from the menu 
 4) Change the price of an item 
 0) press 0 if you want to exit 
3
0)Tacos: Steak, Chicken, Pastor, Lengua: $3.25
1)Nachos: Steak, Chicken, Pastor, Lengua: $4.50
2)Tortas: Steak, Chicken, Pastor, Lengua: $6.00
3)Burritos: Steak, Chicken, Pastor, Lengua: $5.00
4)Quesadillas: Steak, Chicken, Pastor, Lengua: $7.00
5)George: $25.00
 What item would you like to Delete?
5
 What would you like to do ? 
 1) Calculate the Total 
 2) Add an item to the menu 
 3) Delete an item from the menu 
 4) Change the price of an item 
 0) press 0 if you want to exit 
1
0)Tacos: Steak, Chicken, Pastor, Lengua: $3.25
1)Nachos: Steak, Chicken, Pastor, Lengua: $4.50
2)Tortas: Steak, Chicken, Pastor, Lengua: $6.00
3)Burritos: Steak, Chicken, Pastor, Lengua: $5.00
4)Quesadillas: Steak, Chicken, Pastor, Lengua: $7.00
 Cacluating the Total >>> 
Select the item or type 999 for the total:
0
Select the item or type 999 for the total:
0
Select the item or type 999 for the total:
1
Select the item or type 999 for the total:
999
This is your total $11.0
 What would you like to do ? 
 1) Calculate the Total 
 2) Add an item to the menu 
 3) Delete an item from the menu 
 4) Change the price of an item 
 0) press 0 if you want to exit 
0
Abra
  • 19,142
  • 7
  • 29
  • 41