Repository navigation
Expand file tree
/
Copy pathWeekday.java
More file actions
280 lines (253 loc) · 7.63 KB
/
Copy pathWeekday.java
File metadata and controls
280 lines (253 loc) · 7.63 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import java.util.Scanner;
/**
* WEEKDAY
*
* Converted from BASIC to Java by Aldrin Misquitta (@aldrinm)
*
*/
public class Weekday {
//TABLE OF VALUES FOR THE MONTHS TO BE USED IN CALCULATIONS.
//Dummy value added at index 0, so we can reference directly by the month number value
private final static int[] t = new int[]{-1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
public static void main(String[] args) {
printIntro();
Scanner scanner = new Scanner(System.in);
System.out.print("ENTER TODAY'S DATE IN THE FORM: 3,24,1979 ");
DateStruct todaysDate = readDate(scanner);
System.out.print("ENTER DAY OF BIRTH (OR OTHER DAY OF INTEREST) ");
DateStruct dateOfInterest = readDate(scanner);
int I1 = (dateOfInterest.year - 1500) / 100;
//TEST FOR DATE BEFORE CURRENT CALENDAR.
if ((dateOfInterest.year - 1582) >= 0) {
int A = I1 * 5 + (I1 + 3) / 4;
int I2 = (A - b(A) * 7);
int Y2 = (dateOfInterest.year / 100);
int Y3 = (dateOfInterest.year - Y2 * 100);
A = Y3 / 4 + Y3 + dateOfInterest.day + t[dateOfInterest.month] + I2;
calculateAndPrintDayOfWeek(I1, A, todaysDate, dateOfInterest, Y3);
if ((todaysDate.year * 12 + todaysDate.month) * 31 + todaysDate.day
== (dateOfInterest.year * 12 + dateOfInterest.month) * 31 + dateOfInterest.day) {
return; //stop the program
}
int I5 = todaysDate.year - dateOfInterest.year;
System.out.print("\n");
int I6 = todaysDate.month - dateOfInterest.month;
int I7 = todaysDate.day - dateOfInterest.day;
if (I7 < 0) {
I6 = I6 - 1;
I7 = I7 + 30;
}
if (I6 < 0) {
I5 = I5 - 1;
I6 = I6 + 12;
}
if (I5 < 0) {
return; //do nothing. end the program
} else {
if (I7 != 0) {
printHeadersAndAge(I5, I6, I7);
} else {
if (I6 != 0) {
printHeadersAndAge(I5, I6, I7);
} else {
System.out.println("***HAPPY BIRTHDAY***");
printHeadersAndAge(I5, I6, I7);
}
}
}
int A8 = (I5 * 365) + (I6 * 30) + I7 + (I6 / 2);
int K5 = I5;
int K6 = I6;
int K7 = I7;
//CALCULATE RETIREMENT DATE.
int E = dateOfInterest.year + 65;
// CALCULATE TIME SPENT IN THE FOLLOWING FUNCTIONS.
float F = 0.35f;
System.out.printf("%-28s", "YOU HAVE SLEPT");
DateStruct scratchDate = new DateStruct(K6, K7, K5); //K5 is a temp year, K6 is month, K7 is day
printStatisticRow(F, A8, scratchDate);
K5 = scratchDate.year;
K6 = scratchDate.month;
K7 = scratchDate.day;
F = 0.17f;
System.out.printf("%-28s", "YOU HAVE EATEN");
scratchDate = new DateStruct(K6, K7, K5);
printStatisticRow(F, A8, scratchDate);
K5 = scratchDate.year;
K6 = scratchDate.month;
K7 = scratchDate.day;
F = 0.23f;
if (K5 > 3) {
if (K5 > 9) {
System.out.printf("%-28s", "YOU HAVE WORKED/PLAYED");
} else {
System.out.printf("%-28s", "YOU HAVE PLAYED/STUDIED");
}
} else {
System.out.printf("%-28s", "YOU HAVE PLAYED");
}
scratchDate = new DateStruct(K6, K7, K5);
printStatisticRow(F, A8, scratchDate);
K5 = scratchDate.year;
K6 = scratchDate.month;
K7 = scratchDate.day;
if (K6 == 12) {
K5 = K5 + 1;
K6 = 0;
}
System.out.printf("%-28s%14s%14s%14s%n", "YOU HAVE RELAXED", K5, K6, K7);
System.out.printf("%16s*** YOU MAY RETIRE IN %s ***%n", " ", E);
System.out.printf("%n%n%n%n%n");
} else {
System.out.println("NOT PREPARED TO GIVE DAY OF WEEK PRIOR TO MDLXXXII.");
}
}
private static void printStatisticRow(float F, int A8, DateStruct scratchDate) {
int K1 = (int) (F * A8);
int I5 = K1 / 365;
K1 = K1 - (I5 * 365);
int I6 = K1 / 30;
int I7 = K1 - (I6 * 30);
int K5 = scratchDate.year - I5;
int K6 = scratchDate.month - I6;
int K7 = scratchDate.day - I7;
if (K7 < 0) {
K7 = K7 + 30;
K6 = K6 - 1;
}
if (K6 <= 0) {
K6 = K6 + 12;
K5 = K5 - 1;
}
//to return the updated values of K5, K6, K7 we send them through the scratchDate
scratchDate.year = K5;
scratchDate.month = K6;
scratchDate.day = K7;
System.out.printf("%14s%14s%14s%n", I5, I6, I7);
}
private static void printHeadersAndAge(int I5, int I6, int I7) {
System.out.printf("%14s%14s%14s%14s%14s%n", " ", " ", "YEARS", "MONTHS", "DAYS");
System.out.printf("%14s%14s%14s%14s%14s%n", " ", " ", "-----", "------", "----");
System.out.printf("%-28s%14s%14s%14s%n", "YOUR AGE (IF BIRTHDATE)", I5, I6, I7);
}
private static void calculateAndPrintDayOfWeek(int i1, int a, DateStruct dateStruct, DateStruct dateOfInterest, int y3) {
int b = (a - b(a) * 7) + 1;
if (dateOfInterest.month > 2) {
printDayOfWeek(dateStruct, dateOfInterest, b);
} else {
if (y3 == 0) {
int aa = i1 - 1;
int t1 = aa - a(aa) * 4;
if (t1 == 0) {
if (b != 0) {
b = b - 1;
printDayOfWeek(dateStruct, dateOfInterest, b);
} else {
b = 6;
b = b - 1;
printDayOfWeek(dateStruct, dateOfInterest, b);
}
}
}
}
}
/**
* PRINT THE DAY OF THE WEEK THE DATE FALLS ON.
*/
private static void printDayOfWeek(DateStruct dateStruct, DateStruct dateOfInterest, int b) {
if (b == 0) {
b = 7;
}
if ((dateStruct.year * 12 + dateStruct.month) * 31
+ dateStruct.day
<
(dateOfInterest.year * 12
+ dateOfInterest.month) * 31 + dateOfInterest.day) {
System.out.printf("%s / %s / %s WILL BE A ", dateOfInterest.month, dateOfInterest.day, dateOfInterest.year);
} else if ((dateStruct.year * 12 + dateStruct.month) * 31
+ dateStruct.day == (dateOfInterest.year * 12 + dateOfInterest.month)
* 31 + dateOfInterest.day) {
System.out.printf("%s / %s / %s IS A ", dateOfInterest.month, dateOfInterest.day, dateOfInterest.year);
} else {
System.out.printf("%s / %s / %s WAS A ", dateOfInterest.month, dateOfInterest.day, dateOfInterest.year);
}
switch (b) {
case 1:
System.out.println("SUNDAY.");
break;
case 2:
System.out.println("MONDAY.");
break;
case 3:
System.out.println("TUESDAY.");
break;
case 4:
System.out.println("WEDNESDAY.");
break;
case 5:
System.out.println("THURSDAY.");
break;
case 6:
if (dateOfInterest.day == 13) {
System.out.println("FRIDAY THE THIRTEENTH---BEWARE!");
} else {
System.out.println("FRIDAY.");
}
break;
case 7:
System.out.println("SATURDAY.");
break;
}
}
private static int a(int a) {
return a / 4;
}
private static int b(int a) {
return a / 7;
}
private static void printIntro() {
System.out.println(" WEEKDAY");
System.out.println(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
System.out.println("\n\n\n");
System.out.println("WEEKDAY IS A COMPUTER DEMONSTRATION THAT");
System.out.println("GIVES FACTS ABOUT A DATE OF INTEREST TO YOU.");
System.out.println("\n");
}
/**
* Read user input for a date, do some validation and return a simple date structure
*/
private static DateStruct readDate(Scanner scanner) {
boolean done = false;
int mm = 0, dd = 0, yyyy = 0;
while (!done) {
String input = scanner.next();
String[] tokens = input.split(",");
if (tokens.length < 3) {
System.out.println("DATE EXPECTED IN FORM: 3,24,1979 - RETRY INPUT LINE");
} else {
try {
mm = Integer.parseInt(tokens[0]);
dd = Integer.parseInt(tokens[1]);
yyyy = Integer.parseInt(tokens[2]);
done = true;
} catch (NumberFormatException nfe) {
System.out.println("NUMBER EXPECTED - RETRY INPUT LINE");
}
}
}
return new DateStruct(mm, dd, yyyy);
}
/**
* Convenience date structure to hold user date input
*/
private static class DateStruct {
int month;
int day;
int year;
public DateStruct(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
}
}