Create an application that allows a user to reserve a car for the specified day (Fig. 25.52). The car reservation company can rent out only four cars per day. Let the application allow the user to specify a certain day. If four cars have already been reserved for that day, then indicate to the user that no vehicles are available. Reserva- tions are stored in reservations.txt.



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

b) Opening the template file. Open the CarReservation.java file in your text editor. c) Declaring a BufferedReader and a PrintWriter instance variable. At line 24,

declare two instance variables—a BufferedReader object named input, which will

be used to read from a file, and a PrintWriter object named output, which will be used to write to a file.

d) Opening a file to read from. In the reserveCarJButtonActionPerformed method (which immediately follows createUserInterface), create a new File object reserveFile for the file reservations.txt. Then, add a try block. Inside the try block, create a FileReader by passing the reserveFile to its constructor . Use this FileReader to initialize BufferedReader input.

e) Retrieving the selected date. For this application, you should store in reserva- tions.txt the date selected


```

1 // CarReservation.java
2 // This application allows users to input their names and
3 // reserve cars on various days.
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.io.*;
7 import java.util.Date;
8 import javax.swing.*;
9 import javax.swing.event.*;
10
11 public class CarReservation extends JFrame
12 {
13 // JLabel and JSpinner to display date
14 private JLabel selectDateJLabel;
15 private JSpinner dateJSpinner;
16
17 // JLabel and JTextField to display name
18 private JLabel nameJLabel;
19 private JTextField nameJTextField;
20
21 // JButton to reserve car
22 private JButton reserveCarJButton;
23
24 // BufferedReader and PrintWriter to allow
25 // reading from, and writing to, a file
26 private BufferedReader input;
27 private PrintWriter output;
28
29 // no-argument constructor
30 public CarReservation()
31 {
32 createUserInterface();
33 }
34
35 // create and position GUI components
36 public 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 selectDateJLabel
45 selectDateJLabel = new JLabel();
46 selectDateJLabel.setBounds( 16, 16, 96, 23 );
47 selectDateJLabel.setText( "Select the date:" );
48 contentPane.add( selectDateJLabel );
49
50 // set up dateJSpinner
51 dateJSpinner = new JSpinner( new SpinnerDateModel() );
52 dateJSpinner.setBounds( 16, 43, 250, 23 );
53 dateJSpinner.setEditor( new JSpinner.DateEditor(
54 dateJSpinner, "MM/dd/yyyy" ) );
55 contentPane.add( dateJSpinner );
56 dateJSpinner.addChangeListener(
57
58 new ChangeListener() // anonymous inner class
59 {
60 // event handler called when dateJSpinner is changed
61 public void stateChanged( ChangeEvent event )
62 {
63 dateJSpinnerChanged( event );
64 }
65
66 } // end anonymous inner class
67
68 ); // end call to addActionListener
69
70 // set up nameJLabel
71 nameJLabel = new JLabel();
72 nameJLabel.setBounds( 16, 70, 100, 23 );
73 nameJLabel.setText( "Name: " );
74 contentPane.add( nameJLabel );
75
76 // set up nameJTextField
77 nameJTextField = new JTextField();
78 nameJTextField.setBounds( 16, 97, 250, 23 );
79 contentPane.add( nameJTextField );
80
81 // set up reserveCarJButton
82 reserveCarJButton = new JButton();
83 reserveCarJButton.setBounds( 16, 130, 250, 23 );
84 reserveCarJButton.setText( "Reserve Car" );
85 contentPane.add( reserveCarJButton );
86 reserveCarJButton.addActionListener(
87
88 new ActionListener() // anonymous inner class
89 {
90 // event handler called when reserveCarJButton is clicked
91 public void actionPerformed( ActionEvent event )
92 {
93 reserveCarJButtonActionPerformed( event );
94 }
95
96 } // end anonymous inner class
97
98 ); // end call to addActionListener
99
100 // set properties of application's window
101 setTitle( "Car Reservation" ); // set title bar string
102 setSize( 287, 190 ); // set window size
103 setVisible( true ); // display window
104
105 } // end method createUserInterface
106
107 // write reservation to a file
108 private void reserveCarJButtonActionPerformed( ActionEvent event )
109 {
110 File reserveFile = new File( "reservations.txt" );
111
112 // read reservations from, and write a reservation to, a file
113 try
114 {
115 // create buffered reader to open and read text file
116 FileReader inputFile = new FileReader( reserveFile );
117 input = new BufferedReader( inputFile );
118
119 // get selected date
120 Date fullDate = ( Date ) dateJSpinner.getValue();
121 String currentDate = fullDate.toString();
122 String monthDay = currentDate.substring( 0, 10 );
123 String year = currentDate.substring( 24, 28 );
124 currentDate = monthDay + " " + year;
125
126 int dateCount = 1;
127 String contents = input.readLine();
128
129 // check if too many cars are reserved
130 while ( contents != null )
131 {
132 // check reservation date
133 if ( contents.equals( currentDate ) )
134 {
135 // check reservation number
136 if ( dateCount < 4 )
137 {
138 dateCount++;
139 }
140 else
141 {
101 setTitle( "Car Reservation" ); // set title bar string
102 setSize( 287, 190 ); // set window size
103 setVisible( true ); // display window
104
105 } // end method createUserInterface
106
107 // write reservation to a file
108 private void reserveCarJButtonActionPerformed( ActionEvent event )
109 {
110 File reserveFile = new File( "reservations.txt" );
111
112 // read reservations from, and write a reservation to, a file
113 try
114 {
115 // create buffered reader to open and read text file
116 FileReader inputFile = new FileReader( reserveFile );
117 input = new BufferedReader( inputFile );
118
119 // get selected date
120 Date fullDate = ( Date ) dateJSpinner.getValue();
121 String currentDate = fullDate.toString();
122 String monthDay = currentDate.substring( 0, 10 );
123 String year = currentDate.substring( 24, 28 );
124 currentDate = monthDay + " " + year;
125
126 int dateCount = 1;
127 String contents = input.readLine();
128
129 // check if too many cars are reserved
130 while ( contents != null )
131 {
132 // check reservation date
133 if ( contents.equals( currentDate ) )
134 {
135 // check reservation number
136 if ( dateCount < 4 )
137 {
138 dateCount++;
139 }
140 else
141 {
142 JOptionPane.showMessageDialog( this, "Too many " +
143 "cars have already been reserved for that day.",
144 "Sorry", JOptionPane.INFORMATION_MESSAGE );
145
146 // disable reserveCarJButton
147 reserveCarJButton.setEnabled( false );
148
149 return; // exit method
150 }
151
152 } // end if
153
154 contents = input.readLine(); // read next line
155
156 } // end while
157
158 input.close(); // close BufferedReader
159
160 // create PrintWriter to open and write text file
161 FileWriter outputFile = new FileWriter( reserveFile, true );
162 output = new PrintWriter( outputFile );
163
164 // write user input to file output.println
165 ( currentDate ); output.println(
166 nameJTextField.getText() );
167
168 JOptionPane.showMessageDialog( this,
169 "Your car has been reserved.", "Thank you",
170 JOptionPane.INFORMATION_MESSAGE );
171
172 output.close(); // close PrintWriter
173
174 } // end try
175 catch ( IOException exception )
176 {
177 JOptionPane.showMessageDialog( this, "Invalid file.",
178 "Error", JOptionPane.ERROR_MESSAGE );
179
180 // disable dateJSpinner and reserveCarJButton
181 dateJSpinner.setEnabled( false );
182 reserveCarJButton.setEnabled( false );
183 }
184
185 nameJTextField.setText( "" );
186
187 } // end method reserveCarJButtonActionPerformed
188
189 // enable reserveCarJButton
190 private void dateJSpinnerChanged( ChangeEvent event )
191 {
192 reserveCarJButton.setEnabled( true );
193
194 } // end method dateJSpinnerChanged
195
196 // main method
197 public static void main( String[] args )
198 {
199 CarReservation application = new CarReservation();
200 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
201
202 } // end method main
203
204 } // end class CarReservation
```

Computer Science & Information Technology

You might also like to view...

All programs are eventually converted into ____ code before they can be executed by the microprocessor.

A. machine B. source C. binary D. application

Computer Science & Information Technology

The ________ view in Access looks similar to an Excel spreadsheet

A) Report B) Form C) Datasheet D) Design

Computer Science & Information Technology

Users can store files created with other software, such as Word or Excel, in a table using the ________ data type

Fill in the blank(s) with correct word

Computer Science & Information Technology

Refer to the Exhibit. An administrator has configured a host profile so that ESXi 6.x hosts will point to the corporate NTP server for time synchronization. The NTP server is located at 10.0.30.213, but time has not been synchronized properly across the ESXi hosts. The administrator reviews Host Profile settings as shown in the Exhibit. Which two steps are required to resolve the issue? (Choose two.)

A. Correct the NTP server IP address. B. Check the host for host profile compliance. C. Remediate the host based on the updated host profile. D. Change the NTP server to the FQDN as IP Addresses are not supported.

Computer Science & Information Technology