7
CS4-1 CASE STUDY 4 The SerialNumber Class In this case study you examine the SerialNumber class, which is used by the Home Soft- ware Company to validate software serial numbers. A valid software serial number is in the form LLLLL-DDDD-LLLL, where L indicates an alphabetic letter and D indicates a numeric digit. For example, WRXTQ-7786-PGVZ is a valid serial number. Notice that a serial number consists of three groups of characters, delimited by hyphens. Figure CS4-1 shows a UML diagram for the SerialNumber class. Figure CS4-1 UML diagram for the SerialNumber class The fields first, second, and third are used to hold the first, second, and third groups of characters in a serial number. The valid field is set to true by the constructor to indicate a valid serial number, or false to indicate an invalid serial number. Table CS4-1 describes the class’s methods.

Serial number java_code

Embed Size (px)

DESCRIPTION

An example java program..

Citation preview

Page 1: Serial number java_code

CS4-1

CA

SE

ST

UD

Y

4 The SerialNumber Class

In this case study you examine the SerialNumber class, which is used by the Home Soft-ware Company to validate software serial numbers. A valid software serial number is in the form LLLLL-DDDD-LLLL , where L indicates an alphabetic letter and D indicates a numeric digit. For example, WRXTQ-7786-PGVZ is a valid serial number. Notice that a serial number consists of three groups of characters, delimited by hyphens. Figure CS4-1 shows a UML diagram for the SerialNumber class.

Figure CS4-1 UML diagram for the SerialNumber class

The fi elds first , second , and third are used to hold the fi rst, second, and third groups of characters in a serial number. The valid fi eld is set to true by the constructor to indicate a valid serial number, or false to indicate an invalid serial number. Table CS4-1 describes the class’s methods.

Z17_GADD4764_04_SE_CS4.indd Page CS4-1 11/1/10 1:10 PM user-s138Z17_GADD4764_04_SE_CS4.indd Page CS4-1 11/1/10 1:10 PM user-s138 /Users/user-F447/Desktop/Users/user-F447/Desktop

Page 2: Serial number java_code

CS4-2 Case Study 4 The SerialNumber Class

Table CS4-1 SerialNumber class’s methods

Method Description

Constructor The constructor accepts a string argument that contains a serial number. The string is tokenized and its tokens are stored in the first , second , and third fields. The validate method is called.

isValid This method returns the value in the valid field.

validate This method calls the isFirstGroupValid , isSecondGroupValid , and isThirdGroupValid methods to validate the first , second , and third fields.

isFirstGroupValid This method returns true if the value stored in the first field is valid. Otherwise, it returns false .

isSecondGroupValid This method returns true if the value stored in the second field is valid. Otherwise, it returns false .

isThirdGroupValid This method returns true if the value stored in the third field is valid. Otherwise, it returns false .

The code for the SerialNumber class is shown in Code Listing CS4-1.

Code Listing CS4-1 (SerialNumber.java)

1 import java.util.StringTokenizer; 2 3 /** 4 * The SerialNumber class takes a software serial number in 5 * the form of LLLLL-DDDD-LLLL where each L is a letter 6 * and each D is a digit. The serial number has three groups 7 * of characters, separated by hyphens. The class extracts 8 * the three groups of characters and validates them. 9 */ 10 11 public class SerialNumber 12 { 13 private String first; // First group of characters 14 private String second; // Second group of characters 15 private String third; // Third group of characters 16 private boolean valid; // Flag indicating validity 17 18 /** 19 * The following constructor accepts a serial number as 20 * its String argument. The argument is broken into 21 * three groups and each group is validated. 22 */ 23

Z17_GADD4764_04_SE_CS4.indd Page CS4-2 11/1/10 1:10 PM user-s138Z17_GADD4764_04_SE_CS4.indd Page CS4-2 11/1/10 1:10 PM user-s138 /Users/user-F447/Desktop/Users/user-F447/Desktop

Page 3: Serial number java_code

Case Study 4 The SerialNumber Class CS4-3

24 public SerialNumber(String sn) 25 { 26 // Create a StringTokenizer and initialize 27 // it with a trimmed copy of sn. 28 StringTokenizer st = 29 new StringTokenizer(sn.trim(), "-"); 30 31 // Tokenize and validate. 32 if (st.countTokens() != 3) 33 valid = false; 34 else 35 { 36 first = st.nextToken(); 37 second = st.nextToken(); 38 third = st.nextToken(); 39 validate(); 40 } 41 } 42 43 /** 44 * The following method returns the valid field. 45 */ 46 47 public boolean isValid() 48 { 49 return valid; 50 } 51 52 /** 53 * The following method sets the valid field to true 54 * if the serial number is valid. Otherwise it sets 55 * valid to false. 56 */ 57 58 private void validate() 59 { 60 if (isFirstGroupValid() && isSecondGroupValid() && 61 isThirdGroupValid()) 62 valid = true; 63 else 64 valid = false; 65 } 66 67 /** 68 * The following method validates the first group of 69 * characters. If the group is valid, it returns 70 * true. Otherwise it returns false. 71 */

Z17_GADD4764_04_SE_CS4.indd Page CS4-3 11/1/10 1:10 PM user-s138Z17_GADD4764_04_SE_CS4.indd Page CS4-3 11/1/10 1:10 PM user-s138 /Users/user-F447/Desktop/Users/user-F447/Desktop

Page 4: Serial number java_code

CS4-4 Case Study 4 The SerialNumber Class

72 73 private boolean isFirstGroupValid() 74 { 75 boolean groupGood = true; // Flag 76 int i = 0; // Loop control variable 77 78 // Check the length of the group. 79 if (first.length() != 5) 80 groupGood = false; 81 82 // See if each character is a letter. 83 while (groupGood && i < first.length()) 84 { 85 if (!Character.isLetter(first.charAt(i))) 86 groupGood = false; 87 i++; 88 } 89 90 return groupGood; 91 } 92 93 /** 94 * The following method validates the second group 95 * of characters. If the group is valid, it returns 96 * true. Otherwise it returns false. 97 */ 98 99 private boolean isSecondGroupValid() 100 { 101 boolean groupGood = true; // Flag 102 int i = 0; // Loop control variable 103 104 // Check the length of the group. 105 if (second.length() != 4) 106 groupGood = false; 107 108 // See if each character is a digit. 109 while (groupGood && i < second.length()) 110 { 111 if (!Character.isDigit(second.charAt(i))) 112 groupGood = false; 113 i++; 114 } 115 116 return groupGood; 117 } 118

Z17_GADD4764_04_SE_CS4.indd Page CS4-4 11/1/10 1:10 PM user-s138Z17_GADD4764_04_SE_CS4.indd Page CS4-4 11/1/10 1:10 PM user-s138 /Users/user-F447/Desktop/Users/user-F447/Desktop

Page 5: Serial number java_code

Case Study 4 The SerialNumber Class CS4-5

119 /** 120 * The following method validates the third group of 121 * characters. If the group is valid, it returns 122 * true. Otherwise it returns false. 123 */ 124 125 private boolean isThirdGroupValid() 126 { 127 boolean groupGood = true; // Flag 128 int i = 0; // Loop control variable 129 130 // Check the length of the group. 131 if (third.length() != 4) 132 groupGood = false; 133 134 // See if each character is a letter. 135 while (groupGood && i < third.length()) 136 { 137 if (!Character.isLetter(third.charAt(i))) 138 groupGood = false; 139 i++; 140 } 141 142 return groupGood; 143 } 144 }

Let’s take a closer look at the constructor, in lines 24 through 41. The following statement, in lines 28 and 29, creates a StringTokenizer object, using the hyphen character as a delimiter:

StringTokenizer st = new StringTokenizer(sn.trim(), "-");

Notice that we call the argument’s trim method to remove any leading and/or trailing whitespace characters. This is important because whitespace characters are not used as delimiters in this code. If the argument contains leading whitespace characters, they will be included as part of the fi rst token. Trailing whitespace characters will be included as part of the last token.

Next, the if statement in lines 32 through 40 executes:

if (st.countTokens() != 3) valid = false; else { first = st.nextToken(); second = st.nextToken(); third = st.nextToken(); validate(); }

Z17_GADD4764_04_SE_CS4.indd Page CS4-5 11/1/10 1:10 PM user-s138Z17_GADD4764_04_SE_CS4.indd Page CS4-5 11/1/10 1:10 PM user-s138 /Users/user-F447/Desktop/Users/user-F447/Desktop

Page 6: Serial number java_code

CS4-6 Case Study 4 The SerialNumber Class

A valid serial number must have three groups of characters, so the if statement determines whether the string has three tokens. If not, the valid fi eld is set to false . Otherwise, the three tokens are extracted and assigned to the first , second , and third fi elds. Last, the validate method is called. The validate method calls the isFirstGroupValid , isSecondGroupValid , and isThirdGroupValid methods to validate the three groups of characters. In the end, the valid fi eld will be set to true if the serial number is valid, or false otherwise. The program in Code Listing CS4-2 demonstrates the class.

Code Listing CS4-2 (SerialNumberTester.java)

1 /** 2 * This program demonstrates the SerialNumber class. 3 */ 4 5 public class SerialNumberTester 6 { 7 public static void main(String[] args) 8 { 9 String serial1 = "GHTRJ-8975-AQWR"; // Valid 10 String serial2 = "GHT7J-8975-AQWR"; // Invalid 11 String serial3 = "GHTRJ-8J75-AQWR"; // Invalid 12 String serial4 = "GHTRJ-8975-AQ2R"; // Invalid 13 14 // Validate serial1. 15 16 SerialNumber sn = new SerialNumber(serial1); 17 if (sn.isValid()) 18 System.out.println(serial1 + " is valid."); 19 else 20 System.out.println(serial1 + " is invalid."); 21 22 // Validate serial2. 23 24 sn = new SerialNumber(serial2); 25 if (sn.isValid()) 26 System.out.println(serial2 + " is valid."); 27 else 28 System.out.println(serial2 + " is invalid."); 29 30 // Validate serial3. 31 32 sn = new SerialNumber(serial3); 33 if (sn.isValid()) 34 System.out.println(serial3 + " is valid."); 35 else 36 System.out.println(serial3 + " is invalid."); 37

Z17_GADD4764_04_SE_CS4.indd Page CS4-6 11/1/10 1:10 PM user-s138Z17_GADD4764_04_SE_CS4.indd Page CS4-6 11/1/10 1:10 PM user-s138 /Users/user-F447/Desktop/Users/user-F447/Desktop

Page 7: Serial number java_code

Case Study 4 The SerialNumber Class CS4-7

38 // Validate serial4. 39 40 sn = new SerialNumber(serial4); 41 if (sn.isValid()) 42 System.out.println(serial4 + " is valid."); 43 else 44 System.out.println(serial4 + " is invalid."); 45 } 46 }

Program Output

GHTRJ-8975-AQWR is valid. GHT7J-8975-AQWR is invalid. GHTRJ-8J75-AQWR is invalid. GHTRJ-8975-AQ2R is invalid.

Z17_GADD4764_04_SE_CS4.indd Page CS4-7 11/1/10 1:10 PM user-s138Z17_GADD4764_04_SE_CS4.indd Page CS4-7 11/1/10 1:10 PM user-s138 /Users/user-F447/Desktop/Users/user-F447/Desktop