-
Notifications
You must be signed in to change notification settings - Fork 0
/
Date.java
204 lines (184 loc) · 3.97 KB
/
Date.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
194
195
196
197
198
199
200
201
202
203
204
import java.io.Serializable;
public class Date implements Comparable, Cloneable, Serializable{
private int month, year, day;
public static final Date currentDate = new Date(4, 26, 2022); // final Date of the current date
// no arg constructor
public Date() {
this.month = 4;
this.year = 2022;
this.day = 26;
}
// constructor
public Date(int month, int day, int year) {
if(month <= 12 && month >= 1) {
this.month = month;
}
else {
System.out.println("Invalid month given. Set month to 1");
this.month = 1;
}
if(day <= 31 && day >= 1) {
this.day = day;
}
else {
System.out.println("Invalid day given. Set day to 1");
this.day = 1;
}
if(year <= 2024 && year >= 2014) {
this.year = year;
}
else {
System.out.println("Invalid year given. Set year to 2014");
this.year = 2014;
}
}
// copy constructor
private Date(Date other) {
this(other.month, other.day, other.year);
}
/**
* getMonth
* -----------
* PRE: Date is not null.
* POST: returns the month of the date as an integer (1-12)
*/
public int getMonth() {
return month;
}
/**
* setMonth
* -----------
* PRE: Parameter is between 1-12
* POST: Sets the month of the date to the passed integer
*/
public void setMonth(int month) {
if(month <= 12 && month >= 1) {
this.month = month;
}
else {
System.out.println("Invalid month given. Set month to 1");
this.month = 1;
}
}
/**
* getYear
* -----------
* PRE: Date is not null
* POST: Returns the year of the date (2014 - 2024)
*/
public int getYear() {
return year;
}
/**
* setYear
* -----------
* PRE: Parameter is between 2014-2024
* POST: Sets the year of the date to the passed integer
*/
public void setYear(int year) {
if(year <= 2024 && year >= 2014) {
this.year = year;
}
else {
System.out.println("Invalid year given. Set year to 2014");
this.month = 2014;
}
}
/**
* getDay
* -----------
* PRE: Date is not null
* POST: Returns the day of the month (1-31)
*/
public int getDay() {
return day;
}
/**
* setDay
* -----------
* PRE: Parameter is between 1-31
* POST: Sets the day of the month to the passed integer
*/
public void setDay(int day) {
if(day <= 31 && day >= 1) {
this.day = day;
}
else {
System.out.println("Invalid day given. Set day to 1");
this.day = 1;
}
}
/**
* equals
* -----------
* PRE: Date is not null
* POST: Returns true if the parameter is equal to the date, returns false if not equal
*/
public boolean equals(Object o) {
if(!(o instanceof Date))
return false;
Date other = (Date)o;
if(this.month == other.month &&
this.day == other.day &&
this.year == other.year)
return true;
else
return false;
}
/**
* toString
* -----------
* PRE: Date is not null
* POST: returns string form of date. ex. "4/20/2022
*/
public String toString() {
return this.month + "/" + this.day + "/" + this.year;
}
/**
* isEarlierThan
* -----------
* PRE: Date is not null, and parameter is not null
* POST: Returns true if parameter is after the Date, and returns false if parameter is before the date
*/
private boolean isEarlierThan(Date other) {
if(this.year < other.year) {
return true;
}
else if(this.month < other.month) {
return true;
}
else if(this.day < other.day) {
return true;
}
return false;
}
/**
* clone
* -----
* Returns a deep copy of the Date
*/
@Override
public Date clone() {
return new Date(this.month, this.day, this.year);
}
/**
* compareTo
* ---------
* PRE: o is not null
* POST: returns -2 if o is not of type Date,
* return -1 if this date is before other date
* returns 0 if dates are equal
* returns 1 if this date is after other date
*/
@Override
public int compareTo(Object o) {
if(o.getClass() != this.getClass())
return -2;
Date other = (Date) o;
if(this.equals(other))
return 0;
if(this.isEarlierThan(other))
return -1;
return 1;
}
}