-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTollCalculatorGUI.java
More file actions
executable file
·124 lines (98 loc) · 3.36 KB
/
TollCalculatorGUI.java
File metadata and controls
executable file
·124 lines (98 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* @(#)TollCalculatorGUI.java
*
*
* @Derick Warshaw // Lab 12 // COSC1337
* @version 1.00 2014/4/24
*/
import javax.swing.*; // GUI functionality
import java.awt.*; // GUI functionality
import java.awt.event.*; // event listeners
import java.text.DecimalFormat; // for formatting output
/**The TollCalculatorGUI class creates the GUI for the application */
public class TollCalculatorGUI extends JFrame
{
private VehicleAxlesPanel vehicle;
private TrailerAxlesPanel trailer;
private GreetingPanel banner;
private PassengersPanel passengers;
private JPanel buttonPanel;
private JButton calcButton;
private JButton exitButton;
/**TollCalculatorGUI Constructor performs setup operations */
public TollCalculatorGUI()
{
// setting the title
setTitle("Toll Calculator");
// setting action for the close button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create borderlayout manager
setLayout(new BorderLayout());
// custom panels
banner = new GreetingPanel();
vehicle = new VehicleAxlesPanel();
trailer = new TrailerAxlesPanel();
passengers = new PassengersPanel();
// button panel
buildButtonPanel();
// add components
add(banner, BorderLayout.NORTH);
add(passengers, BorderLayout.WEST);
add(vehicle, BorderLayout.CENTER);
add(trailer, BorderLayout.EAST);
add(buttonPanel, BorderLayout.SOUTH);
// for the perfect fit
pack();
// make visible
setVisible(true);
}
/**The buildButtonPanel is a private method that method builds the button
*panel
**/
private void buildButtonPanel()
{
// create a panel for the buttons
buttonPanel = new JPanel();
// create the buttons
calcButton = new JButton("Calculate");
exitButton = new JButton("Exit");
// connect the action listeners
calcButton.addActionListener(new CalcButtonListener());
exitButton.addActionListener(new ExitButtonListener());
//add the buttons to the panel
buttonPanel.add(calcButton);
buttonPanel.add(exitButton);
}
/**Private inner class that handles the event when the user clicks the
*Calculate button */
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double total; // holds total toll value
total = passengers.getPassengerCost() + vehicle.getVehicleAxleCost()
+ trailer.getTrailerAxleCost();
// creating format object to use with toll display
DecimalFormat formatter = new DecimalFormat("#0.00");
// calculate toll and present it formatted in output dialogue box
JOptionPane.showMessageDialog(null, "Thank you for using " +
"Warshaw Super Speedway Toll" +
"\n\n" + "Your toll due is $" +
formatter.format(total));
}
}
/**Private inner class that handles the event when the user clicks the exit
*button */
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
// embedded main
public static void main(String[] args)
{
new TollCalculatorGUI();
}
}