5 Replies - 4194 Views - Last Post: 23 January 2011 - 03:33 PM Rate Topic: -----

#1 Stewybaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-August 10

Running total -- Java

Posted 23 January 2011 - 02:36 PM

I need help creating a running total with each iteration. I have no idea how to create this. I have tried and failed. This is my code, and I have placed tags where the counter should go. Any help would be great.

/**
 * @(#)StewartBaxterP3.java
 *
 * StewartBaxterP3 application
 *
 * @author
 * @version 1.00 2011/1/20
 */

import javax.swing.*;


public class StewartBaxterP3
{
    public static void main(String[] args)
    	{
    		final int NUMBER_OF_ITEMS = 5;
    		int[] productNum = {1, 2, 3, 4, 5};
    		double[] prices = {2.98, 4.50, 9.98, 4.49, 6.87};
    		String strItem;
    		int itemOrdered;
    		double itemPrice = 0.0;
    		boolean validItem = false;
    		strItem = JOptionPane.showInputDialog(null, "Enter Product Number (1-5) or any other number to Quit:");
    		itemOrdered = Integer.parseInt(strItem);
			while(itemOrdered <= NUMBER_OF_ITEMS)
			{
    			for(int x = 0; x < NUMBER_OF_ITEMS; ++x)
    			{
    		   		if(itemOrdered == productNum[x])
    		   		{
    		   			validItem = true;
    		   			itemPrice = prices[x];
    		   		}
    			}
    			if(validItem)
    			{
					int quantity;
    				strItem = JOptionPane.showInputDialog(null, "Enter Quantity: ");
    				quantity = Integer.parseInt(strItem);
    				double total;

    				total = quantity * itemPrice;
    				while(itemOrdered <= NUMBER_OF_ITEMS)
    				{
    					/*Place running total method here.
						*/
    				}

    				JOptionPane.showMessageDialog(null, "Product Number: " + itemOrdered + "\nProduct quantity: "
    					+ quantity + "\nPrice Per Unit: $" + itemPrice + "\nTotal: $" + total);
    					/*Display running total here.*/
    				strItem = JOptionPane.showInputDialog(null, "Enter Product Number (1-5) or any other number to Quit:");
    				itemOrdered = Integer.parseInt(strItem);
    			}
    			else
    				JOptionPane.showMessageDialog(null, "Now Quitting");
    		}
    		JOptionPane.showMessageDialog(null, "Now Quitting");
    	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Running total -- Java

#2 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Running total -- Java

Posted 23 January 2011 - 02:47 PM

By creating a variable outside of the loop and modifying it accordingly within the loop.
Was This Post Helpful? 0
  • +
  • -

#3 Stewybaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-August 10

Re: Running total -- Java

Posted 23 January 2011 - 02:58 PM

View Postmasijade, on 23 January 2011 - 02:47 PM, said:

By creating a variable outside of the loop and modifying it accordingly within the loop.


OK, that make more sense, but how would I initialize each total? Would I have to store each total into a string? Not to sure how to go about this.
Was This Post Helpful? 0
  • +
  • -

#4 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Running total -- Java

Posted 23 January 2011 - 03:01 PM

The "total" is a number, is it not?
Was This Post Helpful? 0
  • +
  • -

#5 Stewybaby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-August 10

Re: Running total -- Java

Posted 23 January 2011 - 03:08 PM

View Postmasijade, on 23 January 2011 - 03:01 PM, said:

The "total" is a number, is it not?



Yes it is. So could I use something like this?

{
double subTotal;
subTotal = 0.0;

runTotal = subTotal + total;

}
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10437
  • View blog
  • Posts: 38,648
  • Joined: 27-December 08

Re: Running total -- Java

Posted 23 January 2011 - 03:33 PM

You need to declare total outside of your loop, like masijade said.
double total = 0.0;
for(/*code*/){
   //modify total here
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1