Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class DateAndStudent {
- static class Date {
- int day;
- int month;
- int year;
- public Date() {
- this.day = 0;
- this.month = 0;
- this.year = 0;
- }
- public Date(int day, int month, int year) {
- boolean isDateValid = true;
- if (isDayValid(day, month, year) && isMonthValid(day, month, year) && isYearValid(day, month, year)) {
- this.day = day;
- this.month = month;
- this.year = year;
- } else {
- this.day = 0;
- this.month = 0;
- this.year = 0;
- }
- }
- private boolean isYearValid(int day, int month, int year) {
- return year >= 0;
- }
- private boolean isMonthValid(int day, int month, int year) {
- return month >= 1 && month <= 12;
- }
- private boolean isDayValid(int day, int month, int year) {
- boolean result = false;
- switch (month) {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12: {
- if (day >= 1 && day <= 31) {
- result = true;
- }
- break;
- }
- case 4:
- case 6:
- case 9:
- case 11: {
- if (day >= 1 && day <= 30) {
- result = true;
- }
- }
- break;
- case 2: {
- if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
- if (day >= 1 && day <= 29) {
- result = true;
- }
- } else if (day >= 1 && day < 28) {
- result = true;
- }
- }
- break;
- default:
- break;
- }
- return result;
- }
- public int getDay() {
- return day;
- }
- public int getMonth() {
- return month;
- }
- public int getYear() {
- return year;
- }
- private static boolean isGreaterOrEqual(Date date1, Date date2) {
- return isGreater(date1, date2) || areEqual(date1, date2);
- }
- public static boolean isGreater(Date date1, Date date2) {
- if (date1.getYear() > date2.getYear()) {
- return false;
- } else if (date1.getYear() < date2.getYear()) {
- return true;
- } else {
- if (date1.getMonth() > date2.getMonth()) {
- return false;
- } else if (date1.getMonth() < date2.getMonth()) {
- return true;
- } else {
- if (date1.getDay() > date2.getDay()) {
- return false;
- } else return date1.getDay() < date2.getDay();
- }
- }
- }
- private static boolean areEqual(Date date1, Date date2) {
- boolean result = false;
- if (date1.getDay() == date2.getDay() && date1.getMonth() == date2.getMonth()
- && date1.getYear() == date2.getYear()) {
- result = true;
- }
- return result;
- }
- }
- static class Student {
- char[] name;
- Date birthDate;
- public Student() {
- name = new char[]{};
- this.birthDate = new Date(1, 1, 2017);
- }
- public Student(char[] name, Date dateOfBirth) {
- this.name = name;
- this.birthDate = dateOfBirth;
- }
- private char[] getName() {
- return name;
- }
- public Date getBirthDate() {
- return birthDate;
- }
- private static boolean isBefore(Student student1, Student student2) {
- char[] firstName = student1.getName();
- char[] secondName = student2.getName();
- int length1 = firstName.length;
- int length2 = secondName.length;
- int minLength = Math.min(length1, length2);
- for (int i = 0; i < minLength; i++) {
- char firstNameChar = firstName[i];
- char secondNameChar = secondName[i];
- if (firstNameChar != secondNameChar) {
- return firstNameChar > secondNameChar;
- }
- }
- if (length1 != length2) {
- return length1 > length2;
- } else {
- return isGreater(student1.getBirthDate(), student2.getBirthDate());
- }
- }
- }
- public static boolean isGreater(Date date1, Date date2) {
- if (date1.getYear() > date2.getYear()) {
- return false;
- } else if (date1.getYear() < date2.getYear()) {
- return true;
- } else {
- if (date1.getMonth() > date2.getMonth()) {
- return false;
- } else if (date1.getMonth() < date2.getMonth()) {
- return true;
- } else {
- if (date1.getDay() > date2.getDay()) {
- return false;
- } else return date1.getDay() < date2.getDay();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement