Java Help With Program ASAP
Hi, so I have a project that requires user to enter numbers to purchase items. At the end when 0 is entered, the program should stop and calculate tax subtotal and total. I must use DecimalFormat to print out the numbers and it works. Problem is when I make it to calculate everything at the end and put it in while or if statements and when I enter 0 it puts out 0 as total subtotal and total. On the other side, when I don't use if or while statements, it just print out everything right but doesn't let me continue and just stops
Here is the output without if or while:
Welcome to our store: Health Foods. We offer variety of different foods to buy
Through our online store, you can purchase the follwing products:
1) One box of Cinnamon Cereal 3.99
2) One box of sugar free Oreos 2.99
3) Two bags of cron ships 4.99
4) Two jars of spicy salsa 6.39
5) One dozen of donuts 2.39
6) One frozen pizza 1.99
Please enter 'shop' to enter your prducts and enter '0' to checkout:
shop
Enter a number corresponding to an item you want to purchase:
1
How much of this item would you like to buy?
2
Subtotal: $7.98
Tax: $0.64
Total: $8.62
Here is a program for it:
import java.util.Scanner;
import java.text.DecimalFormat;
public class ShoppingProgram {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner (System.in);
double total=0, subtotal=0, tax=0, item = 0;
int count = 1;
System.out.println("Welcome to our store: Health Foods. We offer variety of different foods to buy");
System.out.println("Through our online store, you can purchase the follwing products: ");
System.out.println();
System.out.println("1) One box of Cinnamon Cereal 3.99");
System.out.println("2) One box of sugar free Oreos 2.99");
System.out.println("3) Two bags of cron ships 4.99");
System.out.println("4) Two jars of spicy salsa 6.39");
System.out.println("5) One dozen of donuts 2.39");
System.out.println("6) One frozen pizza 1.99");
System.out.println();
System.out.println("Please enter 'shop' to enter your prducts and enter '0' to checkout:");
String begin = in.next();
while (begin.equals ("shop")){
System.out.println("Enter a number corresponding to an item you want to purchase:");
int ci = in.nextInt();
if (ci > 6 || ci>1){
item = in.nextInt();
}
else if (ci==1){
item = 3.99;
}
else if (ci==2){
item = 2.99;
}
else if (ci==3){
item = 4.99;
}
else if (ci==4){
item = 6.39;
}
else if (ci==5){
item = 2.39;
}
else if (ci==6){
item = 1.99;
}
else {
System.out.println("Enter a number corresponding to an item you want to pruchase");
}
System.out.println("How much of this item would you like to buy?");
int amount = in.nextInt();
{
subtotal = item*amount;
tax = subtotal*.08;
total = tax + subtotal;
String pattern = "$##,##.##";
DecimalFormat df= new DecimalFormat(pattern);
System.out.println("Subtotal: " + df.format(subtotal));
System.out.println("Tax: " + df.format(tax));
System.out.println("Total: " + df.format(total));
break;
}
}
}
}
This is the part I have a problem with:
subtotal = item*amount;
tax = subtotal*.08;
total = tax + subtotal;
String pattern = "$##,##.##";
DecimalFormat df= new DecimalFormat(pattern);
System.out.println("Subtotal: " + df.format(subtotal));
System.out.println("Tax: " + df.format(tax));
System.out.println("Total: " + df.format(total));
I need the program to ask user enter item number and then quantity until the user enters 0 for item. Cannot seem to do that. Any suggestions?