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