Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Java Help With Program ASAP (https://www.askmehelpdesk.com/showthread.php?t=822684)

  • Mar 11, 2016, 08:56 AM
    omgawd1234
    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?
  • Mar 11, 2016, 02:45 PM
    CravenMorhead
    Quote:

    Originally Posted by omgawd1234 View Post
    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

    I need to understand the program flow. Should you be able to buy multiple types of items, i.e. cereal and salsa?


    If that is correct then your structure should be, puesdo code because you need to write this.


    double subtotal =0;
    double price = 0;
    int amount;
    int itemCode = 0;
    double total;

    while(1);
    itemCode = in.nextInt();

    if(itemCode == 0)
    break;

    switch (itemCode){
    case 1:
    price = 3.99;
    break;
    case 2:
    ...
    default:
    print("Invalid code. 0 to exit, 1-6 for items");
    continue;
    }

    System.out.println("How much of this item would you like to buy?");
    amount = in.nextInt();

    subTotal += amount * price;
    }

    total = subTotal+ subTotal*0.08;


    The problem was the flow control through your loop. You would go through it once and only once. You needed to look for the exit conditions, and if it wasn't so when you entered 0, it was either 0 dollars or 0 amount. Either way it would be zero total.

  • All times are GMT -7. The time now is 10:52 PM.