Develop an application that calculates a restaurant bill (Fig. 9.27). When the user clicks the Add Items JButton, the user should be able to enter the item ordered, the quantity of the item ordered and the price of the item. To do this, three input dialogs will be displayed per item. The user can enter exactly three items. Once all three items have been entered, your application should display the number ordered, the item ordered and the price per unit in three JTextAreas. The event handler should then calculate and display the total price. For each set of input, the item’s information will be appended to the JTextAreas. Display all prices with dollar formats (that is, preceded by a dollar sign and with two digits to the right of the decimal point). Notice that



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

b) Opening the template file. Open the RestaurantBill.java file in your text editor. c) Adding the do…while loop to the addJButtonActionPerformed event handler.

Beginning in line 130, add a do…while statement that will loop three times. Use an

int variable called counter to control the loop.

d) Adding code to the do…while loop. Within the do…while loop, write three state- ments that display input dialogs (using the JOptionPane.showInputDialog method) to retrieve the quantity, name and price of an item, respectively. Use the value of variable counter in the input dialog prompts to indicate that the user is inputting information for the first, second or third item. Convert the result of the quantity input dialog to type int and the result of the price input dialog to type dou- ble. Use JTextArea meth


```
1 // RestaurantBill.java
2 // This application calculates a restaurant bill.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import java.text.*;
7
8 public class RestaurantBill extends JFrame
9 {
10 // JLabel and JTextArea for quantity of lunch items
11 private JLabel quantityJLabel;
12 private JTextArea quantityJTextArea;
13
14 // JLabel and JTextArea for name of lunch items
15 private JLabel itemJLabel;
16 private JTextArea itemJTextArea;
17
18 // JLabel and JTextArea for price of lunch items
19 private JLabel priceJLabel;
20 private JTextArea priceJTextArea;
21
22 // JButton initiates calculation of bill
23 private JButton addItemsJButton;
24
25 // JLabel and JTextField displays total cost
26 private JLabel totalCostJLabel;
27 private JTextField totalCostJTextField;
28
29 // no-argument constructor
30 public RestaurantBill()
31 {
32 createUserInterface();
33 }
34
35 // create and position GUI components; register event handlers
36 private void createUserInterface()
37 {
38 // get content pane for attaching GUI components
39 Container contentPane = getContentPane();
40
41 // enable explicit positioning of GUI components
42 contentPane.setLayout( null );
43
44 // set up quantityJLabel
45 quantityJLabel = new JLabel();
46 quantityJLabel.setBounds( 16, 8, 56, 23 );
47 quantityJLabel.setText( "Quantity:" );
48 contentPane.add( quantityJLabel );
49
50 // set up quantityJTextArea
51 quantityJTextArea = new JTextArea();
52 quantityJTextArea.setBounds( 16, 32, 72, 55 );
53 contentPane.add( quantityJTextArea );
54
55 // set up itemJLabel
56 itemJLabel = new JLabel();
57 itemJLabel.setBounds( 104, 8, 64, 23 );
58 itemJLabel.setText( "Item:" );
59 contentPane.add( itemJLabel );
60
61 // set up itemJTextArea
62 itemJTextArea = new JTextArea();
63 itemJTextArea.setBounds( 104, 32, 104, 55 );
64 contentPane.add( itemJTextArea );
65
66 // set up priceJLabel
67 priceJLabel = new JLabel();
68 priceJLabel.setBounds( 224, 8, 56, 23 );
69 priceJLabel.setText( "Price:" );
70 contentPane.add( priceJLabel );
71
72 // set up priceJTextArea
73 priceJTextArea = new JTextArea();
74 priceJTextArea.setBounds( 224, 32, 96, 55 );
75 contentPane.add( priceJTextArea );
76
77 // set up addItemsJButton
78 addItemsJButton = new JButton();
79 addItemsJButton.setBounds( 16, 105, 96, 23 );
80 addItemsJButton.setText( "Add Items" );
81 contentPane.add( addItemsJButton );
82 addItemsJButton.addActionListener(
83
84 new ActionListener() // anonymous inner class
85 {
86 // event handler called when addItemsJButton is pressed
87 public void actionPerformed( ActionEvent event )
88 {
89 addItemsJButtonActionPerformed( event );
90 }
91
92 } // end anonymous inner class
93
94 ); // end call to addActionListener
95
96 // set up totalCostJLabel
97 totalCostJLabel = new JLabel();
98 totalCostJLabel.setBounds( 144, 105, 64, 23 );
99 totalCostJLabel.setText( "Total Cost:" );
100 contentPane.add( totalCostJLabel );
101
102 // set up totalCostJTextField
103 totalCostJTextField = new JTextField();
104 totalCostJTextField.setBounds( 224, 105, 96, 23 );
105 totalCostJTextField.setEditable( false );
106 totalCostJTextField.setHorizontalAlignment(
107 JTextField.CENTER );
108 contentPane.add( totalCostJTextField );
109
110 // set properties of application’s window
111 setTitle( "Restaurant Bill" ); // set title bar text
112 setSize( 345, 169 ); // set window size
113 setVisible( true ); // display window
114
115 } // end method createUserInterface
116
117 // method to retrieve input from users and calculate total
118 private void addItemsJButtonActionPerformed( ActionEvent event )
119 {
120 // clear previous grades and calculation result
121 totalCostJTextField.setText( "" );
122 quantityJTextArea.setText( "" );
123 itemJTextArea.setText( "" );
124 priceJTextArea.setText( "" );
125
126 int counter = 1;
127 double total = 0;
128 DecimalFormat dollars = new DecimalFormat( "$0.00" );
129 do
130 {
131 // get quantity of item
132 String quantityInput = JOptionPane.showInputDialog(
133 null, "Enter Quantity of Item " + counter );
134 int quantity = Integer.parseInt( quantityInput );
135
136 // get name of item
137 String item = JOptionPane.showInputDialog(
138 null, "Enter Name of Item " + counter );
139
140 // get price of item
141 String priceInput = JOptionPane.showInputDialog(
142 null, "Enter Price of Item " + counter );
143 double price = Double.parseDouble( priceInput );
144
145
146 // add item info to JTextAreas
147 quantityJTextArea.append( quantity + "\n" );
148 itemJTextArea.append( item + "\n" );
149 priceJTextArea.append( dollars.format( price ) + "\n" );
150
151 total += price * quantity; // calculate total
152 counter++; // increment counter
153 }
154 while ( counter <= 3 );
155
156 // display price with monetary format
157 totalCostJTextField.setText( dollars.format( total ) );
158
159 } // end method addItemsJButtonActionPerformed
160
161 // main method
162 public static void main( String[] args )
163 {
164 RestaurantBill application = new RestaurantBill();
165 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
166
167 } // end method main
168
169 } // end class RestaurantBill
```

Computer Science & Information Technology

You might also like to view...

Which programming language is typically used for Android development?

a. C b. Java c. Python d. C# e. Perl

Computer Science & Information Technology

A style set is a group of title, heading, and paragraph styles

Indicate whether the statement is true or false

Computer Science & Information Technology

You can choose to not publish certain pages in the site.

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

Computer Science & Information Technology

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

If a graphics file is fragmented across areas on a disk, you must recover all the fragments before re-creating the file.

Computer Science & Information Technology