I have a very simple applet to write and I have most of it done. However, I am confused about one easy function that the program is suppose to perform. I would appreciate any help. The problem I am stuck on is how to calculate the running total, hence totalReceipts. I know it is a position thing and normally would use a loop to do this. However, with an applet, I am a little confused. Here is the code I have:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class A6_8R extends JApplet implements ActionListener {
JLabel promptLabel;
JTextField inputField;
public void init()
{
Container container = getContentPane();
container.setLayout( new FlowLayout() );
promptLabel = new JLabel( "Enter number of hours: ");
inputField = new JTextField( 5 );
inputField.addActionListener( this );
container.add(promptLabel);
container.add(inputField);
}
public void actionPerformed( ActionEvent actionEvent)
{
double totalReceipts = 0.00;
double hours = Double.parseDouble(actionEvent.getActionCommand() );
showStatus( "Current charge: " + calculateCharges( hours ) + "; " + "Total Receipts: " +
totalReceipts );
}
public double calculateCharges( double hours )
{
final double minimumCharge = 2.00;
final double maximumCharge = 10.00;
double finalCharge = 0.00;
if ( hours <= 3.0 )
{
finalCharge = minimumCharge;
}
else if ( hours > 3.0 && hours < 24.0)
{
finalCharge = minimumCharge + (0.50 * (hours - 3.0));
}
else if ( hours == 24.0)
{
finalCharge = maximumCharge;
}
return finalCharge;
}
}