import java.awt.*;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.*;

public class SalesFigures

{   
   public static void main (String[]args)

    {
       float prod1 = 0, prod2 = 0, prod3 = 0;
        float prod4 = 0, prod5 = 0;

       String inputString;
        int prodId = 1;

        // Need user input to determine outcome

        while (prodId != 0) // While loop
        {
          inputString = JOptionPane.showInputDialog(
          "Enter product number (Enter 0 to stop):");
          prodId = Integer.parseInt(inputString);

          if (prodId >= 1 && prodId <= 5) // Determine total items sold
          {
           inputString = JOptionPane.showInputDialog(
            "Enter number of items sold:");
            int quantity = Integer.parseInt (inputString);

            switch (prodId)           // Switch used to add price * number of items sold
            {
             case 1:
                prod1 += quantity * 2.98;
                 break;

             case 2:
                prod2 += quantity * 4.50;
                 break;

             case 3:
                prod3 += quantity * 9.98;
                 break;

             case 4:
                prod4 += quantity * 4.49;
                 break;

             case 5:
                prod5 += quantity * 6.87;
                 break;
            }
          }

          else if ( prodId != 0) 
          {
             JOptionPane.showMessageDialog(null,
              "Product number must be 1-5 or 0 to stop",
              "Input Error", JOptionPane.INFORMATION_MESSAGE);
          }
         }

        NumberFormat moneyFormat = NumberFormat.getCurrencyInstance (Locale.US);

        String output = "Product 1:" + moneyFormat.format (prod1);
        output += "\nProduct 2:" + moneyFormat.format (prod2);
        output += "\nProduct 3:" + moneyFormat.format (prod3);
        output += "\nProduct 4:" + moneyFormat.format (prod4);
        output += "\nProduct 5:" + moneyFormat.format (prod5)+ "\n";

        JTextArea outputArea = new JTextArea (6, 20);
        outputArea.setText ( output);

        JOptionPane.showMessageDialog (null, outputArea,
        "Totals" , JOptionPane.INFORMATION_MESSAGE);

        System.exit (0);

     }
    }

I'm sorry I am asking how would I go about figuring out how to calculate and display the subtotal after each pair of inputs and total cost of all of the products...I have been looking in my textbook and online and can only get this far...Any help that can push me in the right direction is greatly appricated

Where in the code are the values that you need to add up to get the subtotals?
Can you define the input that is used to compute a subtotal?
Then given the subtotals how would you compute the total cost?

Can you explain in detail what is wrong with the posted code?

Edited 4 Years Ago by NormR1

This article has been dead for over six months. Start a new discussion instead.