The clothing manufacturer was so impressed with the Sales Report application you created), they want you to create a Profit Report application as well. This application will be similar to the Sales Report appli- cation, but it will allow the user to input information as gains or losses. It should provide JRadioButtons to allow the user to select whether a certain item is a gain or a loss (Fig. 17.33).



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

b) Opening the template file. Open the ProfitReport.java file in your text editor.

c) Modifying the template application.

d) Customizing the Gain JRadioButton. Add code to the createUserInterface method to customize gainJRadioButton. Set the bounds and text so that the compo- nent appears as in Fig. 17.33. Set the JRadioButton to be selected when the applica- tion starts (the default). Add the JRadioButton to profitButtonGroup.

e) Customizing the Loss JRadioButton. Add code to the createUserInterface method to customize lossJRadioButton. Set the bounds and text so that the compo- nent appears as in Fig. 17.33. Add the JRadioButton to profitButtonGroup.

f) Testing which JRadioButton was selected. Add code to the submitItem- JButtonActionPerformed method to test which JRadioButton was selected. If the Gain JRadioButt

```
1 // ProfitReport.java
2 // This application calculates and displays one week's profits.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.*;
6 import javax.swing.*;
7 import javax.swing.border.*;
8
9 public class ProfitReport extends JFrame
10 {
1 // Exercise 17.13: ProfitReport.java
2 // This application calculates and displays one week's profits.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.*;
6 import javax.swing.*;
7 import javax.swing.border.*;
8
9 public class ProfitReport extends JFrame
10 {
11 // JPanel for user inputs
12 private JPanel inputItemJPanel;
13
14 // JLabel and JTextField for item name
15 private JLabel itemJLabel;
16 private JTextField itemJTextField;
17
18 // JLabel and JTextField for monday
19 private JLabel mondayJLabel;
20 private JTextField mondayJTextField;
21
22 // JLabel and JTextField for tuesday
23 private JLabel tuesdayJLabel;
24 private JTextField tuesdayJTextField;
25
26 // JLabel and JTextField for wednesday
27 private JLabel wednesdayJLabel;
28 private JTextField wednesdayJTextField;
29
30 // JLabel and JTextField for thursday
31 private JLabel thursdayJLabel;
32 private JTextField thursdayJTextField;
33
34 // JLabel and JTextField for friday
35 private JLabel fridayJLabel;
36 private JTextField fridayJTextField;
37
38 // JButton to submit gain or loss
39 private JButton submitItemJButton;
40
41 // ButtonGroup for gains and losses
42 private ButtonGroup profitButtonGroup;
43
44 // JRadioButtons for gains and losses
45 private JRadioButton gainJRadioButton;
46 private JRadioButton lossJRadioButton;
47
48 // JLabel, JTextArea and JScrollPane to display profits
49 private JLabel displayJLabel;
50 private JTextArea displayJTextArea;
51 private JScrollPane displayJScrollPane;
52
53 // JLabel and JTextField to display profits
54 private JLabel profitsJLabel;
55 private JTextField profitsJTextField;
56
57 // initialize number of items to zero
58 private int itemCount = 0;
59
60 // constants
61 private final int NUMBER_OF_DAYS = 5;
62 private final int MAXIMUM_ITEMS = 10;
63
64 // one-dimensional array to store names of items
65 private String itemNames[] = new String[ MAXIMUM_ITEMS ];
66
67 // two-dimensional array to store items
68 private double dailyItems[][] =
69 new double[ MAXIMUM_ITEMS ][ NUMBER_OF_DAYS ];
70
71 // DecimalFormat for two digits of precision
11 // JPanel for user inputs
12 private JPanel inputItemJPanel;
13
14 // JLabel and JTextField for item name
15 private JLabel itemJLabel;
16 private JTextField itemJTextField;
17
18 // JLabel and JTextField for monday
19 private JLabel mondayJLabel;
20 private JTextField mondayJTextField;
21
22 // JLabel and JTextField for tuesday
23 private JLabel tuesdayJLabel;
24 private JTextField tuesdayJTextField;
25
26 // JLabel and JTextField for wednesday
27 private JLabel wednesdayJLabel;
28 private JTextField wednesdayJTextField;
29
30 // JLabel and JTextField for thursday
31 private JLabel thursdayJLabel;
32 private JTextField thursdayJTextField;
33
34 // JLabel and JTextField for friday
35 private JLabel fridayJLabel;
36 private JTextField fridayJTextField;
37
38 // JButton to submit gain or loss
39 private JButton submitItemJButton;
40
41 // ButtonGroup for gains and losses
42 private ButtonGroup profitButtonGroup;
43
44 // JRadioButtons for gains and losses
45 private JRadioButton gainJRadioButton;
46 private JRadioButton lossJRadioButton;
47
48 // JLabel, JTextArea and JScrollPane to display profits
49 private JLabel displayJLabel;
50 private JTextArea displayJTextArea;
51 private JScrollPane displayJScrollPane;
52
53 // JLabel and JTextField to display profits
54 private JLabel profitsJLabel;
55 private JTextField profitsJTextField;
56
57 // initialize number of items to zero
58 private int itemCount = 0;
59
60 // constants
61 private final int NUMBER_OF_DAYS = 5;
62 private final int MAXIMUM_ITEMS = 10;
63
64 // one-dimensional array to store names of items
65 private String itemNames[] = new String[ MAXIMUM_ITEMS ];
66
67 // two-dimensional array to store items
68 private double dailyItems[][] =
69 new double[ MAXIMUM_ITEMS ][ NUMBER_OF_DAYS ];
70
71 // DecimalFormat for two digits of precision
11 // JPanel for user inputs
12 private JPanel inputItemJPanel;
13
14 // JLabel and JTextField for item name
15 private JLabel itemJLabel;
16 private JTextField itemJTextField;
17
18 // JLabel and JTextField for monday
19 private JLabel mondayJLabel;
20 private JTextField mondayJTextField;
21
22 // JLabel and JTextField for tuesday
23 private JLabel tuesdayJLabel;
24 private JTextField tuesdayJTextField;
25
26 // JLabel and JTextField for wednesday
27 private JLabel wednesdayJLabel;
28 private JTextField wednesdayJTextField;
29
30 // JLabel and JTextField for thursday
31 private JLabel thursdayJLabel;
32 private JTextField thursdayJTextField;
33
34 // JLabel and JTextField for friday
35 private JLabel fridayJLabel;
36 private JTextField fridayJTextField;
37
38 // JButton to submit gain or loss
39 private JButton submitItemJButton;
40
41 // ButtonGroup for gains and losses
42 private ButtonGroup profitButtonGroup;
43
44 // JRadioButtons for gains and losses
45 private JRadioButton gainJRadioButton;
46 private JRadioButton lossJRadioButton;
47
48 // JLabel, JTextArea and JScrollPane to display profits
49 private JLabel displayJLabel;
50 private JTextArea displayJTextArea;
51 private JScrollPane displayJScrollPane;
52
53 // JLabel and JTextField to display profits
54 private JLabel profitsJLabel;
55 private JTextField profitsJTextField;
56
57 // initialize number of items to zero
58 private int itemCount = 0;
59
60 // constants
61 private final int NUMBER_OF_DAYS = 5;
62 private final int MAXIMUM_ITEMS = 10;
63
64 // one-dimensional array to store names of items
65 private String itemNames[] = new String[ MAXIMUM_ITEMS ];
66
67 // two-dimensional array to store items
68 private double dailyItems[][] =
69 new double[ MAXIMUM_ITEMS ][ NUMBER_OF_DAYS ];
70
71 // DecimalFormat for two digits of precision
11 // JPanel for user inputs
12 private JPanel inputItemJPanel;
13
14 // JLabel and JTextField for item name
15 private JLabel itemJLabel;
16 private JTextField itemJTextField;
17
18 // JLabel and JTextField for monday
19 private JLabel mondayJLabel;
20 private JTextField mondayJTextField;
21
22 // JLabel and JTextField for tuesday
23 private JLabel tuesdayJLabel;
24 private JTextField tuesdayJTextField;
25
26 // JLabel and JTextField for wednesday
27 private JLabel wednesdayJLabel;
28 private JTextField wednesdayJTextField;
29
30 // JLabel and JTextField for thursday
31 private JLabel thursdayJLabel;
32 private JTextField thursdayJTextField;
33
34 // JLabel and JTextField for friday
35 private JLabel fridayJLabel;
36 private JTextField fridayJTextField;
37
38 // JButton to submit gain or loss
39 private JButton submitItemJButton;
40
41 // ButtonGroup for gains and losses
42 private ButtonGroup profitButtonGroup;
43
44 // JRadioButtons for gains and losses
45 private JRadioButton gainJRadioButton;
46 private JRadioButton lossJRadioButton;
47
48 // JLabel, JTextArea and JScrollPane to display profits
49 private JLabel displayJLabel;
50 private JTextArea displayJTextArea;
51 private JScrollPane displayJScrollPane;
52
53 // JLabel and JTextField to display profits
54 private JLabel profitsJLabel;
55 private JTextField profitsJTextField;
56
57 // initialize number of items to zero
58 private int itemCount = 0;
59
60 // constants
61 private final int NUMBER_OF_DAYS = 5;
62 private final int MAXIMUM_ITEMS = 10;
63
64 // one-dimensional array to store names of items
65 private String itemNames[] = new String[ MAXIMUM_ITEMS ];
66
67 // two-dimensional array to store items
68 private double dailyItems[][] =
69 new double[ MAXIMUM_ITEMS ][ NUMBER_OF_DAYS ];
70
71 // DecimalFormat for two digits of precision
72 private DecimalFormat dollars = new DecimalFormat( "$0.00" );
73
74 // no-argument constructor
75 public ProfitReport()
76 {
77 createUserInterface();
78 }
79
80 // create and position GUI components; register event handlers
81 private void createUserInterface()
82 {
83 // get content pane for attaching GUI components
84 Container contentPane = getContentPane();
85
86 // enable explicit positioning of GUI components
87 contentPane.setLayout( null );
88
89 // set up inputItemJPanel
90 inputItemJPanel = new JPanel();
91 inputItemJPanel.setBounds( 16, 16, 208, 242 );
92 inputItemJPanel.setBorder( new TitledBorder( "Input Item" ) );
93 inputItemJPanel.setLayout( null );
94 contentPane.add( inputItemJPanel );
95
96 // set up itemJLabel
97 itemJLabel = new JLabel();
98 itemJLabel.setBounds( 12, 32, 90, 23 );
99 itemJLabel.setText( "Item:" );
100 inputItemJPanel.add( itemJLabel );
101
102 // set up itemJTextField
103 itemJTextField = new JTextField();
104 itemJTextField.setBounds( 104, 32, 88, 21 );
105 itemJTextField.setHorizontalAlignment( JTextField.RIGHT );
106

Computer Science & Information Technology

You might also like to view...

The motor is a part of the hard drive that holds the read/write heads

Indicate whether the statement is true or false

Computer Science & Information Technology

What programming style capitalizes the first letter of all new words in an identifier, including the first one?

A. camel casing B. upper casing C. OOP casing D. Pascal casing

Computer Science & Information Technology