Running Total in Array

I

IanH

Hi,
I need to include a running total in the following array. I have
attempted it but I went wrong somewhere but not sure where. Any help
would be appreciated.
Ian
************************************************************************************




//Java Packages
import java.awt.Container;
import javax.swing.*;
public class test extends JApplet {
// initialize applet by obtaining values from user and storing them
in an array
public void init()
{
String name;
String address;
String creditCardNumber;
String hours;
int noOfHours; // noOfHours used to convert string hours to
integer
double charges; // Used to pass the charge calcualted in the
method calculateCharges
double runningTotal;
String customerArray[]; //declared array
customerArray = new String[ 20 ]; //create array
JTextArea outputArea = new JTextArea(); // create JTextArea to
display outputArea
Container container = getContentPane(); // creates the interface
container.add( outputArea ); // outputArea is placed on the
interface
// set the first line of text in the outputArea
outputArea.append( "Customer Charges\n" + "\n" +

Name\tAddress\tCreditCardNumber\tHours\tFee\n");
// initialise integer to increment each record to the array
int index = 0;
double total = 0;
do {
// get user input to be stored in the array
name = JOptionPane.showInputDialog("Enter your name");

// check if name is null to break out of the loop
if (name.equals(""))
break;
address = JOptionPane.showInputDialog("Enter your
address");
creditCardNumber = JOptionPane.showInputDialog("Enter

your credit card number");
hours = JOptionPane.showInputDialog("Enter total
hours");
// convert numberOfHours to NoOfHours
noOfHours = Integer.parseInt( hours );
// call calculateCharges to calculate the fee
charges = calculateCharges(noOfHours);
runningTotal = sum(charges[customerArray]);
//fill array and position the records
customerArray[index] = name + "\t" + "\t" + address

+"\t" + creditCardNumber + "\t"
+ "\t" + hours + "\t" + charges +
"\n" + runningTotal;
//secondarray[total] = charges;
// add record to the array
index++;
// loop while name is not empty
} while (!name.equals(""));
// loop through the array elements and output the records
for (int counter=0; counter<customerArray.length;
counter++)
// append the outputArea to include the elements in the
array
outputArea.append(customerArray[counter]);
//outputArea.append("running total" + total);
} // end method init
// check calcualtion again
// method to calculate the charges due
public double calculateCharges(int hoursParked) {
// initialise variables
double fee = 0.00;
int NoOfHours = 0;
if ( hoursParked >=24 )
fee = 10.00;
else if ( hoursParked > 3)
fee = (hoursParked - 3 ) * .5 + 2.00;
else if ( hoursParked <= 3)
fee = 2.00;
return fee;
}// end method calculateCharges
//int arraylength = <numbers>.length;
public double sum(double [] numbers)
{
double total =0;
for (int i =0; i<numbers.length; i++) {
total += numbers;
}
return total;
}
 
R

Rhino

IanH said:
Hi,
I need to include a running total in the following array. I have
attempted it but I went wrong somewhere but not sure where. Any help
would be appreciated.
I thought we'd solved this problem.... Looks like we're starting the next
phase of the project where the user (teacher) tells you what he _really_
wanted all along :)


************************************************************************************




//Java Packages
import java.awt.Container;
import javax.swing.*;
public class test extends JApplet {
// initialize applet by obtaining values from user and storing them
in an array
public void init()
{
String name;
String address;
String creditCardNumber;
String hours;
int noOfHours; // noOfHours used to convert string hours to
integer
double charges; // Used to pass the charge calcualted in the
method calculateCharges
double runningTotal;
String customerArray[]; //declared array
customerArray = new String[ 20 ]; //create array
JTextArea outputArea = new JTextArea(); // create JTextArea to
display outputArea
Container container = getContentPane(); // creates the interface
container.add( outputArea ); // outputArea is placed on the
interface
// set the first line of text in the outputArea
outputArea.append( "Customer Charges\n" + "\n" +

Name\tAddress\tCreditCardNumber\tHours\tFee\n");
// initialise integer to increment each record to the array
int index = 0;
double total = 0;
do {
// get user input to be stored in the array
name = JOptionPane.showInputDialog("Enter your name");

// check if name is null to break out of the loop
if (name.equals(""))
break;
address = JOptionPane.showInputDialog("Enter your
address");
creditCardNumber = JOptionPane.showInputDialog("Enter

your credit card number");
hours = JOptionPane.showInputDialog("Enter total
hours");
// convert numberOfHours to NoOfHours
noOfHours = Integer.parseInt( hours );
// call calculateCharges to calculate the fee
charges = calculateCharges(noOfHours);
runningTotal = sum(charges[customerArray]);
//fill array and position the records
customerArray[index] = name + "\t" + "\t" + address

+"\t" + creditCardNumber + "\t"
+ "\t" + hours + "\t" + charges +
"\n" + runningTotal;
//secondarray[total] = charges;
// add record to the array
index++;
// loop while name is not empty
} while (!name.equals(""));
// loop through the array elements and output the records
for (int counter=0; counter<customerArray.length;
counter++)
// append the outputArea to include the elements in the
array
outputArea.append(customerArray[counter]);
//outputArea.append("running total" + total);
} // end method init
// check calcualtion again
// method to calculate the charges due
public double calculateCharges(int hoursParked) {
// initialise variables
double fee = 0.00;
int NoOfHours = 0;
if ( hoursParked >=24 )
fee = 10.00;
else if ( hoursParked > 3)
fee = (hoursParked - 3 ) * .5 + 2.00;
else if ( hoursParked <= 3)
fee = 2.00;
return fee;
}// end method calculateCharges
//int arraylength = <numbers>.length;
public double sum(double [] numbers)
{
double total =0;
for (int i =0; i<numbers.length; i++) {
total += numbers;
}
return total;
}


Logically, all you really need to do to get your running total (and display
it on each line of your report) is:
- create a new variable with a name like 'runningTotal', exactly as you've
done
- initialize the new variable to zero before you start the loop, just to
make sure the initial total is zero
- add the value of the 'charges' value to the new variable each time you go
through the loop
- display the value of the new variable just the way you handled the
'charges' variable

The statement to add the value of the 'charges' variable to the
'runningTotal' variable is:

runningTotal = runningTotal + charges;

Or, you can use this Java shorthand to get the same result:

runningTotal += charges;

You really don't need your 'sum' method at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top