(Inventory Application Enhancement) Enhance the Inventory application to include a JTextField in which the user can enter the number of shipments received in a week (Fig. 4.11). Assume every shipment has the same number of cartons (each of which has the same number of items), and modify the code so that the Inventory application uses the num- ber of shipments in its calculation.
a) Copying the template to your working directory. Copy the C:Examples Tutorial04ExercisesInventory2Enhancement directory to your C:Simply- Java directory.
b) Opening the Command Prompt window and changing directories. Open the Com- mand Prompt window by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd C:SimplyJava Inventory2Enhancement.
c) Compiling the template application. Compile your application by typing javac Inventory.java.
d) Running the template application. Run the enhanced Inventory template applica- tion by typing java Inventory. The GUI of the Address Book template application should appear as shown in Fig. 4.12. Note the differences from Fig. 4.11.
e) Opening the template file. Open the Inventory.java file in y
```
1 // Inventory.java
2 // This application allows the number of cartons, the number of
3 // items in a carton, and the number of shipments this week to be
4 // input and displays the total number of items.
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8
9 public class Inventory extends JFrame
10 {
11 // JLabel and JTextField for number of cartons
12 private JLabel cartonsJLabel;
13 private JTextField cartonsJTextField;
14
15 // JLabel and JTextField for number items per carton
16 private JLabel itemsJLabel;
17 private JTextField itemsJTextField;
18
19 // JLabel and JTextField for number shipments
20 private JLabel shipmentsJLabel;
21 private JTextField shipmentsJTextField;
22
23 // JLabel and JTextField for total items
24 private JLabel totalJLabel;
25 private JTextField totalResultJTextField;
26
27 // JButton to initiate calculation of total items per shipment
28 private JButton calculateJButton;
29
30 // no-argument constructor
31 public Inventory()
32 {
33 createUserInterface();
34 }
35
36 // create and position GUI components; register event handlers
37 private void createUserInterface()
38 {
39 // get content pane and set layout to null
40 Container contentPane = getContentPane();
41 contentPane.setLayout( null );
42
43 // set up cartonsJLabel
44 cartonsJLabel = new JLabel();
45 cartonsJLabel.setText( "Cartons per shipment:" );
46 cartonsJLabel.setBounds( 16, 16, 130, 21 );
47 contentPane.add( cartonsJLabel );
48
49 // set up cartonsJTextField
50 cartonsJTextField = new JTextField();
51 cartonsJTextField.setText( "0" );
52 cartonsJTextField.setBounds( 148, 16, 40, 21 );
53 cartonsJTextField.setHorizontalAlignment( JTextField.RIGHT );
54 contentPane.add( cartonsJTextField );
55
56 // set up itemsJLabel
57 itemsJLabel = new JLabel();
58 itemsJLabel.setText( "Items per carton:" );
59 itemsJLabel.setBounds( 16, 48, 130, 21 );
60 contentPane.add( itemsJLabel );
61
62 // set up itemsJTextField itemsJTextField = new
63 JTextField(); itemsJTextField.setText(
64 "0" ); itemsJTextField.setBounds( 148, 48,
65 40, 21 );
66 itemsJTextField.setHorizontalAlignment( JTextField.RIGHT );
67 contentPane.add( itemsJTextField );
68
69 // set up shipmentsJLabel shipmentsJLabel = new
70 JLabel(); shipmentsJLabel.setText( "Shipments this
71 week:" ); hipmentsJLabel.setBounds( 16, 80, 130,
72 21 ); contentPane.add( shipmentsJLabel );
73
74 // set up shipmentsJTextField
75 shipmentsJTextField = new JTextField();
76
77 shipmentsJTextField.setText( "0" );
78 shipmentsJTextField.setBounds( 148, 80, 40, 21 );
79 shipmentsJTextField.setHorizontalAlignment( JTextField.RIGHT );
80 contentPane.add( shipmentsJTextField );
81
82 // set up totalJLabel totalJLabel = new
83 JLabel(); totalJLabel.setText( "Total:" );
84 totalJLabel.setBounds( 204, 16, 40, 21
85 ); contentPane.add( totalJLabel );
86
87
88 // set up totalResultJTextField
89 totalResultJTextField = new JTextField();
90 totalResultJTextField.setBounds( 244, 16, 86, 21 );
91 totalResultJTextField.setEditable( false );
92 totalResultJTextField.setHorizontalAlignment(
93 JTextField.RIGHT );
94 contentPane.add( totalResultJTextField );
95
96 // set up calculateJButton calculateJButton =
97 new JButton(); calculateJButton.setText(
98 "Calculate Total" );
99 calculateJButton.setBounds( 204, 80, 126, 24 );
100 contentPane.add( calculateJButton );
101 calculateJButton.addActionListener(
102
103 new ActionListener() // anonymous inner class
104 {
105 // event handler called when calculateJButton is pressed
106 public void actionPerformed( ActionEvent event )
107 {
108 calculateJButtonActionPerformed( event );
109 }
110
111 } // end anonymous inner class
112
113 ); // end call to addActionListener
114
115 // set properties of application’s window
116 setTitle( "Inventory" ); // set title bar text
117 setSize( 350, 144 ); // set window size
118 setVisible( true ); // display window
119
120 } // end method createUserInterface
121
122 // calculate total number of items and display results
123 private void calculateJButtonActionPerformed( ActionEvent event )
124 {
125 // display result in totalResultJTextField
126 totalResultJTextField.setText( String.valueOf(
127 Integer.parseInt( cartonsJTextField.getText() ) *
128 Integer.parseInt( itemsJTextField.getText() ) *
129 Integer.parseInt( shipmentsJTextField.getText() ) ) );
130
131 } // end method calculateJButtonActionPerformed
132
133 // main method
134 public static void main( String args[] )
135 {
136 Inventory application = new Inventory();
137 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
138
139 } // end method main
140
141 } // end class Inventory
```
You might also like to view...
Columns must be applied to an entire document
Indicate whether the statement is true or false
This stage in the evolution of the modern IT infrastructure was characterized by connecting personal PCs to work systems.
A. Cloud and mobile computing B. Enterprise computing C. LANs (client/server computing) D. Stand-alone PCs