Modify the application you developed to keep track of multiple table bills at the same time. Sample outputs are shown in Fig. 26.44. The user should be able to calculate a bill for a table and save that table’s subtotal and waiter’s name. The user should also be able to retrieve that informa- tion at a later time. [Hint: The restaurant2 database contains two tables, one for the menu items, as before, and another (restaurantTables) for all the tables in the restaurant. The restaurantTables table (Fig. 26.45) has three columns—tableNumber, subtotal and waiterName. The values in the tableNumber column are ints. The values in the subtotal column are doubles. The values in the waiterName column are Strings.]







a) Copying the template to your working directory. Copy the C:Examples Tutorial26ExercisesRestaurantBillCalculatorEnhanced directory to your C:SimplyJava directory.


b) Copying the database to your working directory. Copy the restaurant2 database directory from C:ExamplesTutorial26ExercisesDatabases to your C:Sim- plyJavaRestaurantBillCalculatorEnhanced directory.


c) Opening the template file. Open the RestaurantBillCalculator.java file in your text editor.


d) Adding code to the loadTableNumbers method. Find the loadTableNumbers method, which immediately follows createMenuItemsJPanel. In the loadTable- Numbers method, add a statement that queries the database and retrieves the table- Number column from the restaurantTables table. Insert a loop that processes the ResultSet and adds each table number to the tableNumbe

```
1 // RestaurantBillCalculator.java
2 // Calculates a table's bill.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.sql.*;
6 import java.text.*;
7 import java.util.*;
8 import javax.swing.*;
9
10 public class RestaurantBillCalculator extends JFrame
11 {
12 // JLabel for Restaurant
13 private JLabel restaurantJLabel;
14
15 // JPanel for Waiter Information
16 private JPanel waiterJPanel;
17
18 // JLabel and JComboBox for Table Number
19 private JLabel tableNumberJLabel;
20 private JComboBox tableNumberJComboBox;
21
22 // JLabel and JTextField for Waiter Name
23 private JLabel waiterNameJLabel;
24 private JTextField waiterNameJTextField;
25
26 // JPanel for Menu Items
27 private JPanel menuItemsJPanel;
28
29 // JLabel and JComboBox for Beverage
30 private JLabel beverageJLabel;
31 private JComboBox beverageJComboBox;
32
33 // JLabel and JComboBox for appetizer
34 private JLabel appetizerJLabel;
35 private JComboBox appetizerJComboBox;
36
37 // JLabel and JComboBox for Main Course
38 private JLabel mainCourseJLabel;
39 private JComboBox mainCourseJComboBox;
40
41 // JLabel and JComboBox for Dessert
42 private JLabel dessertJLabel;
43 private JComboBox dessertJComboBox;
44
45 // JLabel and JTextField for Subtotal
46 private JLabel subtotalJLabel;
47 private JTextField subtotalJTextField;
48
49 // JLabel and JTextField for Tax
50 private JLabel taxJLabel;
51 private JTextField taxJTextField;
52
53 // JLabel and JTextField for Total
54 private JLabel totalJLabel;
55 private JTextField totalJTextField;
56
57 // JButton for Save Table
58 private JButton saveTableJButton;
59
60 // JButton for Calculate Bill
61 private JButton calculateBillJButton;
62
63 // JButton for Pay Bill
64 private JButton payBillJButton;
65
66 // constant for tax rate
67 private final static double TAX_RATE = 0.05;
68
69 // declare instance variables for database processing
70 private Connection myConnection;
71 private Statement myStatement;
72 private ResultSet myResultSet;
73
74 // other instance variables
75 private ArrayList billItems = new ArrayList();
76 private double subtotal;
77
78 // constructor
79 public RestaurantBillCalculator(
80 String databaseDriver, String databaseURL )
81 {
82 // make database connection
83 try
84 {
85 // load Cloudscape driver
86 Class.forName( databaseDriver );
87
88 // connect to database
89 myConnection = DriverManager.getConnection( databaseURL );
90
91 // create statement
92 myStatement = myConnection.createStatement();
93 }
94 catch ( SQLException exception )
95 {
96 exception.printStackTrace();
97 }
98 catch ( ClassNotFoundException exception )
99 {
100 exception.printStackTrace();
101 }
102
103 // set up GUI
104 createUserInterface();
105
106 } // end constructor
107
108 // create and position GUI components; register event handlers
109 private void createUserInterface()
110 {
111 // get content pane for attaching GUI components
112 Container contentPane = getContentPane();
113
114 // enable explicit positioning of GUI components
115 contentPane.setLayout( null );
116
117 // set up restaurantJLabel
118 restaurantJLabel = new JLabel();
119 restaurantJLabel.setBounds( 80, 8, 128, 24 );
120 restaurantJLabel.setText( "Restaurant" );
121 restaurantJLabel.setFont(
122 new Font( "SansSerif", Font.BOLD, 16 ) );
123 contentPane.add( restaurantJLabel );
124
125 // set up waiterJPanel
126 createWaiterJPanel();
127 contentPane.add( waiterJPanel );
128
129 // set up menuItemsJPanel
130 createMenuItemsJPanel();
131 contentPane.add( menuItemsJPanel );
132
133 // set up subtotalJLabel
134 subtotalJLabel = new JLabel();
135 subtotalJLabel.setBounds( 15, 340, 56, 16 );
136 subtotalJLabel.setText( "Subtotal:" );
137 contentPane.add( subtotalJLabel );
138
139 // set up subtotalJTextField
140 subtotalJTextField = new JTextField();
141 subtotalJTextField.setBounds( 70, 340, 80, 20 );
142 subtotalJTextField.setEditable( false );
143 subtotalJTextField.setBorder(
144 BorderFactory.createLoweredBevelBorder() );
145 subtotalJTextField.setHorizontalAlignment( JTextField.RIGHT );
146 contentPane.add( subtotalJTextField );
147
148 // set up taxJLabel
149 taxJLabel = new JLabel();
150 taxJLabel.setBounds( 15, 372, 56, 16 );
151 taxJLabel.setText( "Tax:" );
152 contentPane.add( taxJLabel );
153
154 // set up taxJTextField
155 taxJTextField = new JTextField();
156 taxJTextField.setBounds( 70, 372, 80, 20 );
157 taxJTextField.setEditable( false );
158 taxJTextField.setBorder(
159 BorderFactory.createLoweredBevelBorder() );
160 taxJTextField.setHorizontalAlignment( JTextField.RIGHT );
161 contentPane.add( taxJTextField );
162
163 // set up totalJLabel
164 totalJLabel = new JLabel();
165 totalJLabel.setBounds( 15, 404, 56, 16 );
166 totalJLabel.setText( "Total:" );
167 contentPane.add( totalJLabel );
168
169 // set up totalJTextField
170 totalJTextField = new JTextField();
171 totalJTextField.setBounds( 70, 404, 80, 20 );
172 totalJTextField.setEditable( false );
173 totalJTextField.setBorder(
174 BorderFactory.createLoweredBevelBorder() );
175 totalJTextField.setHorizontalAlignment( JTextField.RIGHT );
176 contentPane.add( totalJTextField );
177
178 // set up saveTableJButton
179 saveTableJButton = new JButton();
180 saveTableJButton.setBounds( 167, 328, 90, 24 );
181 saveTableJButton.setText( "Save Table" );
182 saveTableJButton.setBorder(
183 BorderFactory.createRaisedBevelBorder() );
184 saveTableJButton.setEnabled( false );
185 contentPane.add( saveTableJButton );
186 saveTableJButton.addActionListener(
187
188 new ActionListener() // anonymous inner class
189 {
190 // event handler called when saveTableJButton is clicked
191 public void actionPerformed( ActionEvent event )
192 {
193 saveTableJButtonActionPerformed( event );
194 }
195
196 } // end anonymous inner class
197
198 ); // end addActionListener
199
200 // set up calculateBillJButton
201 calculateBillJButton = new JButton();
202 calculateBillJButton.setBounds( 167, 360, 90, 24 );
203 calculateBillJButton.setText( "Calculate Bill" );
204 calculateBillJButton.setBorder(
205 BorderFactory.createRaisedBevelBorder() );
206 calculateBillJButton.setEnabled( false );
207 contentPane.add( calculateBillJButton );
208 calculateBillJButton.addActionListener(
209
210 new ActionListener() // anonymous inner class
211 {
212 // event handler called when calculateBillJButton
213 // is clicked
214 public void actionPerformed( ActionEvent event )
215 {
216 calculateBillJButtonActionPerformed( event );
217 }
218
219 } // end anonymous inner class
220
221 ); // end addActionListener
222
223 // set up payBillJButton
224 payBillJButton = new JButton();
225 payBillJButton.setBounds( 167, 392, 90, 24 );
226 payBillJButton.setText( "Pay Bill" );
227 payBillJButton.setBorder(
228 BorderFactory.createRaisedBevelBorder() );
229 payBillJButton.setEnabled( false );
230 contentPane.add( payBillJButton );
231 payBillJButton.addActionListener(
232
233 new ActionListener() // anonymous inner class
234 {
235 // event handler called when payBillJButton is clicked
236 public void actionPerformed( ActionEvent event )
237 {
238 payBillJButtonActionPerformed( event );
239 }
240
241 } // end anonymous inner class
242
243 ); // end addActionListener
244
245 // set properties of application’s window
246 setTitle( "Restaurant Bill Calculator" ); // set window title
247 setSize( 280, 500 ); // set window size
248 setVisible( true ); // display window
249
250 // ensure database connection is closed
251 // when user quits application
252 addWindowListener(
253
254 new WindowAdapter() // anonymous inner class
255 {
256 // event handler called when close button is clicked
257 public void windowClosing( WindowEvent event )
258 {
259 frameWindowClosing( event );
260 }
261
262 } // end anonymous inner class
263
264 ); // end addWindowListener
265
266 } // end method createUserInterface
267
268 // set up waiterJPanel
269 private void createWaiterJPanel()
270

Computer Science & Information Technology

You might also like to view...

Adobe Flash Professional software includes development tools to make the Flash player more accessible.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Data transfers between digital devices using a USB connection are significantly faster than using FireWire.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology