forked from jerager/CS111Lab11B
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateObject.java
More file actions
231 lines (175 loc) · 4.49 KB
/
Copy pathDateObject.java
File metadata and controls
231 lines (175 loc) · 4.49 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
// Lab11 Start abbas, stanley
// Matt Solberg and Dylan Finazzo
// Lab11 Start Jack Fergus & Sarah McDonald
// Lab11 Start Names Here Please Soon-Young Shimizu Wayne
// Lab11 Start Emily Fitts & Ooga Na
// Lab11 Start Kaitlyn Haase & Cameron Chandler
public class DateObject {
// 1 o'oclock section
private int day;
private int month;
private int year;
private String calendar;
private static String[] names = {"No Month Zero","January","February","March",
"April","May","June","July","August","September","October",
"November","December"};
public static int monthToInt(String name) {
for (int i=1; i<13; i++) {
if (name.equals(names[i]))
return i;
}
return -1;
}
public void setDay(String m, int d, int y) {
day = d;
year = y;
month = monthToInt(m);
calendar = "Gregorian";
}
public void setDay(String m, int d, int y, String cal) {
day=d;
year=y;
month= monthToInt(m);
calendar= cal;
}
public void bump() {
day = day + 1;
}
public String printRep() {
return names[month]+"/"+day+"/"+year;
}
// calculate the days in the month Use isLeap
public int daysInMonth() {
int daysinmonth=0;
if(isLeap()==false){
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
daysinmonth = 31;
return daysinmonth;}
if(month==4||month==6||month==9||month==12){
daysinmonth = 30;
return daysinmonth;}
if(month==2){
daysinmonth=28;
return daysinmonth;
}
}
if(isLeap()==true){
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
daysinmonth = 31;
return daysinmonth;}
if(month==4||month==6||month==9||month==12){
daysinmonth = 30;
return daysinmonth;}
if(month==2){
daysinmonth=29;
return daysinmonth;
}
}
return 0;
}
// call one of isLeapGregorian and isLeap Julian
public boolean isLeap() {
if(calendar.equals("Gregorian"))
return isLeapGregorian();
if(calendar.equals("Julian"))
return isLeapJulian();
else return false;
}
// leap year if divisible by 400, or if divisible by 4 and not 100
public boolean isLeapGregorian(int year) {
if( year%400==0 || (year%4==0 && !(year%100==0)))
return true;
else return false;
}
// leap year if divisible by 4
public boolean isLeapJulian(int year) {
if ( year%4==0)
return true;
else return false;
}
public boolean sameDate(DateObject other) {
if (day == other.day && year == other.year && month == other.month)
return true;
else
return false;
}
// return true if December 31
public boolean isLastDayInYear() {
return false;
}
// return a new Date with the same fields as this one
public DateObject generateCopy() {
DateObject new1 = new DateObject();
new1.setDay(this.getMonth(), this.getDay(), this.getYear(), calendar);
return new1;
}
// return a new Date with the same fields as this one's tomorrow
public DateObject generateTomorrow() {
DateObject new1 = generateCopy();
new1.makeTomorrow();
return new1;
}
//return the year
public int getYear() {
return year;
}
// return the day
public int getDay() {
return day;
}
// return the month
public String getMonth() {
return names[month];
}
// finish this - check for the end of a month use daysinmonth
public void makeTomorrow() {
if (isLastDayInYear()) {
day = 1;
month = 1;
year = year+1;
}
else {
bump();
if (daysInMonth(month)<day){
month++;
day = 1;
}
}
}
// move forward n days (use makeTomorrow
public void makeTomorrow(int n){
for (int i=0;i<n;i++){
makeTomorrow();
}
}
// return true if this is after other
public boolean after(DateObject other) {
if(year > other.getYear()){
return true;
}
else if(year == other.getYear() && month > monthToInt(other.getMonth())){
return true;
}
else if(year==other.getYear() && month==monthToInt(other.getMonth()) && day > other.getDay()){
return true;
}
else{
return false;
}
}
// return true if this is before other
public boolean before(DateObject other) {
if(other.getYear() > year){
return true;
}
else if(year == other.getYear() && month < monthToInt(other.getMonth())){
return true;
}
else if(year==other.getYear() && month==monthToInt(other.getMonth()) && day < other.getDay()){
return true;
}
else{
return false;
}
}
}