Advertisement
Braber01

Braber01

Sep 26th, 2010
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. package people;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.*;
  5.  
  6. public abstract class Person
  7. {
  8.     protected String first,last,email;
  9.  
  10.     public Person()
  11.     {
  12.         setFirst("");
  13.         setLast("");
  14.         setEmail("");
  15.     }//Person()
  16.  
  17.     public Person(String first,String last,String email)
  18.     {
  19.         setFirst(first);
  20.         setLast(last);
  21.         setEmail(email);
  22.     }//Person(String,String,String)
  23.  
  24.     public abstract String getDisplayText();
  25.  
  26.     public String getFirst(){return this.first;}
  27.     public void setFirst(String first){this.first=first;}
  28.    
  29.     public String getLast(){return this.last;}
  30.     public void setLast(String last){this.last=last;}
  31.  
  32.     public String getEmail(){return this.email;}
  33.     public void setEmail(String email){this.email=email;}
  34.  
  35.     @Override
  36.     public String toString()
  37.     {
  38.         String msg = "Name: " + this.getFirst() + " " + this.getLast() +
  39.             "\n" + "Email: " + this.getEmail();
  40.         return msg;
  41.     }//toString()
  42. }//end class Person
  43.  
  44. class Customer extends Person
  45. {
  46.     private String num;
  47.    
  48.     public Customer()
  49.     {
  50.         super();
  51.         setNum("");
  52.     }//Customer()
  53.  
  54.     public Customer(String first,String last,String email,String num)
  55.     {
  56.         super(first,last,email);
  57.         setNum(num);
  58.     }//Customer(String,String,String,String)
  59.  
  60.     public String getNum(){return this.num;}
  61.     public void setNum(String num){this.num=num;}
  62.  
  63.     @Override
  64.     public String toString()
  65.     {
  66.         String msg = "Name: " + this.getFirst() + " " + this.getLast() +
  67.             "\n" + "Email: " + this.getEmail() + "\n" +
  68.             "Customer Number: " + this.getNum();
  69.         return msg;
  70.     }//toString()
  71.  
  72.     @Override
  73.     public String getDisplayText()
  74.     {
  75.         return this.toString();
  76.     }//getDisplayText()
  77. }//end class Customer
  78.  
  79. class Employee extends Person
  80. {
  81.     private String SSN;
  82.  
  83.     public Employee()
  84.     {
  85.         super();
  86.         setSSN();
  87.     }//Employee()
  88.  
  89.     public Employee(String first,String last,String email,String SSN)
  90.     {
  91.         super(first,last,email,SSN);
  92.         setSSN(SSN);
  93.     }//Employee(String,String,String,String)
  94.  
  95.     public String getSSN(){return this.SSN;}
  96.     public void setSSN(String SSN){this.SSN=SSN;}
  97.  
  98.     @Override
  99.     public String toString()
  100.     {
  101.         String msg = "Name: " + this.getFirst() + this.getLast() + "\n" +
  102.             "Email: " + this.getEmail() + "\n" +
  103.             "SSN: " + this.getSSN();
  104.         return msg;
  105.     }
  106.  
  107.     @Override
  108.     public String getDisplayText()
  109.     {
  110.         return this.toString();
  111.     }
  112. }//end class Employee
  113.  
  114. class Validator
  115. {
  116.     public static boolean isEmail(String email)
  117.     {
  118.         final String VALID_EMAIL_PATTERN =
  119.             "^[A-Za-z]+[A-Za-z0-9_-]*@[A-Za-z]+\\p{Punct}[A-Za-z]+$";
  120.         Pattern p = Pattern.compile(VALID_EMAIL_PATTERN);
  121.         Matcher m = p.matcher(email);
  122.         return mmatches();
  123.     }//isEmail(String)
  124.  
  125.     public static String getEmail(Scanner sc,String prompt)
  126.     {
  127.         System.out.print(prompt);
  128.         String email = sc.nextLine();
  129.         while(!isEmail(email))
  130.         {
  131.             System.err.println("Error! Invalid E-mail Pattern.");
  132.             email = sc.nextLine();
  133.         }//end while
  134.         return email;
  135.     }//getEmail(Scanner,String)
  136.  
  137.     public static boolean isSSN(String SSN)
  138.     {
  139.         final String VALID_SSN="^\\d{3}-\\d{2}-\\d{4}$";
  140.         Pattern p = Pattern.compile(VALID_SSN);
  141.         Matcher m = p.matcher(SSN);
  142.         return m.matches();
  143.     }//isSSN(String)
  144.    
  145.     public static String getSSN(Scanner sc,String prompt)
  146.     {
  147.         System.out.print(prompt);
  148.         String SSN = sc.nextLine();
  149.         while(!isSSN(SSN))
  150.         {
  151.             System.err.println("Error! Invalid SSN Format.");
  152.             System.out.print(prompt);
  153.             SSN = sc.nextLine();
  154.         }
  155.         return SSN;
  156.     }//getSSN(Scanner,String)
  157. }//end Class Validator
  158.  
  159. class PeopleApp
  160. {
  161.     public static void main(String[] args)
  162.     {
  163.         String first,last,email,SSN;
  164.        
  165.         System.out.println("Welcome to Braber01's People Tester.");
  166.         System.out.println();
  167.        
  168.         Scanner sc1 = new Scanner(System.in);
  169.    
  170.         do
  171.         {
  172.             System.out.println("Choose a Person type:(c/e)");
  173.             String type=sc1.next();
  174.             if(type.equalsIgnoreCase("C"))
  175.             {
  176.                 System.out.print("Enter First name: ");
  177.                 first = sc1.next();
  178.                 System.out.println();
  179.                 System.out.print("Enter Last Name: ");
  180.                 last = sc.next();
  181.                 System.out.println();
  182.             email = Validator.getEmail(new Scanner(System.in),"Enter email: ");
  183.                 System.out.print("Enter Customer Number: ");
  184.                 num = sc1.next();
  185.                 Customer c = new Customer(first,last,email,num);
  186.                 System.out.println(c.getDisplayText());
  187.             }else if(sc.next().equalsIgnoreCase("E"))
  188.             {
  189.                 System.out.print("Enter first name: ");
  190.                 first = sc1.next();
  191.                 System.out.println();
  192.                 System.out.print("Enter last name: ");
  193.                 last = sc1.next();
  194.                 System.out.println();
  195.             email = Validator.getEmail(new Scanner(System.in),"Enter email: ");
  196.                 System.out.println();
  197.             SSN = Validator.getSSN(new Scanner(System.in),"Enter SSN: ");
  198.                 Employee e = new Employee(first,last,email,SSN);
  199.                 System.out.println(e.getDisplayText());
  200.    
  201.             }else
  202.                 System.err.println("Error! Invalid Choice choose Again.");
  203.            
  204.             System.out.print("Add another Object?(y/n): ");
  205.         }while(sc.next().equalsIgnoreCase("Y"));
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement