Skip to content

Instantly share code, notes, and snippets.

@kshitijvarshne1
Created February 18, 2021 14:10
Show Gist options
  • Save kshitijvarshne1/fbc75ca66f1e1188af5e94a3fc5befe1 to your computer and use it in GitHub Desktop.
Save kshitijvarshne1/fbc75ca66f1e1188af5e94a3fc5befe1 to your computer and use it in GitHub Desktop.
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 18-Feb-21
* Time: 6:35 PM
* File: Person.java
*/
package feb18_21;
public class Person {
private String firstName;
private String lastName;
private int birthMonth;
private int birthDay;
private int birthYear;
public Person(String firstName, String lastName, int birthMonth, int birthDay, int birthYear) {
this.firstName = firstName;
this.lastName = lastName;
this.birthMonth = birthMonth;
this.birthDay = birthDay;
this.birthYear = birthYear;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getBirthMonth() {
return birthMonth;
}
public void setBirthMonth(int birthMonth) {
this.birthMonth = birthMonth;
}
public int getBirthDay() {
return birthDay;
}
public void setBirthDay(int birthDay) {
this.birthDay = birthDay;
}
public int getBirthYear() {
return birthYear;
}
public void setBirthYear(int birthYear) {
this.birthYear = birthYear;
}
public void setBirthDate(int month, int day, int year) {
if ((day >= 1 && day <= 31) && (month >= 1 && month <= 12) && (year >= 1900 && year <= 2019)) {
if (month == 2 && (day >= 1 && day <= 28)) {
this.birthDay = day;
this.birthMonth = month;
this.birthYear = year;
} else {
System.out.println("A/c to Febuary month , day is worng");
}
} else {
System.out.println("Worng values input");
}
}
public int computeAge() {
int currentdate = 5;
int currentMonth = 11;
int currentYear = 2020;
int[] month = {31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31};
if (this.birthDay > currentdate) {
currentMonth = currentMonth - 1;
currentdate = currentdate + month[this.birthMonth - 1];
}
if (this.birthMonth > currentMonth) {
currentYear = currentYear - 1;
currentMonth = currentMonth + 12;
}
return currentYear - this.birthYear;
}
public boolean isAdult() {
return computeAge() >= 18;
}
public void printDetails() {
System.out.println(this.firstName + " " + this.lastName
+ " " + "born " + this.birthMonth + "/" + this.birthDay + "/" + birthYear + " age " +
computeAge() + " " + (isAdult() ? "adult" : "minor"));
}
}
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 18-Feb-21
* Time: 6:53 PM
* File: PersonTest.java
*/
package feb18_21;
public class PersonTest {
public static void main(String[] args) {
Person[] persons= new Person[4];
persons[0]= new Person("Robin","Doe",12,1,1999);
persons[1]= new Person("Pat","Smith",11,3,2002);
persons[2]=new Person("Tyler","Willians",11,25,2002);
persons[3]=new Person("Alex","Park",10,1,2019);
for (int i = 0; i < 4; i++) {
persons[i].printDetails();
}
}
}

#Output Screenshot img.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment