Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Person.java - abstract class
- * bank clients should be derived from this class
- */
- package BankClients;
- import java.time.LocalDate;
- import BankExceptions.BankIdException;
- import BankExceptions.BankNameException;
- import BankPkg.BankUtils;
- public abstract class Person {
- private String name;
- private int id;
- private LocalDate birthDate;
- public Person(String name, int id, LocalDate birthDate) throws Exception {
- super();
- if (!BankUtils.isValidName(name))
- throw new BankNameException(name);
- if (!BankUtils.isValidId(id)) {
- throw new BankIdException(id);
- }
- this.name = name;
- this.id = id;
- this.birthDate = birthDate.plusDays(0); // to clone date object
- }
- public Person(Person other) {
- this.id = other.id;
- this.name = other.name;
- this.birthDate = other.birthDate.plusDays(0);
- }
- public String getName() {
- return this.name;
- }
- public int getId() {
- return this.id;
- }
- public LocalDate getBirthDate() {
- return this.birthDate.plusDays(0); // return a cloned object
- }
- public int getAge() {
- return (int) BankUtils.getAge(this.birthDate);
- }
- public int getAge(LocalDate atDate) {
- return (int) BankUtils.getAge(this.birthDate, atDate);
- }
- public void setName(String name) throws Exception {
- if (!BankUtils.isValidName(name)) {
- throw new BankNameException(this.name, name);
- }
- this.name = name;
- }
- protected String feildsAsString() {
- return "name=" + name + ", id=" + id + ", birthDate=" + birthDate;
- }
- @Override
- public String toString() {
- return this.getClass().getSimpleName() + " [" + feildsAsString() + "]";
- }
- @Override
- public boolean equals(Object obj) {
- if (obj instanceof Person) {
- Person tmpPerson = (Person) obj;
- return this.id == tmpPerson.id && this.name.equals(tmpPerson.name)
- && this.birthDate.equals(tmpPerson.birthDate);
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment