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);

     }
    }

Dani AI

Generated

A simple, maintainable approach is to compute the line total for each product/quantity pair, add that to a running grand total and to the per-product accumulator, then show the formatted running subtotal after each pair. The posted code by works but uses five separate variables and a switch; using arrays makes the logic clearer and reduces duplication.

import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JOptionPane;

double[] price = {0.0, 2.98, 4.50, 9.98, 4.49, 6.87}; // index 1..5
double[] totals = new double[price.length];
double grandTotal = 0.0;
NumberFormat cf = NumberFormat.getCurrencyInstance(Locale.US);

while (true) {
    String sid = JOptionPane.showInputDialog("Product number (0 to stop):");
    if (sid == null) break;
    int id = Integer.parseInt(sid);
    if (id == 0) break;
    if (id < 1 || id >= price.length) { JOptionPane.showMessageDialog(null, "Enter 1-5 or 0"); continue; }

    String sqty = JOptionPane.showInputDialog("Quantity:");
    if (sqty == null) break;
    int qty = Integer.parseInt(sqty);

    double lineTotal = price[id] * qty;       // subtotal for this pair
    totals[id] += lineTotal;                  // per-product running total
    grandTotal += lineTotal;                  // running grand total

    JOptionPane.showMessageDialog(null,
        "Line total: " + cf.format(lineTotal) + "\nRunning subtotal: " + cf.format(grandTotal));
}

Notes and troubleshooting: validate inputs with try/catch for NumberFormatException and handle null (cancel). Use double for simplicity here, but prefer BigDecimal for real money to avoid rounding errors. To show final results, build a JTextArea listing each product total from totals[] and the grandTotal. As asked: the "subtotal after each pair" is the single entry's line total (or the running grand total if that is what's desired), and the grand total is the sum of all line totals (equivalently, the sum of per-product totals).

Recommended Answers

All 3 Replies

Is there a question or problem?

Or are you just sharing some code?

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.