Tutorial 4 – Completing the Inventory
Application
Introducing Programming
Outline
4.1 Test-Driving the Inventory Application 4.2 Introduction to Java Code
4.3 Placing Code in an Event Handler
4.4 Performing a Calculation and Displaying the Result 4.5 Wrap-Up
Objectives
• In this tutorial, you will learn to:
– Enable your applications to perform actions in response to
JButton clicks.
– Use the multiplication operator.
– Use method Integer.parseInt to convert a String to an int.
– Use method String.valueOf to convert a numeric value to a String.
4.1 Completing the Inventory Application
Application Requirements
A college bookstore receives cartons of textbooks. In each shipment, each carton contains the same number of textbooks. The inventory manager wants to use a computer to calculate the total number of textbooks arriving at the bookstore for each shipment. The inventory manager will enter the number of cartons received in a shipment and the fixed number of textbooks per carton of the shipment; the application should then calculate and display the total number of textbooks in the shipment.
(Cont.)
Figure 4.1 Inventory application with quantities entered.
• Input data into application
– Enter 3 in the Cartons per shipment: JTextField
– Enter 15 in the Items per carton: JTextField
4.1 Completing the Inventory Application (Cont.)
Figure 4.2 Calculating the total in the Inventory application.
Result of calculation
• Calculating the total
– Click the Calculate Total JButton
4.2 Introduction to Java Code
Figure 4.3 Text editor showing a portion of the code for the Inventory application.
Beginning of class declaration
4.2 Introduction to Java Code (Cont.)
• Java Code
– Classes (Case sensitive)
• Class declaration
• Class keyword
• Class name
• Identifier
• Left brace
• Body
• Right brace
• Inherits
• Extends
– Methods
• Blocks
– Keywords (reserved words)
4.3 Placing Code in an Event Handler (Cont.)
Figure 4.4 Empty event handler calculateJButtonActionPerformed before you add your application code.
Empty event handler
• Clicking a JButton generates an actionPerformed
event
– Code in the event handler is executed when JButton is clicked
4.3 Placing Code in an Event Handler (Cont.)
Figure 4.5 Running the application before adding functionality to the event handler.
4.3 Placing Code in an Event Handler (Cont.)
• Comment
– Indicated by two forward slash characters (//) – Full-line comments
– End-of-line comments
• Statement
– Ends with a semicolon – Example:
• totalResultJTextField.setText( “100” );
• Method
– Called
– Return value – Arguments – Dot separator
4.3 Placing Code in an Event Handler (Cont.)
• Multiplication operator
– Operands
• Left operand
• Right operand
– Binary operator
4.3 Placing Code in an Event Handler (Cont.)
Figure 4.6 Code added to the Calculate Total JButton’s event handler.
Event handler for
Calculate Total JButton
Type this code
4.3 Placing Code in an Event Handler (Cont.)
Figure 4.7 Execution of application with an event handler.
Result of clicking
Calculate Total JButton
• Clicking the Calculate Total JButton
– Total: JTextField still displays 45
the Result
• Multiline statement
– Java ignores extra spaces, tabs and newlines (blank lines)
• White space
• Integer.parseInt
– Converts a String to an int so that you can perform calculations
• getText
– Returns a String containing the text property of a component
4.4 Performing a Calculation and Displaying the Result (Cont.)
Figure 4.8 Using multiplication in the Inventory application.
Read the values from
cartonsJTextField and
itemsJTextField, convert them to integers, multiply the integer values and display the result in
totalResultJTextField
the Result (Cont.)
Figure 4.9 Execution of the completed Inventory application.
Result of clicking
Calculate Total JButton
Outline
Inventory.java (1 of 6)
2 // Calculates the number of items in a shipment based on the number 3 // of cartons received and the number of items per carton.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7
8 public class Inventory extends JFrame 9 {
10 // JLabel and JTextField for cartons per shipment 11 private JLabel cartonsJLabel;
12 private JTextField cartonsJTextField;
13
14 // JLabel and JTextField for items per carton 15 private JLabel itemsJLabel;
16 private JTextField itemsJTextField;
17
18 // JLabel and JTextField for total items per shipment 19 private JLabel totalJLabel;
20 private JTextField totalResultJTextField;
21
22 // JButton to initiate calculation of total items per shipment 23 private JButton calculateJButton;
24
Inventory.java (2 of 6)
27 {
28 createUserInterface();
29 } 30
31 // create and position GUI components; register event handlers 32 public void createUserInterface()
33 {
34 // get content pane and set layout to null 35 Container contentPane = getContentPane();
36 contentPane.setLayout( null );
37
38 // set up cartonsJLabel
39 cartonsJLabel = new JLabel();
40 cartonsJLabel.setText( "Cartons per shipment:" );
41 cartonsJLabel.setBounds( 16, 16, 130, 21 );
42 contentPane.add( cartonsJLabel );
43
44 // set up itemsJLabel
45 itemsJLabel = new JLabel();
46 itemsJLabel.setText( "Items per carton:" );
47 itemsJLabel.setBounds( 16, 48, 104, 21 );
48 contentPane.add( itemsJLabel );
49
Outline
Inventory.java (3 of 6)
51 totalJLabel = new JLabel();
52 totalJLabel.setText( "Total:" );
53 totalJLabel.setBounds( 204, 16, 40, 21 );
54 contentPane.add( totalJLabel );
55
56 // set up cartonsJTextField
57 cartonsJTextField = new JTextField();
58 cartonsJTextField.setText( "0" );
59 cartonsJTextField.setBounds( 148, 16, 40, 21 );
60 cartonsJTextField.setHorizontalAlignment( JTextField.RIGHT );
61 contentPane.add( cartonsJTextField );
62
63 // set up itemsJTextField
64 itemsJTextField = new JTextField();
65 itemsJTextField.setText( "0" );
66 itemsJTextField.setBounds( 148, 48, 40, 21 );
67 itemsJTextField.setHorizontalAlignment( JTextField.RIGHT );
68 contentPane.add( itemsJTextField );
69
70 // set up totalResultJTextField
71 totalResultJTextField = new JTextField();
72 totalResultJTextField.setBounds( 244, 16, 86, 21 );
73 totalResultJTextField.setHorizontalAlignment(
74 JTextField.RIGHT );
Inventory.java (4 of 6)
77
78 // set up calculateJButton
79 calculateJButton = new JButton();
80 calculateJButton.setText( "Calculate Total" );
81 calculateJButton.setBounds( 204, 48, 126, 24 );
82 contentPane.add( calculateJButton );
83 calculateJButton.addActionListener(
84
85 new ActionListener() // anonymous inner class 86 {
87 // method called when calculate JButton is pressed 88 public void actionPerformed( ActionEvent event ) 89 {
90 calculateJButtonActionPerformed( event );
91 } 92
93 } // end anonymous inner class 94
95 ); // end call to addActionListener 96
Outline
Inventory.java (5 of 6)
98 setSize( 354, 112 ); // set window size
99 setTitle( "Inventory" ); // set title bar string 100 setVisible( true ); // display window
101
102 } // end method createUserInterface 103
104 // calculate the total items in the shipment
105 private void calculateJButtonActionPerformed( ActionEvent event ) 106 {
107 // multiply values input and display result in the text field 108 totalResultJTextField.setText( String.valueOf(
109 Integer.parseInt( cartonsJTextField.getText() ) * 110 Integer.parseInt( itemsJTextField.getText() ) ) );
111
112 } // end method calculateJButtonActionPerformed 113
Read the values from
cartonsJTextField
and
itemsJTextField, convert them to
integers, multiply the integer values and display the result in
totalResultJText Field
Inventory.java (6 of 6)
116 {
117 Inventory application = new Inventory();
118 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
119
120 } // end method main 121
122 } // end class Inventory