a parking garage charges a ksh60.00 minimum fee to park for up to three hours. the garage charges an additional ksh.15.00 per hour for each hour or part thereof in excess of three hours. the maximum charge for any given 24-hour period is ksh.375.00. assume that no car parks for longer than 24 hours at a time. write an application that calculates and displays the parking charges for each customer who parked a car in this garage yesterday. you should enter in a jtextfield the hours parked for each customer. the program should display the charge for the current customer and should calculate and display the running total of yesterday’s receipts. the program should use the method calculate charges to determine the charge for each customer
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Main {
private static double total = 0;
public static double calculateCharges(double hours) {
double charge;
if (hours <= 3) {
charge = 60;
} else if (hours > 18) {
charge = 375;
} else {
charge = 60;
if (hours - (int) hours > 0) {
charge += 15;
}
charge += ((int) hours - 3) * 15;
}
return charge;
}
public static void main(String[] args) {
JTextField field = new JTextField();
field.setMaximumSize(new Dimension(80,25));
field.setAlignmentX(.5f);
JLabel curLabel = new JLabel("Current customer:");
curLabel.setAlignmentX(0.5f);
JLabel totalLabel = new JLabel("Total: ");
totalLabel.setAlignmentX(.5f);
JButton button = new JButton("Add");
button.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double hours = Double.parseDouble(field.getText());
double charge = calculateCharges(hours);
total += charge;
totalLabel.setText("Total: " + total + "khs");
curLabel.setText("Current customer: " + charge + "khs");
} catch (Exception ex) {
}
}
});
button.setAlignmentX(.5f);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(field);
panel.add(curLabel);
panel.add(totalLabel);
panel.add(button);
JFrame frame = new JFrame();
frame.setSize(200, 150);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel);
frame.setResizable(false);
frame.setVisible(true);
}
}
Comments
Leave a comment