Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package people;
- import java.util.Scanner;
- import java.util.regex.*;
- public abstract class Person
- {
- protected String first,last,email;
- public Person()
- {
- setFirst("");
- setLast("");
- setEmail("");
- }//Person()
- public Person(String first,String last,String email)
- {
- setFirst(first);
- setLast(last);
- setEmail(email);
- }//Person(String,String,String)
- public abstract String getDisplayText();
- public String getFirst(){return this.first;}
- public void setFirst(String first){this.first=first;}
- public String getLast(){return this.last;}
- public void setLast(String last){this.last=last;}
- public String getEmail(){return this.email;}
- public void setEmail(String email){this.email=email;}
- @Override
- public String toString()
- {
- String msg = "Name: " + this.getFirst() + " " + this.getLast() +
- "\n" + "Email: " + this.getEmail();
- return msg;
- }//toString()
- }//end class Person
- class Customer extends Person
- {
- private String num;
- public Customer()
- {
- super();
- setNum("");
- }//Customer()
- public Customer(String first,String last,String email,String num)
- {
- super(first,last,email);
- setNum(num);
- }//Customer(String,String,String,String)
- public String getNum(){return this.num;}
- public void setNum(String num){this.num=num;}
- @Override
- public String toString()
- {
- String msg = "Name: " + this.getFirst() + " " + this.getLast() +
- "\n" + "Email: " + this.getEmail() + "\n" +
- "Customer Number: " + this.getNum();
- return msg;
- }//toString()
- @Override
- public String getDisplayText()
- {
- return this.toString();
- }//getDisplayText()
- }//end class Customer
- class Employee extends Person
- {
- private String SSN;
- public Employee()
- {
- super();
- setSSN();
- }//Employee()
- public Employee(String first,String last,String email,String SSN)
- {
- super(first,last,email,SSN);
- setSSN(SSN);
- }//Employee(String,String,String,String)
- public String getSSN(){return this.SSN;}
- public void setSSN(String SSN){this.SSN=SSN;}
- @Override
- public String toString()
- {
- String msg = "Name: " + this.getFirst() + this.getLast() + "\n" +
- "Email: " + this.getEmail() + "\n" +
- "SSN: " + this.getSSN();
- return msg;
- }
- @Override
- public String getDisplayText()
- {
- return this.toString();
- }
- }//end class Employee
- class Validator
- {
- public static boolean isEmail(String email)
- {
- final String VALID_EMAIL_PATTERN =
- "^[A-Za-z]+[A-Za-z0-9_-]*@[A-Za-z]+\\p{Punct}[A-Za-z]+$";
- Pattern p = Pattern.compile(VALID_EMAIL_PATTERN);
- Matcher m = p.matcher(email);
- return mmatches();
- }//isEmail(String)
- public static String getEmail(Scanner sc,String prompt)
- {
- System.out.print(prompt);
- String email = sc.nextLine();
- while(!isEmail(email))
- {
- System.err.println("Error! Invalid E-mail Pattern.");
- email = sc.nextLine();
- }//end while
- return email;
- }//getEmail(Scanner,String)
- public static boolean isSSN(String SSN)
- {
- final String VALID_SSN="^\\d{3}-\\d{2}-\\d{4}$";
- Pattern p = Pattern.compile(VALID_SSN);
- Matcher m = p.matcher(SSN);
- return m.matches();
- }//isSSN(String)
- public static String getSSN(Scanner sc,String prompt)
- {
- System.out.print(prompt);
- String SSN = sc.nextLine();
- while(!isSSN(SSN))
- {
- System.err.println("Error! Invalid SSN Format.");
- System.out.print(prompt);
- SSN = sc.nextLine();
- }
- return SSN;
- }//getSSN(Scanner,String)
- }//end Class Validator
- class PeopleApp
- {
- public static void main(String[] args)
- {
- @SupressWarnings("unused")
- String first,last,email,SSN;
- System.out.println("Welcome to Braber01's People Tester.");
- System.out.println();
- Scanner sc1 = new Scanner(System.in);
- do
- {
- System.out.println("Choose a Person type:(c/e)");
- String type=sc.next();
- if(type.equalsIgnoreCase("C"))
- {
- System.out.print("Enter First name: ");
- first = sc.nextLine();
- System.out.println();
- System.out.print("Enter Last Name: ");
- last = sc.nextLine();
- System.out.println();
- email = Validator.getEmail(new Scanner(System.in),"Enter email: ");
- System.out.print("Enter Customer Number: ");
- num = sc.nextLine();
- Customer c = new Customer(first,last,email,num);
- c.getDisplayText();
- }
- System.out.print("Add another Object?(y/n): ");
- }while(sc.next().equalsIgnoreCase("Y"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement