Advertisement
Braber01

Braber01

Sep 25th, 2010
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 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. class Customer extends Person
  44. {
  45.     private String num;
  46.    
  47.     public Customer()
  48.     {
  49.         super();
  50.         setNum("");
  51.     }//Customer()
  52.  
  53.     public Customer(String first,String last,String email,String num)
  54.     {
  55.         super(first,last,email);
  56.         setNum(num);
  57.     }//Customer(String,String,String,String)
  58.  
  59.     public String getNum(){return this.num;}
  60.     public void setNum(String num){this.num=num;}
  61.  
  62.     @Override
  63.     public String toString()
  64.     {
  65.         String msg = "Name: " + this.getFirst() + " " + this.getLast() +
  66.             "\n" + "Email: " + this.getEmail() + "\n" +
  67.             "Customer Number: " + this.getNum();
  68.         return msg;
  69.     }//toString()
  70.  
  71.     @Override
  72.     public String getDisplayText()
  73.     {
  74.         return this.toString();
  75.     }//getDisplayText()
  76. }//end class Customer
  77. class Employee extends Person
  78. {
  79.     private String SSN;
  80.  
  81.     public Employee()
  82.     {
  83.         super();
  84.         setSSN();
  85.     }//Employee()
  86.  
  87.     public Employee(String first,String last,String email,String SSN)
  88.     {
  89.         super(first,last,email,SSN);
  90.         setSSN(SSN);
  91.     }//Employee(String,String,String,String)
  92.  
  93.     public String getSSN(){return this.SSN;}
  94.     public void setSSN(String SSN){this.SSN=SSN;}
  95.  
  96.     @Override
  97.     public String toString()
  98.     {
  99.         String msg = "Name: " + this.getFirst() + this.getLast() + "\n" +
  100.             "Email: " + this.getEmail() + "\n" +
  101.             "SSN: " + this.getSSN();
  102.         return msg;
  103.     }
  104.  
  105.     @Override
  106.     public String getDisplayText()
  107.     {
  108.         return this.toString();
  109.     }
  110. }//end class Employee
  111. class Validator
  112. {
  113.     public static boolean isEmail(String email)
  114.     {
  115.         final String VALID_EMAIL_PATTERN =
  116.             "^[A-Za-z]+[A-Za-z0-9_-]*@[A-Za-z]+\\p{Punct}[A-Za-z]+$";
  117.         Pattern p = Pattern.compile(VALID_EMAIL_PATTERN);
  118.         Matcher m = p.matcher(email);
  119.         return mmatches();
  120.     }//isEmail(String)
  121.  
  122.     public static String getEmail(Scanner sc,String prompt)
  123.     {
  124.         System.out.print(prompt);
  125.         String email = sc.nextLine();
  126.         while(!isEmail(email))
  127.         {
  128.             System.err.println("Error! Invalid E-mail Pattern.");
  129.             email = sc.nextLine();
  130.         }//end while
  131.         return email;
  132.     }//getEmail(Scanner,String)
  133.  
  134.     public static boolean isSSN(String SSN)
  135.     {
  136.         final String VALID_SSN="^\\d{3}-\\d{2}-\\d{4}$";
  137.         Pattern p = Pattern.compile(VALID_SSN);
  138.         Matcher m = p.matcher(SSN);
  139.         return m.matches();
  140.     }//isSSN(String)
  141.    
  142.     public static String getSSN(Scanner sc,String prompt)
  143.     {
  144.         System.out.print(prompt);
  145.         String SSN = sc.nextLine();
  146.         while(!isSSN(SSN))
  147.         {
  148.             System.err.println("Error! Invalid SSN Format.");
  149.             System.out.print(prompt);
  150.             SSN = sc.nextLine();
  151.         }
  152.         return SSN;
  153.     }//getSSN(Scanner,String)
  154. }//end Class Validator
  155. class PeopleApp
  156. {
  157.     public static void main(String[] args)
  158.     {
  159.         @SupressWarnings("unused")
  160.         String first,last,email,SSN;
  161.        
  162.         System.out.println("Welcome to Braber01's People Tester.");
  163.         System.out.println();
  164.        
  165.         Scanner sc1 = new Scanner(System.in);
  166.    
  167.         do
  168.         {
  169.             System.out.println("Choose a Person type:(c/e)");
  170.             String type=sc.next();
  171.             if(type.equalsIgnoreCase("C"))
  172.             {
  173.                 System.out.print("Enter First name: ");
  174.                 first = sc.nextLine();
  175.                 System.out.println();
  176.                 System.out.print("Enter Last Name: ");
  177.                 last = sc.nextLine();
  178.                 System.out.println();
  179.             email = Validator.getEmail(new Scanner(System.in),"Enter email: ");
  180.                 System.out.print("Enter Customer Number: ");
  181.                 num = sc.nextLine();
  182.                 Customer c = new Customer(first,last,email,num);
  183.                 c.getDisplayText();
  184.             }
  185.             System.out.print("Add another Object?(y/n): ");
  186.         }while(sc.next().equalsIgnoreCase("Y"));
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement