-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNextDate.java
197 lines (179 loc) · 5.62 KB
/
NextDate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// -*- Java++ -*-
// Triangle.java
// $Id: Triangle.java 827 2011-02-07 14:20:53Z medkulk $
// I pledge that I have neither given nor received any help
// on this assignment.
//psuedoCode implemented from textbook
public class NextDate {
//empty constructor
public NextDate(){
}
/**
* variable day day of the month
* variable month month of the year '
* variable year present year
* @return checker next date
*/
//first implementation of the NextDate
public String checkNextDate1 (int day, int month, int year) {
int tomDay = 12;
int tomMonth = 10;
int tomYear = 2020;
String checker;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
if (day < 31) {
tomDay = day + 1;
} else {
tomDay = 1;
tomMonth = month + 1;
}
break;
case 4:
case 6:
case 9:
case 11:
if (day < 30) {
tomDay = day + 1;
} else {
tomDay = 1;
tomMonth = month + 1;
}
break;
case 12:
if (day < 31) {
tomDay = day + 1;
} else {
tomDay = 1;
tomMonth = 1;
if (year == 2012) {
checker = "2012 is over.";
} else {
tomYear = year + 1;
}
}
break;
case 2:
if (day < 28) {
tomDay = day + 1;
} else {
if (day == 28) {
if (year % 4 == 0) { //for leap year
tomDay = 29;
}
else {
tomDay = 1;
tomMonth = 3;
}
} else if (day == 29) {
if (year % 4 == 0) { //condition for leap year
tomDay = 1;
tomMonth = 3;
} else {
checker = "Cannot have Feb. " + day;
}
}
}
break;
}
checker = tomMonth + "-" + tomDay + "-" + tomYear;
return checker;
} // end checkNextDate1 ()
//second implemention with similar parameters and returns like first (copied from textbook)
public String checkNextDate2 (int day, int month, int year) {
String checker;
int tomDay = 12;
int tomMonth = 10;
int tomYear = 2020;
boolean c1 = (1 <= day) && (day <= 31);
boolean c2;
c2 = (1 <= month) && (month <= 12);
boolean c3;
c3 = (1812 <= year) && (year <= 2012);
if (!c1) {
System.out.println ("Value of day not in the range 1......31");
checker = "Invalid Input Date";
}
if (!c2) {
System.out.println ("Value of month not in the range 1.....12");
checker = "Invalid Input Date";
}
if (!c3) {
System.out.println ("Value of year not in the range 1812..........2012");
checker = "Invalid Input Date";
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
if (day < 31) {
tomDay = day + 1;
} else {
tomDay = 1;
tomMonth = month + 1;
}
break;
case 4:
case 6:
case 9:
case 11:
if (day < 30) {
tomDay = day + 1;
} else {
if (day == 30) {
tomDay = 1;
tomMonth = month + 1;
} else {
checker = "Invalid Input Date";
}
}
break;
case 12:
if (day < 31) {
tomDay = day + 1;
} else {
tomDay = 1;
tomMonth = 1;
if (year == 2012) {
checker = "Invalid Input Date";
} else {
tomYear = year + 1;
}
}
break;
case 2:
if (day < 28) {
tomDay = day + 1;
} else {
if (day == 28) {
if (year % 4 == 0) {
tomDay = 29;
} else {
tomDay = 1;
tomMonth = 3;
}
} else if (day == 29) {
if (year % 4 == 0) { //condition for leap
tomDay = 1;
tomMonth = 3;
} else {
if (day > 29) {
checker = "Invalid Input Date";
}
}
}
}
break;
}
checker = tomMonth + "-" + tomDay + "-" + tomYear;
return checker;
} // end nextDate2 ()
} //end NextDate