Description The Wisconsin Department of Motor Vehicles encodes detailed informat
ID: 3641170 • Letter: D
Question
Description
The Wisconsin Department of Motor Vehicles encodes detailed information about each driver in their driver's license number. Two of the simplest data encodings are year of birth and first letter of the last name. You will writer a program that prompts the user for a valid last name, year of birth, and driver's license number. Then your program will report if these values are consistent, or if they are fraudulent. You program could be used to assist a business that uses a driver's license to confirm age.
Functional requirements:
1. The program will prompt the user for as valid last name, reprompting until a valid last name is entered. A last name is valid if it contains only letters and spaces, it begins and ends with a letter, and it does not contain 2 consecutive spaces.
2. The program then prompts for a year of birth. Valid birth years are integers between 1900 and 1999, inclusive, but your program must reprompt on non-numeric input. Implicitly, this means you must read the data as a string.
3. Finally, the program prompts for a valid Wisconsin driver's license number. It will have the format LDDD-DDDD-DDDD-DD, where L is a capital letter and each D is a digit. For example,
A123-4567-8901-23. The program reprompts until a valid number, including dashes, is entered.
4. For these inputs to be consistent, the first letter of the last name must match the first letter of the driver's license number. Case is ignored, so a last name of “el Guapo” would match a driver's license number beginning with “E”. Also, the last 2 digits of the year of birth are encoded in the driver's license number (denoted by Y): LDDD-DDDY-YDDD-DD. So A123-4567-8901-23 would belong to a person born in 1978. If the last name, year of birth, and driver's license number are consistent the program prints consistent; otherwise, it prints fraudulent.
Sample Run
License Validator
Enter a last name: ab bA
Enter a last name: ab bA
Enter year of birth: a12w
Enter year of birth: 1979
Enter driver's license number: A123-4567-9801-23
Consistent
Explanation / Answer
import java.util.Scanner;
public class LicenseValidator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String lastName;
String yearOfBirth;
String dln;
do {
System.out.print("Enter last name: ");
lastName = in.nextLine();
} while (!isValidLastName(lastName));
do {
System.out.print("Enter year of birth: ");
yearOfBirth = in.nextLine();
} while (!isValidYearOfBirth(yearOfBirth));
do {
System.out.print("Enter driver's license number: ");
dln = in.nextLine();
} while (!isValidDln(dln));
if (isConsistentDln(dln, lastName, yearOfBirth))
System.out.println("Consistent");
else
System.out.println("Fraudulent");
}
//1. contains only letters and spaces
//2. begins and ends with a letter
//3. does not contain 2 consecutive spaces
private static boolean isValidLastName(String lName) {
if (!Character.isLetter(lName.charAt(0))
|| !Character.isLetter(lName.charAt(lName.length() - 1))) //2.
return false;
for (int i = 1; i < lName.length() - 1; i++) {
if (!Character.isLetter(lName.charAt(i))) {
if (lName.charAt(i) != ' ') //1.
return false;
else if (lName.charAt(i + 1) == ' ') //3.
return false;
}
}
return true;
}
//integers between 1900 and 1999
// string length = 4
// format: 19DD (D: digit number)
private static boolean isValidYearOfBirth(String yearStr) {
if (yearStr.length() != 4)
return false;
if (yearStr.charAt(0) != '1' || yearStr.charAt(1) != '9')
return false;
if (!Character.isDigit(yearStr.charAt(2))
|| !Character.isDigit(yearStr.charAt(3)))
return false;
return true;
}
//LDDD-DDDD-DDDD-DD
//01234567890123456
//1. length 17
//2. dash character at char 4 9 14
//3. L: upper case letter
//4. D: digit
private static boolean isValidDln(String dln) {
if (dln.length() != 17) //1.
return false;
if (!Character.isUpperCase(dln.charAt(0))) //3.
return false;
for (int i = 1; i < dln.length(); i++) {
if (i == 4 || i == 9 || i == 14) {
if (dln.charAt(i) != '-') //2.
return false;
}
else {
if (!Character.isDigit(dln.charAt(i))) //4.
return false;
}
}
return true;
}
//1. 1st letter of lastName must match 1st letter of dln
//2. LDDD-DDDY-YDDD-DD
// YY = last 2 digits of the year of birth
private static boolean isConsistentDln(
String dln, String lName, String yearStr) {
if (dln.charAt(0) != Character.toUpperCase(lName.charAt(0))) //1.
return false;
if (dln.charAt(8) != yearStr.charAt(2)
|| dln.charAt(10) != yearStr.charAt(3)) //2.
return false;
return true;
}
}