Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.30 KB | None | 0 0
  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5.  
  6. public class Person {
  7.     private String firstname;
  8.     private String lastname;
  9.     private int age;
  10.     private String date;
  11.  
  12.     SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
  13.  
  14.     public Person() {
  15.         firstname = "";
  16.         lastname = "";
  17.         age = 0;
  18.         date = "";
  19.     }
  20.  
  21.     public boolean validateFirstName(String name) {
  22.         if (name.equals("") || name.equals(null) || name.length() < 2) {
  23.             return false;
  24.         } else {
  25.             if (validateFirstNameFormat(name)) {
  26.                 return true;
  27.             } else {
  28.                 return false;
  29.             }
  30.         }
  31.     }
  32.  
  33.     public String formatFirstName(String s) {
  34.         return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
  35.     }
  36.  
  37.     public boolean validateFirstNameFormat(String s) {
  38.         for (int i = 0; i < s.length() - 1; i++) {
  39.             if (!Character.isLetter(s.charAt(i))) {
  40.                 return false;
  41.             }
  42.         }
  43.         return true;
  44.     }
  45.  
  46.     public boolean validateLastName(String name) {
  47.         if (name.equals("") || name.equals(null) || name.length() < 2) {
  48.             return false;
  49.         } else {
  50.             if (validateLastNameFormat(name)) {
  51.                 return true;
  52.             } else {
  53.                 return false;
  54.             }
  55.         }
  56.     }
  57.  
  58.     public String formatLastName(String s) {
  59.         int position = -1;
  60.         for (int i = 0; i < s.length() - 1; i++) {
  61.             if (s.charAt(i) == '-') {
  62.                 position = i;
  63.                 break;
  64.             }
  65.         }
  66.         if (position != -1) {
  67.             return s.substring(0, 1).toUpperCase() + s.substring(1, position).toLowerCase() + "-"
  68.                     + s.substring(position + 1, position + 2).toUpperCase() + s.substring(position + 2).toLowerCase();
  69.         } else {
  70.             return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
  71.         }
  72.     }
  73.  
  74.     public boolean validateLastNameFormat(String s) {
  75.         int counter = 0;
  76.         int position = -1;
  77.         for (int i = 0; i < s.length(); i++) {
  78.             if (s.charAt(i) == '-') {
  79.                 counter++;
  80.                 position = i;
  81.             }
  82.         }
  83.         if (counter == 0) {
  84.             for (int i = 0; i < s.length(); i++) {
  85.                 if (!Character.isLetter(s.charAt(i))) {
  86.                     return false;
  87.                 }
  88.             }
  89.             return true;
  90.         } else if (counter == 1 && position != 0 && position != s.length() - 1) {
  91.             for (int i = 0; i < s.length(); i++) {
  92.                 if (!Character.isLetter(s.charAt(i)) && i != position) {
  93.                     return false;
  94.                 }
  95.             }
  96.             return true;
  97.         } else {
  98.             return false;
  99.         }
  100.  
  101.     }
  102.  
  103.     public boolean validateAge(String s) throws NumberFormatException {
  104.         try {
  105.             if (Integer.parseInt(s) <= 0) {
  106.                 return false;
  107.             } else {
  108.                 return true;
  109.             }
  110.         } catch (NumberFormatException e) {
  111.             return false;
  112.         }
  113.     }
  114.  
  115.     public boolean validateBirthdayDate(String s) {
  116.  
  117.         try {
  118.             dateFormat.setLenient(false);
  119.             dateFormat.parse(s);
  120.             return true;
  121.         } catch (ParseException e) {
  122.             return false;
  123.         }
  124.     }
  125.  
  126.     public boolean validateAgeAndBirthdayDate() throws ParseException {
  127.         if (date != null) {
  128.             try {
  129.                 Date birthdayDate = dateFormat.parse(date);
  130.                 if (age != getAges(birthdayDate)) {
  131.                     return false;
  132.                 } else {
  133.                     return true;
  134.                 }
  135.             } catch (ParseException e) {
  136.                 return false;
  137.             }
  138.         } else {
  139.             return false;
  140.         }
  141.     }
  142.  
  143.     private static int getAges(Date birthdayDate) {
  144.         Date todayDate = new Date();
  145.         Calendar birthdayCal = Calendar.getInstance();
  146.         birthdayCal.setTime(birthdayDate);
  147.         Calendar todayCal = Calendar.getInstance();
  148.         todayCal.setTime(todayDate);
  149.         int difference = todayCal.get(Calendar.YEAR) - birthdayCal.get(Calendar.YEAR);
  150.         if (birthdayCal.get(Calendar.MONTH) > todayCal.get(Calendar.MONTH)
  151.                 || (birthdayCal.get(Calendar.MONTH) == todayCal.get(Calendar.MONTH)
  152.                         && birthdayCal.get(Calendar.DAY_OF_MONTH) > todayCal.get(Calendar.DAY_OF_MONTH))) {
  153.             difference--;
  154.         }
  155.         return difference;
  156.     }
  157.  
  158.     public String getFirstname() {
  159.         return firstname;
  160.     }
  161.  
  162.     public void setFirstname(String firstname) {
  163.         this.firstname = formatFirstName(firstname);
  164.     }
  165.  
  166.     public String getLastname() {
  167.         return lastname;
  168.     }
  169.  
  170.     public void setLastname(String lastname) {
  171.         this.lastname = formatLastName(lastname);
  172.     }
  173.  
  174.     public int getAge() {
  175.         return age;
  176.     }
  177.  
  178.     public void setAge(String age) {
  179.         this.age = Integer.parseInt(age);
  180.     }
  181.  
  182.     public String getDate() {
  183.         return date;
  184.     }
  185.  
  186.     public void setDate(String date) throws ParseException {
  187.         this.date = dateFormat.format(dateFormat.parse(date));
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement