1
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline11.1 Test-Driving the Security Panel Application11.2 Introducing the switch Multiple-Selection Statement11.3 Constructing the Security Panel Application11.4 Wrap-Up
Tutorial 11 – Security Panel Application
Introducing the switch Multiple-Selection Statement, Date and DateFormat
2
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Objectives
• In this tutorial, you will learn to:– Use the switch multiple-selection statement.
– Use case labels.
– Display a date and time.
– Use a JPasswordField.
– Use a Date to determine the system’s current date and time.
– Use a DateFormat to format the date and time.
3
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.1 Test Driving the Security Panel Application
Application Requirements A pharmaceutical company wants to install a security panel outside its laboratory facility. Only authorized personnel may enter the lab, using their security codes. The following are the valid security codes (also called access codes) and the groups of employees they represent:
Values Groups
1645 Technicians
8345 Custodians
9998, 1006–1008 Scientists
When a security code is entered, it should not be visible to anyone standing near the security panel. For each security code, access is either granted or denied. All access attempts are displayed in a screen below the keypad. If access is granted, the date, time and group (scientists, custodians, etc.) are displayed on the screen. If access is denied, the date, the time and a message, “Access Denied,” are displayed on the screen. Furthermore, an employee can enter the access code 7, 8 or 9 to summon a security guard for assistance. The date, the time and a message, “Restricted Access,” are then displayed on the screen to indicate that the request has been received.
4
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.1 Test Driving the Security Panel Application (Cont.)
Figure 11.1 Security Panel application.
Keypad
Output JTextArea
JPasswordField
5
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.1 Test Driving the Security Panel Application (Cont.)
Figure 11.2 Asterisks displayed in the SecurityCode: JPasswordField.
JPasswordField displays one asterisk (*) for each numeric key the user presses (so no one can see the actual security code entered)
• Enter the security code 1212• JPasswordField displays asterisks rather than the typed
characters
6
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.1 Test Driving the Security Panel Application (Cont.)
Figure 11.3 Security Panel displaying the Access Denied message.
Message indicating that an invalid security code was entered
• Press # to submit your security code
• Press C to clear your security code
7
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.1 Test Driving the Security Panel Application (Cont.)
Figure 11.4 Security Panel application confirming a valid security code entry.
Message displayed when a valid security code is entered
• Enter 1006 to log on with a valid security code
8
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.2 Introducing the switch Multiple-Selection Statement
if ( grade == 'A' ) { displayJLabel.setText( "Excellent!" );}else if ( grade == 'B' ){ displayJLabel.setText( "Very good!" );}else if ( grade == 'C' ){ displayJLabel.setText( "Good." );}else if ( grade == 'D' ){ displayJLabel.setText( "Poor." );}else if ( grade == 'F' ){ displayJLabel.setText( "Failure." );}else{ displayJLabel.setText( "Invalid grade." );}
• Multiple selections with a nested if … else statement
9
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.2 Introducing the switch Multiple-Selection Statement (Cont.)
• switch statement: multiple selection statement– Controlling expression– case labels– default case
– Only types char, byte, short, and int can be tested in a switch statement
– break statement
• Char– one of Java’s eight primitive types
– Character constant (character literal)
– Represented as a character within single quotes
10
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.2 Introducing the switch Multiple-Selection Statement (Cont.)
switch ( grade ){ case 'A': displayJLabel.setText( "Excellent!" ); break;
case 'B': displayJLabel.setText( "Very good!" ); break;
case 'C': displayJLabel.setText( "Good." ); break;
case 'D': displayJLabel.setText( "Poor." ); break;
case 'F': displayJLabel.setText( "Failure." ); break;
default: displayJLabel.setText( "Invalid grade." );}
11
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.2 Introducing the switch Multiple-Selection Statement (Cont.)
Figure 11.5 switch multiple-selection statement UML activity diagram.
12
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application
When the user clicks a numeric JButtonGet the JButton’s digitAppend the digit to the text in the JPasswordField
When the user clicks the # JButtonGet the security code input by the user from the JPasswordFieldClear the JPasswordField
switch based on the security code variablecase where access code is 7, 8 or 9
Store text “Restricted Access” in a String variablecase where access code equals 1645
Store text “Technician” in a String variablecase where access code equals 8345
Store text “Custodian” in a String variablecase where access code equals 9998 or is in the range 1006 to 1008
Store text “Scientist” in a String variabledefault case where none of the preceding cases match
Store text “Access Denied” in a String variable
Display a message in the JTextArea with current time and the Stringvariable’s contents
13
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Action Component/Object Event/Method Label all the application’s components
securityCodeJLabel, accessLogJLabel
Application is run
Get the JButton’s digit zeroJButton, oneJButton, twoJButton, threeJButton, fourJButton, fiveJButton, sixJButton, sevenJButton, eightJButton, nineJButton
User clicks a numeric JButton
Append the digit to the text in the JPasswordField
securityCodeJPasswordField
Clear JPasswordField securityCodeJPasswordField User clicks the C JButton
Get the security code input from the JPasswordField
securityCodeJPasswordField User clicks the # JButton
Clear the JPasswordField securityCodeJPasswordField
Determine whether security code is valid
message (String)
Display message in the JTextArea
accessLogJTextArea
Figure 11.6 ACE table for Security Panel application.
14
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Figure 11.7 Setting the securityCodeJPasswordField’sbounds and editable properties.
• Echo characters (masked characters)
• Set character displayed using the setEchoChar method
Set the bounds and editable properties of the
JPasswordField
15
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Figure 11.8 Security Panel application after customizing the JPasswordField.
16
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Figure 11.9 Storing the access code and clearing the Security code: JPasswordField.
Declare message
Store the access code
Clear the JPasswordField
17
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Figure 11.10 Adding a switch statement to the method.
Beginning of the switch statement
18
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Figure 11.11 Adding case labels to the switch statement.
Multiple case labels result in same message
19
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Figure 11.12 Finishing the switch statement.
Access code 1645 for technicians
Access code 8345 for custodians
Access codes 9998, 1006, 1007 and
1008 for scientists
20
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Figure 11.13 Adding a default case to the switch statement.
• switch statements can have at most one default statement
default case at the end of the switch statement
21
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Figure 11.14 Outputting the current date, the time and the message.
• The DateFormat object is similar to a DecimalFormat object– Allows you to output a date and time
Format for the date and time
Add the date and time to the message
22
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Figure 11.15 Appending a one to the end of the security code.
Add 1 to the end of the security code
• Use the + operator to append (concatenate) Strings to other Strings
23
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Figure 11.16 Coding event handlers for 2 JButton and 3 JButton; other JButtons would be similar.
Add 2 to the end of the security code
Add 3 to the end of the security code
24
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Figure 11.17 Clearing the Security Code: JPasswordField.
Clear the security code
25
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11.3 Constructing the Security Panel Application (Cont.)
Figure 11.18 Completed Security Panel application.
2004 Prentice Hall, Inc.All rights reserved.
Outline26
SecurityPanel.java(1 of 19)
1 // Tutorial 11: SecurityPanel.java2 // Enable user to enter security codes specifying access privileges.3 import java.awt.*;4 import java.awt.event.*;5 import javax.swing.*;6 import java.text.DateFormat;7 import java.util.Date;8
9 public class SecurityPanel extends JFrame10 {11 // JLabel and JPasswordField for user to input security code12 private JLabel securityCodeJLabel;13 private JPasswordField securityCodeJPasswordField;14
15 // JButtons to represent security keypad16 private JButton oneJButton;17 private JButton twoJButton;18 private JButton threeJButton;19 private JButton fourJButton;20 private JButton fiveJButton;21 private JButton sixJButton;22 private JButton sevenJButton;23 private JButton eightJButton;24 private JButton nineJButton;25 private JButton clearJButton;
2004 Prentice Hall, Inc.All rights reserved.
Outline27
SecurityPanel.java(2 of 19)
26 private JButton zeroJButton;27 private JButton enterJButton;28
29 // JLabel, JTextArea and JScrollPane to display access log30 private JLabel accessLogJLabel;31 private JTextArea accessLogJTextArea;32 private JScrollPane accessLogJScrollPane;33
34 // no-argument constructor35 public SecurityPanel()36 {37 createUserInterface();38 }39
40 // create and position GUI components; register event handlers41 private void createUserInterface()42 {43 // get content pane for attaching GUI components44 Container contentPane = getContentPane();45 46 // enable explicit positioning of GUI components47 contentPane.setLayout( null );48
2004 Prentice Hall, Inc.All rights reserved.
Outline28
SecurityPanel.java(3 of 19)
49 // set up securityCodeJLabel50 securityCodeJLabel = new JLabel();51 securityCodeJLabel.setBounds( 16, 16, 90, 21 );52 securityCodeJLabel.setText( "Security code:" );53 contentPane.add( securityCodeJLabel );54
55 // set up securityCodeJPasswordField56 securityCodeJPasswordField = new JPasswordField();57 securityCodeJPasswordField.setBounds( 114, 16, 172, 26 );58 securityCodeJPasswordField.setEditable( false ); 59 contentPane.add( securityCodeJPasswordField );60
61 // set up oneJButton62 oneJButton = new JButton();63 oneJButton.setBounds( 80, 64, 50, 50 );64 oneJButton.setText( "1" );65 contentPane.add( oneJButton );66 oneJButton.addActionListener(67
68 new ActionListener() // anonymous inner class69 {70 // event handler called when oneJButton is pressed71 public void actionPerformed( ActionEvent event )72 {73 oneJButtonActionPerformed( event );
Set the bounds and editable properties of the JPasswordField
2004 Prentice Hall, Inc.All rights reserved.
Outline29
SecurityPanel.java(4 of 19)
74 }75
76 } // end anonymous inner class77
78 ); // end call to addActionListener79
80 // set up twoJButton81 twoJButton = new JButton();82 twoJButton.setBounds( 130, 64, 50, 50 );83 twoJButton.setText( "2" );84 contentPane.add( twoJButton );85 twoJButton.addActionListener(86
87 new ActionListener() // anonymous inner class88 {89 // event handler called when twoJButton is pressed90 public void actionPerformed( ActionEvent event )91 {92 twoJButtonActionPerformed( event );93 }94
95 } // end anonymous inner class96
97 ); // end call to addActionListener98
2004 Prentice Hall, Inc.All rights reserved.
Outline30
SecurityPanel.java(5 of 19)
99 // set up threeJButton100 threeJButton = new JButton();101 threeJButton.setBounds( 180, 64, 50, 50 );102 threeJButton.setText( "3" );103 contentPane.add( threeJButton );104 threeJButton.addActionListener(105
106 new ActionListener() // anonymous inner class107 {108 // event handler called when threeJButton is pressed109 public void actionPerformed( ActionEvent event )110 {111 threeJButtonActionPerformed( event );112 }113
114 } // end anonymous inner class115
116 ); // end call to addActionListener117
118 // set up fourJButton119 fourJButton = new JButton();120 fourJButton.setBounds( 80, 114, 50, 50 );121 fourJButton.setText( "4" );122 contentPane.add( fourJButton );123 fourJButton.addActionListener(
2004 Prentice Hall, Inc.All rights reserved.
Outline31
SecurityPanel.java(6 of 19)
124
125 new ActionListener() // anonymous inner class126 {127 // event handler called when fourJButton is pressed128 public void actionPerformed( ActionEvent event )129 {130 fourJButtonActionPerformed( event );131 }132
133 } // end anonymous inner class134
135 ); // end call to addActionListener136
137 // set up fiveJButton138 fiveJButton = new JButton();139 fiveJButton.setBounds( 130, 114, 50, 50 );140 fiveJButton.setText( "5" );141 contentPane.add( fiveJButton );142 fiveJButton.addActionListener(143
2004 Prentice Hall, Inc.All rights reserved.
Outline32
SecurityPanel.java(7 of 19)
144 new ActionListener() // anonymous inner class145 {146 // event handler called when fiveJButton is pressed147 public void actionPerformed( ActionEvent event )148 {149 fiveJButtonActionPerformed( event );150 }151
152 } // end anonymous inner class153
154 ); // end call to addActionListener155
156 // set up sixJButton157 sixJButton = new JButton();158 sixJButton.setBounds( 180, 114, 50, 50 );159 sixJButton.setText( "6" );160 contentPane.add( sixJButton );161 sixJButton.addActionListener(162
163 new ActionListener() // anonymous inner class164 {165 // event handler called when sixJButton is pressed166 public void actionPerformed( ActionEvent event )167 {168 sixJButtonActionPerformed( event );169 }
2004 Prentice Hall, Inc.All rights reserved.
Outline33
SecurityPanel.java(8 of 19)
170
171 } // end anonymous inner class172
173 ); // end call to addActionListener174
175 // set up sevenJButton176 sevenJButton = new JButton();177 sevenJButton.setBounds( 80, 164, 50, 50 );178 sevenJButton.setText( "7" );179 contentPane.add( sevenJButton );180 sevenJButton.addActionListener(181
182 new ActionListener() // anonymous inner class183 {184 // event handler called when sevenJButton is pressed185 public void actionPerformed( ActionEvent event )186 {187 sevenJButtonActionPerformed( event );188 }189
190 } // end anonymous inner class191
192 ); // end call to addActionListener193
2004 Prentice Hall, Inc.All rights reserved.
Outline34
SecurityPanel.java(9 of 19)
194 // set up eightJButton195 eightJButton = new JButton();196 eightJButton.setBounds( 130, 164, 50, 50 );197 eightJButton.setText( "8" );198 contentPane.add( eightJButton );199 eightJButton.addActionListener(200
201 new ActionListener() // anonymous inner class202 {203 // event handler called when eightJButton is pressed204 public void actionPerformed( ActionEvent event )205 {206 eightJButtonActionPerformed( event );207 }208
209 } // end anonymous inner class210
211 ); // end call to addActionListener212
213 // set up nineJButton214 nineJButton = new JButton();215 nineJButton.setBounds( 180, 164, 50, 50 );216 nineJButton.setText( "9" );217 contentPane.add( nineJButton );218 nineJButton.addActionListener(
2004 Prentice Hall, Inc.All rights reserved.
Outline35
SecurityPanel.java(10 of 19)
219
220 new ActionListener() // anonymous inner class221 {222 // event handler called when nineJButton is pressed223 public void actionPerformed( ActionEvent event )224 {225 nineJButtonActionPerformed( event );226 }227
228 } // end anonymous inner class229
230 ); // end call to addActionListener231
232 // set up clearJButton233 clearJButton = new JButton();234 clearJButton.setBounds( 80, 214, 50, 50 );234 clearJButton.setText( "C" );236 contentPane.add( clearJButton );237 clearJButton.addActionListener(238
239 new ActionListener() // anonymous inner class240 {
2004 Prentice Hall, Inc.All rights reserved.
Outline36
SecurityPanel.java(11 of 19)
241 // event handler called when clearJButton is pressed242 public void actionPerformed( ActionEvent event )243 {244 clearJButtonActionPerformed( event );245 }246
247 } // end anonymous inner class248
249 ); // end call to addActionListener250
251 // set up zeroJButton252 zeroJButton = new JButton();253 zeroJButton.setBounds( 130, 214, 50, 50 );254 zeroJButton.setText( "0" );255 contentPane.add( zeroJButton );256 zeroJButton.addActionListener(257
258 new ActionListener() // anonymous inner class259 {260 // event handler called when zeroJButton is pressed261 public void actionPerformed( ActionEvent event )262 {263 zeroJButtonActionPerformed( event );264 }265
2004 Prentice Hall, Inc.All rights reserved.
Outline37
SecurityPanel.java(11 of 19)
241 // event handler called when zeroJButton is pressed242 public void actionPerformed( ActionEvent event )243 {244 zeroJButtonActionPerformed( event );245 }246
247 } // end anonymous inner class248
249 ); // end call to addActionListener250
251 // set up clearJButton252 clearJButton = new JButton();253 clearJButton.setBounds( 80, 214, 50, 50 );254 clearJButton.setText( "C" );255 contentPane.add( clearJButton );256 clearJButton.addActionListener(257
258 new ActionListener() // anonymous inner class259 {260 // event handler called when clearJButton is pressed261 public void actionPerformed( ActionEvent event )262 {263 clearJButtonActionPerformed( event );264 }265
2004 Prentice Hall, Inc.All rights reserved.
Outline38
SecurityPanel.java(12 of 19)
266 } // end anonymous inner class267
268 ); // end call to addActionListener269
270 // set up enterJButton271 enterJButton = new JButton();272 enterJButton.setBounds( 180, 214, 50, 50 );273 enterJButton.setText( "#" );274 contentPane.add( enterJButton );275 enterJButton.addActionListener(276
277 new ActionListener() // anonymous inner class278 {279 // event handler called when enterJButton is pressed280 public void actionPerformed( ActionEvent event )281 {282 enterJButtonActionPerformed( event );283 }284
285 } // end anonymous inner class286
287 ); // end call to addActionListener288
2004 Prentice Hall, Inc.All rights reserved.
Outline39
SecurityPanel.java(13 of 19)
289 // set up accessLogJLabel290 accessLogJLabel = new JLabel();291 accessLogJLabel.setBounds( 16, 285, 100, 16 );292 accessLogJLabel.setText( "Access log:" );293 contentPane.add( accessLogJLabel );294
295 // set up accessLogJTextArea296 accessLogJTextArea = new JTextArea();297
298 // set up accessLogJScrollPane299 accessLogJScrollPane = new JScrollPane( accessLogJTextArea );300 accessLogJScrollPane.setBounds( 16, 309, 270, 95 );301 contentPane.add( accessLogJScrollPane );302
303 // set properties of application's window304 setTitle( "Security Panel" ); // set window's title305 setSize( 310, 450 ); // set window's size306 setVisible( true ); // display window307
308 } // end method createUserInterface309
310 // append 1 to the security code311 private void oneJButtonActionPerformed( ActionEvent event )312 {
2004 Prentice Hall, Inc.All rights reserved.
Outline40
SecurityPanel.java(14 of 19)
313 securityCodeJPasswordField.setText( String.valueOf( 314 securityCodeJPasswordField.getPassword() ) + "1" );315
316 } // end method oneJButtonActionPerformed317
318 // append 2 to the security code319 private void twoJButtonActionPerformed( ActionEvent event )320 {321 securityCodeJPasswordField.setText( String.valueOf( 322 securityCodeJPasswordField.getPassword() ) + "2" );323
324 } // end method twoJButtonActionPerformed325
326 // append 3 to the security code327 private void threeJButtonActionPerformed( ActionEvent event )328 {329 securityCodeJPasswordField.setText( String.valueOf( 330 securityCodeJPasswordField.getPassword() ) + "3" );331
332 } // end method threeJButtonActionPerformed333
334 // append 4 to the security code335 private void fourJButtonActionPerformed( ActionEvent event )336 {
Append the numeric JButton value "1" to the password stored in the JPasswordField
Append the numeric JButton value "2" to the password stored in the JPasswordField
Append the numeric JButton value "3" to the password stored in the JPasswordField
2004 Prentice Hall, Inc.All rights reserved.
Outline41
SecurityPanel.java(15 of 19)
337 securityCodeJPasswordField.setText( String.valueOf( 338 securityCodeJPasswordField.getPassword() ) + "4" );339
340 } // end method fourJButtonActionPerformed341
342 // append 5 to the security code343 private void fiveJButtonActionPerformed( ActionEvent event )344 {345 securityCodeJPasswordField.setText( String.valueOf( 346 securityCodeJPasswordField.getPassword() ) + "5" );347
348 } // end method fiveJButtonActionPerformed349
350 // append 6 to the security code351 private void sixJButtonActionPerformed( ActionEvent event )352 {353 securityCodeJPasswordField.setText( String.valueOf( 354 securityCodeJPasswordField.getPassword() ) + "6" );355
356 } // end method sixJButtonActionPerformed357
358 // append 7 to the security code359 private void sevenJButtonActionPerformed( ActionEvent event )360 {
Append the numeric JButton value "4" to the password stored in the JPasswordField
Append the numeric JButton value "5" to the password stored in the JPasswordField
Append the numeric JButton value "6" to the password stored in the JPasswordField
2004 Prentice Hall, Inc.All rights reserved.
Outline42
SecurityPanel.java(16 of 19)
361 securityCodeJPasswordField.setText( String.valueOf( 362 securityCodeJPasswordField.getPassword() ) + "7" );363
364 } // end method sevenJButtonActionPerformed365
366 // append 8 to the security code367 private void eightJButtonActionPerformed( ActionEvent event )368 {369 securityCodeJPasswordField.setText( String.valueOf( 370 securityCodeJPasswordField.getPassword() ) + "8" );371
372 } // end method eightJButtonActionPerformed373
374 // append 9 to the security code375 private void nineJButtonActionPerformed( ActionEvent event )376 {377 securityCodeJPasswordField.setText( String.valueOf( 378 securityCodeJPasswordField.getPassword() ) + "9" );379
380 } // end method nineJButtonActionPerformed381
382 // append 0 to the security code383 private void zeroJButtonActionPerformed( ActionEvent event )384 {
Append the numeric JButton value "7" to the password stored in the JPasswordField
Append the numeric JButton value "8" to the password stored in the JPasswordField
Append the numeric JButton value "9" to the password stored in the JPasswordField
2004 Prentice Hall, Inc.All rights reserved.
Outline43
SecurityPanel.java(17 of 19)
385 securityCodeJPasswordField.setText( String.valueOf( 386 securityCodeJPasswordField.getPassword() ) + "0" );387
388 } // end method zeroJButtonActionPerformed389
390 // clears the securityCodeJPasswordField391 private void clearJButtonActionPerformed( ActionEvent event )392 {393 securityCodeJPasswordField.setText( "" );394
395 } // end method clearJButtonActionPerformed396 397 // gets access code and determines level of clearance398 private void enterJButtonActionPerformed( ActionEvent event )399 {400 String message; // displays access status of users401
402 // stores access code entered 403 int accessCode = Integer.parseInt( String.valueOf( 404 securityCodeJPasswordField.getPassword() ) );405
406 securityCodeJPasswordField.setText( "" ); 407
408 switch ( accessCode ) // check access code input409 {
Append the numeric JButton value "0" to the password stored in the JPasswordField
Clear the JPasswordField
Get password property
Using a switch statement to determine user access level
String for message to display
Clear the JPasswordField
2004 Prentice Hall, Inc.All rights reserved.
Outline44
SecurityPanel.java(18 of 19)
410 // access code is 7, 8 or 9 411 case 7: 412 case 8: 413 case 9: 414 message = "Restricted Access";415 break; // done processing case416
417 // access code equal to 1645 418 case 1645: 419 message = "Technician"; 420 break; // done processing case421
422 // access code equal to 8345 423 case 8345: 424 message = "Custodian"; 425 break; // done processing case426
427 // access code equal to 9998 or between 1006 and 1008428 case 9998: 429 case 1006: 430 case 1007: 431 case 1008: 432 message = "Scientist"; 433 break; // done processing case 434
Three case labels result in the same message
Terminate switch statement and continue execution with next statement after switch
Access code 1645 for Technicians
Access code 8345 for Custodians
Access codes 9998, 1006, 1007, and 1008 for Scientists
2004 Prentice Hall, Inc.All rights reserved.
Outline45
SecurityPanel.java(19 of 19)
435 // if no other case is true 436 default: 437 message = "Access Denied";438
439 } // end switch statement440
441 // display time and message in access log JTextArea 442 DateFormat formatter = DateFormat.getDateTimeInstance(); 443 accessLogJTextArea.append( formatter.format( new Date() ) + 444 " " + message + "\n" ); 445
446 } // end method enterJButtonActionPerformed447 448 // main method449 public static void main( String[] args )450 {451 SecurityPanel application = new SecurityPanel();452 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );453
454 } // end method main455
456 } // end class SecurityPanel
Default case executes if no other cases match
Right brace ends the switch statement
Append the current date and time to the message