Hi all,
I am in my first Java class at the local community college but my class is online and my teacher takes DAYS to respond via email. I need some help with my homework and thought I would try here. I'm not looking for someone to give me the answers, but push me in the right direction.
So the code I have written contains a Scanner. The Scanner does not have a problem accepting the first variable passed to it, but when I put the second variable into it, the program abruptly ends.
There is a menu of 3 items, each contains it's own item number, item name, and item price. The user is shown the menu and asked to choose an item. The menu is presented again for the user to choose another item. Once three items have been selected the user enters a 0 to get his total bill.
I have not learned loops yet, as that is the next chapter so I cannot use them for this assignment. What I really need to know is why can my Scanner not take multiple items and how can I work with it?
Thanks in advance, here's my code:
/*display menu of 3 items in a restaurant
(1) Cheeseburger 4.99
(2) Pepsi 2.00
(3) Chips 0.75
prompt user to choose item (1,2,or 3) or 0 to quit application.
if first choice is 0, display $0., otherwise display menu again.
if user enters 0, display cost of 1 item. If user types 1,2, or 3
add cost to original item.
*/
import java.util.Scanner;
public class FastFood_fix
{
private static double foodTotal = 0.0;
private static int selection, selection2, selection3;
private final static double CBURGER_PRICE = 4.99;
private final static double PEPSI_PRICE = 2.00;
private final static double CHIPS_PRICE = 0.75;
private final static String CBURGER_NAME = "Cheeseburger";
private final static String PEPSI_NAME = "Pepsi";
private final static String CHIPS_NAME = "Chips";
private final static int CBURGER_ITEM = 1;
private final static int PEPSI_ITEM = 2;
private final static int CHIPS_ITEM = 3;
private final static int REQUEST_TOTAL = 0;
public static void main(String[] args)
{
menu();
chooseYourItem();
chosenItem(selection, foodTotal);
}
public static void menu()
{
System.out.println(" FAST FOOD MENU\n(" + CBURGER_ITEM + ") " + CBURGER_NAME +
" " + CBURGER_PRICE + "\n(" + PEPSI_ITEM + ") " + PEPSI_NAME + " " +
PEPSI_PRICE + "0\n(" + CHIPS_ITEM + ") " + CHIPS_NAME + " " + CHIPS_PRICE);
}
public static void chooseYourItem()
{
Scanner input = new Scanner(System.in);
System.out.println("\nPlease choose an item from the menu above:" + "\n(Enter " +
CBURGER_ITEM + " for a " + CBURGER_NAME + ", " + PEPSI_ITEM + " for a " +
PEPSI_NAME + ", or " + CHIPS_ITEM + " for " + CHIPS_NAME + ")" +
"\nTo receive your total, please enter a " + REQUEST_TOTAL);
selection = input.nextInt();
}
public static void chosenItem(int selection, double total)
{
switch(selection)
{
case CBURGER_ITEM:
foodTotal = total + CBURGER_PRICE;
System.out.println(selection + " " + total + " " + foodTotal);
menu();
chooseYourItem();
break;
case PEPSI_ITEM:
foodTotal = total + PEPSI_PRICE;
System.out.println(selection + " " + total + " " + foodTotal);
menu();
chooseYourItem();
break;
case CHIPS_ITEM:
foodTotal = total + CHIPS_PRICE;
System.out.println(selection + " " + total + " " + foodTotal);
menu();
chooseYourItem();
break;
case REQUEST_TOTAL:
System.out.println("Your total comes to: $" + foodTotal + "0");
break;
default:
System.out.println("You have entered " + selection + ", an invalid code, please try again.");
}
}
}