1 / 45

Objectives

Tutorial 11 – Security Panel Application Introducing the switch Multiple-Selection Statement, Date and DateFormat. Outline 11.1 Test-Driving the Security Panel Application 11.2 Introducing the switch Multiple-Selection Statement 11.3 Constructing the Security Panel Application

colby-riley
Download Presentation

Objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial 11 – SecurityPanel ApplicationIntroducing the switch Multiple-Selection Statement, Date and DateFormat Outline11.1 Test-Driving the Security Panel Application11.2 Introducing the switch Multiple-Selection Statement11.3 Constructing the SecurityPanel Application 11.4 Wrap-Up

  2. 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. 11.1 Test Driving the SecurityPanel Application

  4. JPasswordField Keypad Output JTextArea 11.1 Test Driving the SecurityPanel Application (Cont.) Figure 11.1 SecurityPanel application.

  5. JPasswordField displays one asterisk (*) for each numeric key the user presses (so no one can see the actual security code entered) 11.1 Test Driving the SecurityPanel Application (Cont.) Figure 11.2 Asterisks displayed in the SecurityCode:JPasswordField. • Enter the security code 1212 • JPasswordField displays asterisks rather than the typed characters

  6. Message indicating that an invalid security code was entered 11.1 Test Driving the SecurityPanel Application (Cont.) Figure 11.3 SecurityPanel displaying the AccessDenied message. • Press # to submit your security code • Press C to clear your security code

  7. Message displayed when a valid security code is entered 11.1 Test Driving the SecurityPanel Application (Cont.) Figure 11.4 SecurityPanel application confirming a valid security code entry. • Enter 1006 to log on with a valid security code

  8. 11.2 Introducing the switch Multiple-Selection Statement • Multiple selections with a nested if … else 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." );}

  9. 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. 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. 11.2 Introducing the switch Multiple-Selection Statement (Cont.) Figure 11.5 switch multiple-selection statement UML activity diagram.

  12. 11.3 Constructing the Security Panel Application When the user clicks a numeric JButton Get the JButton’s digit Append the digit to the text in the JPasswordField When the user clicks the # JButton Get the security code input by the user from the JPasswordField Clear the JPasswordField switch based on the security code variable case where access code is 7, 8 or 9 Store text “Restricted Access” in a String variable case where access code equals 1645 Store text “Technician” in a String variable case where access code equals 8345 Store text “Custodian” in a String variable case where access code equals 9998 or is in the range 1006 to 1008 Store text “Scientist” in a String variable default 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 String variable’s contents

  13. 11.3 Constructing the Security Panel Application (Cont.)

  14. Set the bounds and editable properties of the JPasswordField 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

  15. 11.3 Constructing the Security Panel Application (Cont.) Figure 11.8 SecurityPanel application after customizing the JPasswordField.

  16. Declare message Store the access code Clear the JPasswordField 11.3 Constructing the Security Panel Application (Cont.) Figure 11.9 Storing the access code and clearing the Securitycode:JPasswordField.

  17. Beginning of the switch statement 11.3 Constructing the Security Panel Application (Cont.) Figure 11.10 Adding a switch statement to the method.

  18. Multiple case labels result in same message 11.3 Constructing the Security Panel Application (Cont.) Figure 11.11 Adding case labels to the switch statement.

  19. Access code 1645 for technicians Access code 8345 for custodians Access codes 9998, 1006, 1007 and 1008 for scientists 11.3 Constructing the Security Panel Application (Cont.) Figure 11.12 Finishing the switch statement.

  20. default case at the end of the switch statement 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

  21. Format for the date and time Add the date and time to the message 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

  22. Add 1 to the end of the security code 11.3 Constructing the Security Panel Application (Cont.) Figure 11.15 Appending a one to the end of the security code. • Use the + operator to append (concatenate) Strings to other Strings

  23. Add 2 to the end of the security code Add 3 to the end of the security code 11.3 Constructing the Security Panel Application (Cont.) Figure 11.16 Coding event handlers for 2JButton and 3JButton; other JButtons would be similar.

  24. Clear the security code 11.3 Constructing the Security Panel Application (Cont.) Figure 11.17 Clearing the SecurityCode:JPasswordField.

  25. 11.3 Constructing the Security Panel Application (Cont.) Figure 11.18 Completed SecurityPanel application.

  26. 1 // Tutorial 11: SecurityPanel.java 2 // 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 JFrame 10 { 11 // JLabel and JPasswordField for user to input security code 12 private JLabel securityCodeJLabel; 13 private JPasswordField securityCodeJPasswordField; 14 15 // JButtons to represent security keypad 16 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; SecurityPanel.java(1 of 19)

  27. 26 private JButton zeroJButton; 27 private JButton enterJButton; 28 29 // JLabel, JTextArea and JScrollPane to display access log 30 private JLabel accessLogJLabel; 31 private JTextArea accessLogJTextArea; 32 private JScrollPane accessLogJScrollPane; 33 34 // no-argument constructor 35 public SecurityPanel() 36 { 37 createUserInterface(); 38 } 39 40 // create and position GUI components; register event handlers 41 private void createUserInterface() 42 { 43 // get content pane for attaching GUI components 44 Container contentPane = getContentPane(); 45 46 // enable explicit positioning of GUI components 47 contentPane.setLayout( null ); 48 SecurityPanel.java(2 of 19)

  28. 49 // set up securityCodeJLabel 50 securityCodeJLabel = new JLabel(); 51 securityCodeJLabel.setBounds( 16, 16, 90, 21 ); 52 securityCodeJLabel.setText( "Security code:" ); 53 contentPane.add( securityCodeJLabel ); 54 55 // set up securityCodeJPasswordField 56 securityCodeJPasswordField = new JPasswordField(); 57 securityCodeJPasswordField.setBounds( 114, 16, 172, 26 ); 58 securityCodeJPasswordField.setEditable( false ); 59 contentPane.add( securityCodeJPasswordField ); 60 61 // set up oneJButton 62 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 class 69 { 70 // event handler called when oneJButton is pressed 71 public void actionPerformed( ActionEvent event ) 72 { 73 oneJButtonActionPerformed( event ); Set the bounds and editable properties of the JPasswordField SecurityPanel.java(3 of 19)

  29. 74 } 75 76 } // end anonymous inner class 77 78 ); // end call to addActionListener 79 80 // set up twoJButton 81 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 class 88 { 89 // event handler called when twoJButton is pressed 90 public void actionPerformed( ActionEvent event ) 91 { 92 twoJButtonActionPerformed( event ); 93 } 94 95 } // end anonymous inner class 96 97 ); // end call to addActionListener 98 SecurityPanel.java(4 of 19)

  30. 99 // set up threeJButton 100 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 class 107 { 108 // event handler called when threeJButton is pressed 109 public void actionPerformed( ActionEvent event ) 110 { 111 threeJButtonActionPerformed( event ); 112 } 113 114 } // end anonymous inner class 115 116 ); // end call to addActionListener 117 118 // set up fourJButton 119 fourJButton = new JButton(); 120 fourJButton.setBounds( 80, 114, 50, 50 ); 121 fourJButton.setText( "4" ); 122 contentPane.add( fourJButton ); 123 fourJButton.addActionListener( SecurityPanel.java(5 of 19)

  31. 124 125 new ActionListener() // anonymous inner class 126 { 127 // event handler called when fourJButton is pressed 128 public void actionPerformed( ActionEvent event ) 129 { 130 fourJButtonActionPerformed( event ); 131 } 132 133 } // end anonymous inner class 134 135 ); // end call to addActionListener 136 137 // set up fiveJButton 138 fiveJButton = new JButton(); 139 fiveJButton.setBounds( 130, 114, 50, 50 ); 140 fiveJButton.setText( "5" ); 141 contentPane.add( fiveJButton ); 142 fiveJButton.addActionListener( 143 SecurityPanel.java(6 of 19)

  32. 144 new ActionListener() // anonymous inner class 145 { 146 // event handler called when fiveJButton is pressed 147 public void actionPerformed( ActionEvent event ) 148 { 149 fiveJButtonActionPerformed( event ); 150 } 151 152 } // end anonymous inner class 153 154 ); // end call to addActionListener 155 156 // set up sixJButton 157 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 class 164 { 165 // event handler called when sixJButton is pressed 166 public void actionPerformed( ActionEvent event ) 167 { 168 sixJButtonActionPerformed( event ); 169 } SecurityPanel.java(7 of 19)

  33. 170 171 } // end anonymous inner class 172 173 ); // end call to addActionListener 174 175 // set up sevenJButton 176 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 class 183 { 184 // event handler called when sevenJButton is pressed 185 public void actionPerformed( ActionEvent event ) 186 { 187 sevenJButtonActionPerformed( event ); 188 } 189 190 } // end anonymous inner class 191 192 ); // end call to addActionListener 193 SecurityPanel.java(8 of 19)

  34. 194 // set up eightJButton 195 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 class 202 { 203 // event handler called when eightJButton is pressed 204 public void actionPerformed( ActionEvent event ) 205 { 206 eightJButtonActionPerformed( event ); 207 } 208 209 } // end anonymous inner class 210 211 ); // end call to addActionListener 212 213 // set up nineJButton 214 nineJButton = new JButton(); 215 nineJButton.setBounds( 180, 164, 50, 50 ); 216 nineJButton.setText( "9" ); 217 contentPane.add( nineJButton ); 218 nineJButton.addActionListener( SecurityPanel.java(9 of 19)

  35. 219 220 new ActionListener() // anonymous inner class 221 { 222 // event handler called when nineJButton is pressed 223 public void actionPerformed( ActionEvent event ) 224 { 225 nineJButtonActionPerformed( event ); 226 } 227 228 } // end anonymous inner class 229 230 ); // end call to addActionListener 231 232 // set up clearJButton 233 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 class 240 { SecurityPanel.java(10 of 19)

  36. 241 // event handler called when clearJButton is pressed 242 public void actionPerformed( ActionEvent event ) 243 { 244 clearJButtonActionPerformed( event ); 245 } 246 247 } // end anonymous inner class 248 249 ); // end call to addActionListener 250 251 // set up zeroJButton 252 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 class 259 { 260 // event handler called when zeroJButton is pressed 261 public void actionPerformed( ActionEvent event ) 262 { 263 zeroJButtonActionPerformed( event ); 264 } 265 SecurityPanel.java(11 of 19)

  37. 241 // event handler called when zeroJButton is pressed 242 public void actionPerformed( ActionEvent event ) 243 { 244 zeroJButtonActionPerformed( event ); 245 } 246 247 } // end anonymous inner class 248 249 ); // end call to addActionListener 250 251 // set up clearJButton 252 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 class 259 { 260 // event handler called when clearJButton is pressed 261 public void actionPerformed( ActionEvent event ) 262 { 263 clearJButtonActionPerformed( event ); 264 } 265 SecurityPanel.java(11 of 19)

  38. 266 } // end anonymous inner class 267 268 ); // end call to addActionListener 269 270 // set up enterJButton 271 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 class 278 { 279 // event handler called when enterJButton is pressed 280 public void actionPerformed( ActionEvent event ) 281 { 282 enterJButtonActionPerformed( event ); 283 } 284 285 } // end anonymous inner class 286 287 ); // end call to addActionListener 288 SecurityPanel.java(12 of 19)

  39. 289 // set up accessLogJLabel 290 accessLogJLabel = new JLabel(); 291 accessLogJLabel.setBounds( 16, 285, 100, 16 ); 292 accessLogJLabel.setText( "Access log:" ); 293 contentPane.add( accessLogJLabel ); 294 295 // set up accessLogJTextArea 296 accessLogJTextArea = new JTextArea(); 297 298 // set up accessLogJScrollPane 299 accessLogJScrollPane = new JScrollPane( accessLogJTextArea ); 300 accessLogJScrollPane.setBounds( 16, 309, 270, 95 ); 301 contentPane.add( accessLogJScrollPane ); 302 303 // set properties of application's window 304 setTitle( "Security Panel" ); // set window's title 305 setSize( 310, 450 ); // set window's size 306 setVisible( true ); // display window 307 308 } // end method createUserInterface 309 310 // append 1 to the security code 311 private void oneJButtonActionPerformed( ActionEvent event ) 312 { SecurityPanel.java(13 of 19)

  40. 313 securityCodeJPasswordField.setText( String.valueOf( 314 securityCodeJPasswordField.getPassword() ) + "1" ); 315 316 } // end method oneJButtonActionPerformed 317 318 // append 2 to the security code 319 private void twoJButtonActionPerformed( ActionEvent event ) 320 { 321 securityCodeJPasswordField.setText( String.valueOf( 322 securityCodeJPasswordField.getPassword() ) + "2" ); 323 324 } // end method twoJButtonActionPerformed 325 326 // append 3 to the security code 327 private void threeJButtonActionPerformed( ActionEvent event ) 328 { 329 securityCodeJPasswordField.setText( String.valueOf( 330 securityCodeJPasswordField.getPassword() ) + "3" ); 331 332 } // end method threeJButtonActionPerformed 333 334 // append 4 to the security code 335 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 SecurityPanel.java(14 of 19)

  41. 337 securityCodeJPasswordField.setText( String.valueOf( 338 securityCodeJPasswordField.getPassword() ) + "4" ); 339 340 } // end method fourJButtonActionPerformed 341 342 // append 5 to the security code 343 private void fiveJButtonActionPerformed( ActionEvent event ) 344 { 345 securityCodeJPasswordField.setText( String.valueOf( 346 securityCodeJPasswordField.getPassword() ) + "5" ); 347 348 } // end method fiveJButtonActionPerformed 349 350 // append 6 to the security code 351 private void sixJButtonActionPerformed( ActionEvent event ) 352 { 353 securityCodeJPasswordField.setText( String.valueOf( 354 securityCodeJPasswordField.getPassword() ) + "6" ); 355 356 } // end method sixJButtonActionPerformed 357 358 // append 7 to the security code 359 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 SecurityPanel.java(15 of 19)

  42. 361 securityCodeJPasswordField.setText( String.valueOf( 362 securityCodeJPasswordField.getPassword() ) + "7" ); 363 364 } // end method sevenJButtonActionPerformed 365 366 // append 8 to the security code 367 private void eightJButtonActionPerformed( ActionEvent event ) 368 { 369 securityCodeJPasswordField.setText( String.valueOf( 370 securityCodeJPasswordField.getPassword() ) + "8" ); 371 372 } // end method eightJButtonActionPerformed 373 374 // append 9 to the security code 375 private void nineJButtonActionPerformed( ActionEvent event ) 376 { 377 securityCodeJPasswordField.setText( String.valueOf( 378 securityCodeJPasswordField.getPassword() ) + "9" ); 379 380 } // end method nineJButtonActionPerformed 381 382 // append 0 to the security code 383 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 SecurityPanel.java(16 of 19)

  43. 385 securityCodeJPasswordField.setText( String.valueOf( 386 securityCodeJPasswordField.getPassword() ) + "0" ); 387 388 } // end method zeroJButtonActionPerformed 389 390 // clears the securityCodeJPasswordField 391 private void clearJButtonActionPerformed( ActionEvent event ) 392 { 393 securityCodeJPasswordField.setText( "" ); 394 395 } // end method clearJButtonActionPerformed 396 397 // gets access code and determines level of clearance 398 private void enterJButtonActionPerformed( ActionEvent event ) 399 { 400 String message; // displays access status of users 401 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 input 409 { Append the numeric JButton value "0" to the password stored in the JPasswordField Clear the JPasswordField String for message to display Get password property Clear the JPasswordField Using a switch statement to determine user access level SecurityPanel.java(17 of 19)

  44. 410 // access code is 7, 8 or 9 411 case7: 412 case8: 413 case9: 414 message = "Restricted Access"; 415 break; // done processing case 416 417 // access code equal to 1645 418 case1645: 419 message = "Technician"; 420 break; // done processing case 421 422 // access code equal to 8345 423 case8345: 424 message = "Custodian"; 425 break; // done processing case 426 427 // access code equal to 9998 or between 1006 and 1008 428 case9998: 429 case1006: 430 case1007: 431 case1008: 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 SecurityPanel.java(18 of 19)

  45. 435 // if no other case is true 436 default: 437 message = "Access Denied"; 438 439 } // end switch statement 440 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 enterJButtonActionPerformed 447 448 // main method 449 public static void main( String[] args ) 450 { 451 SecurityPanel application = new SecurityPanel(); 452 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 453 454 } // end method main 455 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 SecurityPanel.java(19 of 19)

More Related