-1

Ok below is my code for a class I am taking... I need to know how to properly code the totalCost for this product and I can not for the life of me figure out how to properly do this. The price per product is $1 and I need to calculate a 7% sales tax as well. Help!!!

import javax.swing.JOptionPane;

/** * @author bary */ public class CocaCola1 {

    public static void main(String[] args) 
    {


        String openingMsg = "*** Welcome to CocaCola Online Ordering System ***\n" 
                   + "Do yourself a favor and order some Cola... NOW!";
        JOptionPane.showMessageDialog(null, openingMsg);
        System.out.println(0);

        String name = getUserInput("What is your name?");
        System.out.println(name);

        String returnCustomer = getUserInput("Are you a returning customer? (yes/no)");
        System.out.println(returnCustomer);

        String orderType = getUserInput("Would you like to purchase Dasani, Coke, Diet Coke, or Cherry Coke?");
        System.out.println(orderType);

        String orderAmount = getUserInput("How many units would you like to purchase?");
        System.out.println(orderAmount);
        Integer.parseInt(orderAmount);
        // each unit costs $1.00 so orderAmount is equal to cost per unit

        double salesTax = 0.7;


        // create and display output string
                String outputMsg
                = "Hello " +  name + ".\n\n" +
                    "Your return customer status is " + returnCustomer + ".\n" +
                    "You ordered " + orderAmount + 
                    " units of " + orderType + ".\n" +
                    "Your total cost is $" + orderAmount + salesTax + ".\n" +
                    "Thank you for visiting CocaCola" + ".\n\n" +
                    "Your order will be proccessed ASAP.\n";
                JOptionPane.showMessageDialog(null, outputMsg);
    }



    private static String getUserInput(String prompt)
    {
        int failCount = 0;
        do
        {
            String answer = JOptionPane.showInputDialog(prompt);
            if (answer == null)
            {
                System.exit(0);
            }

            answer = answer.trim();
            if (answer.isEmpty())
            {
                JOptionPane.showMessageDialog(null, "You must provide a non-blank answer");
                failCount = failCount + 1;
            } else
            {
                return answer;
            }
        } while (failCount < 3);

        JOptionPane.showMessageDialog(null, "You failed three times to provide input... Try again later!");
        System.exit(0);
        return null; // means nothing, as I just exited on the prior line, but need it to compile


} // end main()

} // end class CocaCola1

  • What problem are you having? Don't just dump your code here and say it doesn't work – Ghost Mar 03 '14 at 15:48
  • I did ask a question if you read beginning. I'm trying to calculate the totalCost correctly but am unsure of the proper code to use to do this calculation. – user3375536 Mar 03 '14 at 18:03
  • so `totalCost = Quantity * PriceEach * (1 + SalesTaxRate)`? – Ghost Mar 03 '14 at 18:10
  • figured it out thanks! I used your equation which I knew that was how i needed to calculate it but it wasnt working because i needed to use it in the following way. – user3375536 Mar 03 '14 at 19:03
  • double salesTax = 0.07; double cost = Integer.parseInt(orderAmount); double totalCost = cost * (salesTax + 1); – user3375536 Mar 03 '14 at 19:04

1 Answers1

-1

orderAmount + salesTax would only be if the sales tax was static. You need to multiply the orderAmount by the sales tax and add salestax

ex: orderAmount*salesTax+prderAmount

or set salesTax to 1.7 and it would be orderAmount*salesTax