I am having trouble using values from a switch statement in a separate method that I have below the main method. How would I get it so I can use the values I am trying to return from the switch statement? Here is my code:
private static int itemNo;
private static double userQuantity;
private static double oneTotal, twoTotal, threeTotal, fourTotal, fiveTotal;
private static double result;
public static void main(String[]args){
double totalAmount = 0;
Scanner input = new Scanner(System.in);
double tA = calculateTotal(itemNo);
totalAmount += result;
while(true){
System.out.print("Please enter the item number you wish to use, or enter -1 to stop: ");
int itemNo = input.nextInt();
if(itemNo == -1){
System.out.println("The total value of the purchase is: " + totalAmount);
break;
}
System.out.println("Now please input the quantity of the item you specified above: ");
double userQuantity = input.nextInt();
}
}
public static double calculateTotal(int itemNo){
double result = 0;
switch(itemNo){
case 1:
double oneTotal = 2.98 * userQuantity;
break;
case 2:
double twoTotal = 4.50 * userQuantity;
break;
case 3:
double threeTotal = 9.98 * userQuantity;
break;
case 4:
double fourTotal = 4.49 * userQuantity;
break;
case 5:
double fiveTotal = 6.87 * userQuantity;
break;
}
result = oneTotal + twoTotal + threeTotal + fourTotal + fiveTotal;
return result;
}
I am trying to get it so it allows the user to input what item number they would like to use, and then the quantity of that item they bought, and then return the value that is calculated in the switch statement to be used in result, which is the printed out once they type "-1" to stop the program. However totalAmount is staying at zero, and is not taking the returned result value.