In: Computer Science
OOPDA in java project. in eclipse
Instructions:
Create a class called
Person
with the properties listed:
int id, String firstName, String middleName, String lastName,
String email, String ssn, int age.
The
Person class should have getters
and
setters for all properties
other
than
id. (You may use Eclipse to help you auto-generate
these.)
Person class should also have a getter for id
Create a no-arg constructor for Person
In addition to these getters and setters, Person should have
the
following
instance methods:
String toString() – returns full name, like "Edgar Allen Poe"
String
getEmailDomain()
– returns the domain portion of an email (after
the
@), as in "gmail.com"
String getLast4SSN()
– returns the last 4 digits of the Social
Security
Number
Add validation checking methods
static boolean validAge(String testAge)
You will have to convert the testAge String to an int to store it after validation
static boolean validEmail(String testEmail)
static boolean validSSN(String testSSN)
for the age, email and ssn.You may have done this via the setters in the past. However, this time, let's make them class-level (static) methods to validate these fields as best you can.
Give some thought to what advantages this approach would have.
Notes on validation.
Here is a list of validation checks you should perform. Some are easier to implement than others, and there are many ways to correctly validate these aspects of the fields. Implement as many of these checks as you can.
You may accept any value for the names
Extra Credit: Is there a good way to validate a name? Do a little research on topic and include your answer. If there is, include your algorithm.
Age entered by user
The String entered must be an integer greater than 16
If the String is greater than 100 you should log an info message
Email address string entered by user
String must contain a '@' and at least one '.’
A '.' must follow the '@’
String must only contain one '@'
The domain name (the part after ‘@’ but before the first ‘.’ )must be at least one character in length
Clearly there are far more restrictions on email addresses than those listed above. See if you can think of any others that might be applicable. I will give out extra credit for especially clever email validation tests. Make sure to document and comment your ideas.
Social Security Number string entered by user
String must contain two '-' characters in the correct position
The length of the SSN must be 11 total characters
String must contain only digits or numbers
If you encounter invalid data for any of these fields, log it using the logging API of Java. (Do not use System.out.println(); )
Static variables: Keep track of the highest age entered for a Person in a static variable.
Create a driver program PersonApp to test the class
Prompt user for all Person properties except id. You can hardcode any id you like, but use the id as the HashMap key
Accept any value for the name field
Only accept valid values for age, email and ssn
Create a Person with the valid input
Create subclasses for Instructor and Student
Instructors should have a Department
Students should have a Major
Generate getters and setters
Add the Person, the Student and the Instructor to a HashMap.
In the Driver, create a Student & an Instructor (Hardcoded, do not prompt for Input)
Add each of these three instances (the hardcoded Student & Instructor, and previously created Person ) to a HashMap using the id as the key
Perform the following after creating each person in the Driver:
Loop through the HashMap and display
The person's full name and what kind of Person are they
The person's email domain
The last 4 digits of the person's Social Security Number
Whether the person is the oldest in the collection
The major for the Student, the department for the Instructor
Here is some sample output, to help you understand the end goal of the exercise (your prompts may look different than mine, don’t worry about that as long as they are clear)
Enter person's first name
Julie
Enter
person's middle name
Theresa
Enter
person's last name
Collins
Enter
person's email address
[email protected]
Enter
person's SSN in ###-##-#### format
123-45-6789
Enter
person's age
47
Julie
Theresa Collins (Person)
acme.com
6789
Oldest
Jane Marie Doe (Student)
students.rowan.edu
4444
not
oldest
Computer Science
Stephen
J. Hartley (Instructor)
rowan.edu
5555
not
oldest
Math/Science
/*************************Person.java**********************/
package person;
// TODO: Auto-generated Javadoc
/**
* The Class Person.
*/
public class Person {
/** The id. */
private int id;
/** The first name. */
private String firstName;
/** The middle name. */
private String middleName;
/** The last name. */
private String lastName;
/** The email. */
private String email;
/** The ssn. */
private String ssn;
/** The age. */
private int age;
/** The max age. */
public static int maxAge = 0;
/**
* Instantiates a new person.
*/
public Person() {
this.id = 0;
this.firstName = "";
this.middleName = "";
this.lastName = "";
this.email = "";
this.ssn = "";
this.age = 0;
}
/**
* Instantiates a new person.
*
* @param id the id
* @param firstName the first name
* @param middleName the middle name
* @param lastName the last name
* @param email the email
* @param ssn the ssn
* @param age the age
*/
public Person(int id, String firstName, String
middleName, String lastName, String email, String ssn, int age)
{
super();
this.id = id;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.email = email;
this.ssn = ssn;
if (age > maxAge) {
maxAge =
age;
}
this.age = age;
}
/**
* Gets the first name.
*
* @return the first name
*/
public String getFirstName() {
return firstName;
}
/**
* Sets the first name.
*
* @param firstName the new first name
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Gets the middle name.
*
* @return the middle name
*/
public String getMiddleName() {
return middleName;
}
/**
* Sets the middle name.
*
* @param middleName the new middle name
*/
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
/**
* Gets the last name.
*
* @return the last name
*/
public String getLastName() {
return lastName;
}
/**
* Sets the last name.
*
* @param lastName the new last name
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* Gets the email.
*
* @return the email
*/
public String getEmail() {
return email;
}
/**
* Sets the email.
*
* @param email the new email
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Gets the ssn.
*
* @return the ssn
*/
public String getSsn() {
return ssn;
}
/**
* Sets the ssn.
*
* @param ssn the new ssn
*/
public void setSsn(String ssn) {
this.ssn = ssn;
}
/**
* Gets the age.
*
* @return the age
*/
public int getAge() {
return age;
}
/**
* Sets the age.
*
* @param age the new age
*/
public void setAge(int age) {
if (age > maxAge) {
maxAge =
age;
}
this.age = age;
}
/**
* Gets the id.
*
* @return the id
*/
public int getId() {
return id;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return firstName + " " + middleName
+ " " + lastName;
}
/**
* Gets the email domain.
*
* @return the email domain
*/
public String getEmailDomain() {
return "@g m ail. c om
";//rearrange this
}
/**
* Gets the last 4 SSN.
*
* @return the last 4 SSN
*/
public String getLast4SSN() {
return
ssn.substring(ssn.length() - 4, ssn.length());
}
/**
* Valid age.
*
* @param testAge the test age
* @return true, if successful
*/
public static boolean validAge(String testAge) {
int age = Integer.parseInt(testAge);
if (age > 16 && age
< 120) {
if (age >
100) {
System.out.println("Person age is above
100");
}
return
true;
} else {
return
false;
}
}
/**
* Valid email.
*
* @param testEmail the test email
* @return true, if successful
*/
public static boolean validEmail(String testEmail)
{
String validator =
"[A-Z]+[a-zA-Z_]+@\b([a-zA-Z]+.){2}\b?.[a-zA-Z]+";
if (testEmail.matches(validator))
{
return
true;
} else {
return
false;
}
}
/**
* Valid SSN.
*
* @param testSSN the test SSN
* @return true, if successful
*/
public static boolean validSSN(String testSSN) {
/*
* ^\\d{3}: Starts with three
numeric digits. [- ]?: Followed by an optional "-"
* \\d{2}: Two numeric digits after
the optional "-" [- ]?: May contain an
* optional second "-" character.
\\d{4}: ends with four numeric digits.
*/
String validator = "^\\d{3}[-
]?\\d{2}[- ]?\\d{4}$";
if (testSSN.matches(validator))
{
return
true;
} else {
return
false;
}
}
}
/*********************Driver.java*********************/
package person;
import java.util.HashMap;
import java.util.Scanner;
/**
* The Class Driver.
*/
public class Driver {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
HashMap<Integer, Person>
hm = new HashMap<Integer, Person>();
Scanner scan = new
Scanner(System.in);
hm.put(101, new Person(101, "Jane",
"Marie", "Doe", "students.rowan.edu", "111-22-4444", 25));
hm.put(102, new Person(102,
"Stephen", "J.", "Hartley", "rowan.edu", "222-33-5555", 35));
System.out.println("Enter
person's first name");
String firstName =
scan.nextLine();
System.out.println("Enter person's
middle name");
String middleName =
scan.nextLine();
System.out.println("Enter person's
last name");
String lastName =
scan.nextLine();
System.out.println("Enter person's
email address");
String testEmail =
scan.nextLine();
System.out.println("Enter person's
SSN in ###-##-#### format");
String testSSN =
scan.nextLine();
System.out.println("Enter person's
age");
String testAge =
scan.nextLine();
int age = 0;
String email = null;
String ssn = null;
if (Person.validAge(testAge)) {
age =
Integer.parseInt(testAge);
}
if (Person.validEmail(testEmail))
{
email =
testEmail;
}
if (Person.validSSN(testSSN)) {
ssn =
testSSN;
}
hm.put(103, new Person(103, firstName, middleName, lastName, email, ssn, age));
for (int key : hm.keySet())
{
String ageFlag =
"";
if(hm.get(key).getAge()==Person.maxAge) {
ageFlag = "Oldest";
}
else {
ageFlag = "Not oldest";
}
System.out.println(hm.get(key).toString()+"\n"+hm.get(key).getEmail()+"\n"+hm.get(key).getLast4SSN()+"\n"+ageFlag);
System.out.println();
}
}
}
/******************output********************/
Enter person's first name
Julie
Enter person's middle name
Theresa
Enter person's last name
Collins
Enter person's email address
[email protected]
Enter person's SSN in ###-##-#### format
123-45-6789
Enter person's age
47
Jane Marie Doe
students.rowan.edu
4444
Not oldest
Stephen J. Hartley
rowan.edu
5555
Not oldest
Julie Theresa Collins
null
6789
Oldest
Please let me know if you have any doubt or modify the answer, Thanks :)