mmayoub

Bank, Person abstract class

Aug 10th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. /*
  2.  * Person.java - abstract class
  3.  * bank clients should be derived from this class
  4.  */
  5. package BankClients;
  6.  
  7. import java.time.LocalDate;
  8.  
  9. import BankExceptions.BankIdException;
  10. import BankExceptions.BankNameException;
  11. import BankPkg.BankUtils;
  12.  
  13. public abstract class Person {
  14.     private String name;
  15.     private int id;
  16.     private LocalDate birthDate;
  17.  
  18.     public Person(String name, int id, LocalDate birthDate) throws Exception {
  19.         super();
  20.  
  21.         if (!BankUtils.isValidName(name))
  22.             throw new BankNameException(name);
  23.         if (!BankUtils.isValidId(id)) {
  24.             throw new BankIdException(id);
  25.         }
  26.  
  27.         this.name = name;
  28.         this.id = id;
  29.         this.birthDate = birthDate.plusDays(0); // to clone date object
  30.     }
  31.  
  32.     public Person(Person other) {
  33.         this.id = other.id;
  34.         this.name = other.name;
  35.         this.birthDate = other.birthDate.plusDays(0);
  36.     }
  37.  
  38.     public String getName() {
  39.         return this.name;
  40.     }
  41.  
  42.     public int getId() {
  43.         return this.id;
  44.     }
  45.  
  46.     public LocalDate getBirthDate() {
  47.         return this.birthDate.plusDays(0); // return a cloned object
  48.     }
  49.  
  50.     public int getAge() {
  51.         return (int) BankUtils.getAge(this.birthDate);
  52.     }
  53.  
  54.     public int getAge(LocalDate atDate) {
  55.         return (int) BankUtils.getAge(this.birthDate, atDate);
  56.     }
  57.  
  58.     public void setName(String name) throws Exception {
  59.         if (!BankUtils.isValidName(name)) {
  60.             throw new BankNameException(this.name, name);
  61.  
  62.         }
  63.  
  64.         this.name = name;
  65.     }
  66.  
  67.     protected String feildsAsString() {
  68.         return "name=" + name + ", id=" + id + ", birthDate=" + birthDate;
  69.     }
  70.  
  71.     @Override
  72.     public String toString() {
  73.         return this.getClass().getSimpleName() + " [" + feildsAsString() + "]";
  74.     }
  75.  
  76.     @Override
  77.     public boolean equals(Object obj) {
  78.         if (obj instanceof Person) {
  79.             Person tmpPerson = (Person) obj;
  80.             return this.id == tmpPerson.id && this.name.equals(tmpPerson.name)
  81.                     && this.birthDate.equals(tmpPerson.birthDate);
  82.         }
  83.         return false;
  84.     }
  85. }
Add Comment
Please, Sign In to add comment