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;