Ask Experts Questions for FREE Help !
Ask
    omgawd1234's Avatar
    omgawd1234 Posts: 1, Reputation: 1
    New Member
     
    #1

    Mar 11, 2016, 08:56 AM
    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?
    CravenMorhead's Avatar
    CravenMorhead Posts: 4,532, Reputation: 1065
    Adult Sexuality Expert
     
    #2

    Mar 11, 2016, 02:45 PM
    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.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Java Program [ 0 Answers ]

I Cannot calculate the average per day of person I want my code like this: Student Daily spending Name Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Average per person Joe Smith ...

Java program help!! [ 1 Answers ]

Dr.Java method and overloaded methods help in code? Write a program that uses two overloaded methods to calculate the monthly payment for a loan. The formula for calculating a payment is shown further down. Given that the user enters: - an annual interest rate such as 3.600%, which is the...

Java netbeans program- Connection between java and mysql,How to conn [ 0 Answers ]

which I wrote from a book but when I run it, it shows exception please give me the code to connect jlistbox to database when I click on btn1. btn coding should search "name" in database and shows all the names in listbox my coding: here-- r1,r2,r3 are radio btns and l1,l2,l3 are labels,...

Java Program [ 2 Answers ]

1) Code a java class for an Object SONG, that has as a minimum the following attributes:- SONG Title (a String) , SONG Group/artist (a String ), SONG genre (a String – “Pop”, “Jazz”, “Hard Rock” etc. SONG price (double), SONG duration (int number of seconds). Code all accessor and mutator methods...

Java program? [ 2 Answers ]

we have an assignment! but hold up! I would only ask what is the name of this code if I have fill up the blanks! code: puclic class problim{ public static void main(Stringarg){ int x=___; int y=___; while;(__<s){ x=____;


View more questions Search