4-10 TotalSales

posted Feb 23, 2014, 2:06 PM by Dlovan ismail   [ updated Feb 23, 2014, 2:06 PM by Karwan Jacksi ]
//This program calculates a running total.

package totalsales;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class TotalSales 
{
    public static void main(String[] args)
    {
           int days;                      // The number of days
           double sales;             // A day's sales figure
           double totalSales;  // Accumulator
           String input;               // To hold the user's input

           // Create a DecimalFormat object to format output.
           DecimalFormat dollar = new DecimalFormat("#,##0.00");

           // Get the number of days.
           input = JOptionPane.showInputDialog("For how many days do you have sales figures?");    
           days = Integer.parseInt(input);

           // Set the accumulator to 0.
           totalSales = 0.0;

           // Get the sales figures and calculate a running total.
            for (int count = 1; count <= days; count++)
            {
                input = JOptionPane.showInputDialog("Enter the sales for day " + count + ":");
                sales = Double.parseDouble(input);
                totalSales += sales; // Add sales to totalSales.
            }

            // Display the total sales.
            JOptionPane.showMessageDialog(null, "The total sales are $ " + dollar.format(totalSales));

            System.exit(0);
        
    }   
}

ċ
TotalSales.java
(1k)
Karwan Jacksi,
Feb 23, 2014, 2:06 PM
Comments