Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- public class Person {
- private String firstname;
- private String lastname;
- private int age;
- private String date;
- SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
- public Person() {
- firstname = "";
- lastname = "";
- age = 0;
- date = "";
- }
- public boolean validateFirstName(String name) {
- if (name.equals("") || name.equals(null) || name.length() < 2) {
- return false;
- } else {
- if (validateFirstNameFormat(name)) {
- return true;
- } else {
- return false;
- }
- }
- }
- public String formatFirstName(String s) {
- return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
- }
- public boolean validateFirstNameFormat(String s) {
- for (int i = 0; i < s.length() - 1; i++) {
- if (!Character.isLetter(s.charAt(i))) {
- return false;
- }
- }
- return true;
- }
- public boolean validateLastName(String name) {
- if (name.equals("") || name.equals(null) || name.length() < 2) {
- return false;
- } else {
- if (validateLastNameFormat(name)) {
- return true;
- } else {
- return false;
- }
- }
- }
- public String formatLastName(String s) {
- int position = -1;
- for (int i = 0; i < s.length() - 1; i++) {
- if (s.charAt(i) == '-') {
- position = i;
- break;
- }
- }
- if (position != -1) {
- return s.substring(0, 1).toUpperCase() + s.substring(1, position).toLowerCase() + "-"
- + s.substring(position + 1, position + 2).toUpperCase() + s.substring(position + 2).toLowerCase();
- } else {
- return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
- }
- }
- public boolean validateLastNameFormat(String s) {
- int counter = 0;
- int position = -1;
- for (int i = 0; i < s.length(); i++) {
- if (s.charAt(i) == '-') {
- counter++;
- position = i;
- }
- }
- if (counter == 0) {
- for (int i = 0; i < s.length(); i++) {
- if (!Character.isLetter(s.charAt(i))) {
- return false;
- }
- }
- return true;
- } else if (counter == 1 && position != 0 && position != s.length() - 1) {
- for (int i = 0; i < s.length(); i++) {
- if (!Character.isLetter(s.charAt(i)) && i != position) {
- return false;
- }
- }
- return true;
- } else {
- return false;
- }
- }
- public boolean validateAge(String s) throws NumberFormatException {
- try {
- if (Integer.parseInt(s) <= 0) {
- return false;
- } else {
- return true;
- }
- } catch (NumberFormatException e) {
- return false;
- }
- }
- public boolean validateBirthdayDate(String s) {
- try {
- dateFormat.setLenient(false);
- dateFormat.parse(s);
- return true;
- } catch (ParseException e) {
- return false;
- }
- }
- public boolean validateAgeAndBirthdayDate() throws ParseException {
- if (date != null) {
- try {
- Date birthdayDate = dateFormat.parse(date);
- if (age != getAges(birthdayDate)) {
- return false;
- } else {
- return true;
- }
- } catch (ParseException e) {
- return false;
- }
- } else {
- return false;
- }
- }
- private static int getAges(Date birthdayDate) {
- Date todayDate = new Date();
- Calendar birthdayCal = Calendar.getInstance();
- birthdayCal.setTime(birthdayDate);
- Calendar todayCal = Calendar.getInstance();
- todayCal.setTime(todayDate);
- int difference = todayCal.get(Calendar.YEAR) - birthdayCal.get(Calendar.YEAR);
- if (birthdayCal.get(Calendar.MONTH) > todayCal.get(Calendar.MONTH)
- || (birthdayCal.get(Calendar.MONTH) == todayCal.get(Calendar.MONTH)
- && birthdayCal.get(Calendar.DAY_OF_MONTH) > todayCal.get(Calendar.DAY_OF_MONTH))) {
- difference--;
- }
- return difference;
- }
- public String getFirstname() {
- return firstname;
- }
- public void setFirstname(String firstname) {
- this.firstname = formatFirstName(firstname);
- }
- public String getLastname() {
- return lastname;
- }
- public void setLastname(String lastname) {
- this.lastname = formatLastName(lastname);
- }
- public int getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = Integer.parseInt(age);
- }
- public String getDate() {
- return date;
- }
- public void setDate(String date) throws ParseException {
- this.date = dateFormat.format(dateFormat.parse(date));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement