Advertisement
kalinikov

Date & Student

Dec 14th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class DateAndStudent {
  4.  
  5. static class Date {
  6. int day;
  7. int month;
  8. int year;
  9.  
  10. public Date() {
  11. this.day = 0;
  12. this.month = 0;
  13. this.year = 0;
  14. }
  15.  
  16. public Date(int day, int month, int year) {
  17. boolean isDateValid = true;
  18. if (isDayValid(day, month, year) && isMonthValid(day, month, year) && isYearValid(day, month, year)) {
  19. this.day = day;
  20. this.month = month;
  21. this.year = year;
  22. } else {
  23. this.day = 0;
  24. this.month = 0;
  25. this.year = 0;
  26. }
  27. }
  28.  
  29. private boolean isYearValid(int day, int month, int year) {
  30. return year >= 0;
  31. }
  32.  
  33. private boolean isMonthValid(int day, int month, int year) {
  34. return month >= 1 && month <= 12;
  35. }
  36.  
  37. private boolean isDayValid(int day, int month, int year) {
  38. boolean result = false;
  39. switch (month) {
  40. case 1:
  41. case 3:
  42. case 5:
  43. case 7:
  44. case 8:
  45. case 10:
  46. case 12: {
  47. if (day >= 1 && day <= 31) {
  48. result = true;
  49. }
  50. break;
  51. }
  52. case 4:
  53. case 6:
  54. case 9:
  55. case 11: {
  56. if (day >= 1 && day <= 30) {
  57. result = true;
  58. }
  59. }
  60. break;
  61. case 2: {
  62. if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
  63. if (day >= 1 && day <= 29) {
  64. result = true;
  65. }
  66. } else if (day >= 1 && day < 28) {
  67. result = true;
  68. }
  69. }
  70. break;
  71. default:
  72. break;
  73. }
  74.  
  75. return result;
  76. }
  77.  
  78. public int getDay() {
  79. return day;
  80. }
  81.  
  82. public int getMonth() {
  83. return month;
  84. }
  85.  
  86. public int getYear() {
  87. return year;
  88. }
  89.  
  90. private static boolean isGreaterOrEqual(Date date1, Date date2) {
  91. return isGreater(date1, date2) || areEqual(date1, date2);
  92. }
  93.  
  94. public static boolean isGreater(Date date1, Date date2) {
  95. if (date1.getYear() > date2.getYear()) {
  96. return false;
  97. } else if (date1.getYear() < date2.getYear()) {
  98. return true;
  99. } else {
  100. if (date1.getMonth() > date2.getMonth()) {
  101. return false;
  102. } else if (date1.getMonth() < date2.getMonth()) {
  103. return true;
  104. } else {
  105. if (date1.getDay() > date2.getDay()) {
  106. return false;
  107. } else return date1.getDay() < date2.getDay();
  108. }
  109. }
  110. }
  111.  
  112. private static boolean areEqual(Date date1, Date date2) {
  113. boolean result = false;
  114. if (date1.getDay() == date2.getDay() && date1.getMonth() == date2.getMonth()
  115. && date1.getYear() == date2.getYear()) {
  116. result = true;
  117. }
  118.  
  119. return result;
  120. }
  121.  
  122.  
  123. }
  124.  
  125.  
  126. static class Student {
  127. char[] name;
  128. Date birthDate;
  129.  
  130. public Student() {
  131. name = new char[]{};
  132. this.birthDate = new Date(1, 1, 2017);
  133. }
  134.  
  135. public Student(char[] name, Date dateOfBirth) {
  136. this.name = name;
  137. this.birthDate = dateOfBirth;
  138. }
  139.  
  140. private char[] getName() {
  141. return name;
  142. }
  143.  
  144. public Date getBirthDate() {
  145. return birthDate;
  146. }
  147.  
  148. private static boolean isBefore(Student student1, Student student2) {
  149. char[] firstName = student1.getName();
  150. char[] secondName = student2.getName();
  151.  
  152. int length1 = firstName.length;
  153. int length2 = secondName.length;
  154. int minLength = Math.min(length1, length2);
  155.  
  156. for (int i = 0; i < minLength; i++) {
  157. char firstNameChar = firstName[i];
  158. char secondNameChar = secondName[i];
  159. if (firstNameChar != secondNameChar) {
  160. return firstNameChar > secondNameChar;
  161. }
  162. }
  163.  
  164. if (length1 != length2) {
  165. return length1 > length2;
  166. } else {
  167. return isGreater(student1.getBirthDate(), student2.getBirthDate());
  168. }
  169. }
  170.  
  171. }
  172.  
  173. public static boolean isGreater(Date date1, Date date2) {
  174. if (date1.getYear() > date2.getYear()) {
  175. return false;
  176. } else if (date1.getYear() < date2.getYear()) {
  177. return true;
  178. } else {
  179. if (date1.getMonth() > date2.getMonth()) {
  180. return false;
  181. } else if (date1.getMonth() < date2.getMonth()) {
  182. return true;
  183. } else {
  184. if (date1.getDay() > date2.getDay()) {
  185. return false;
  186. } else return date1.getDay() < date2.getDay();
  187. }
  188. }
  189. }
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement