-
Notifications
You must be signed in to change notification settings - Fork 0
/
YoungHuman.java
213 lines (195 loc) · 5.52 KB
/
YoungHuman.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
205
206
207
208
209
210
211
212
213
import java.io.Serializable;
public class YoungHuman implements Comparable , Cloneable, Serializable{
//YoungHuman x is less than YoungHuman y if x's birthday is before y's ( x is older than y )
private Weight currentWeight;
private Date birthDate;
private Date lastCheckUpDate;
private String firstName;
private String lastName;
// constructor
public YoungHuman(Weight weight, Date birthDate,
String firstName, String lastName) {
this.currentWeight = weight.clone();
this.birthDate = birthDate.clone();
this.lastCheckUpDate = null;
this.firstName = firstName;
this.lastName = lastName;
}
// copy constructor
private YoungHuman(YoungHuman toCopy) {
this.currentWeight = toCopy.currentWeight.clone();
this.birthDate = toCopy.birthDate.clone();
if(toCopy.lastCheckUpDate != null)
this.lastCheckUpDate = toCopy.lastCheckUpDate.clone();
this.firstName = toCopy.firstName;
this.lastName = toCopy.lastName;
}
/**
* getBirthDate
* -----------
* PRE: YoungHuman is not null
* POST: returns the date of the YoungHuman's birthday
*/
public Date getBirthDate() {
return this.birthDate.clone();
}
/**
* getLastCheckUpDate
* -----------
* PRE: YoungHuman is not null
* POST: Returns the date of the last check up if there is one. If there isn't returns null
*/
public Date getLastCheckUpDate() {
if(this.lastCheckUpDate != null)
return this.lastCheckUpDate.clone();
else
return null;
}
/**
* getName
* -----------
* PRE: YoungHuman is not null
* POST: Returns the string of YoungHuman's name. ex. "Parth Gupta"
*/
public String getName() {
return this.firstName + " " + this.lastName;
}
/**
* hasHadCheckUp
* -----------
* PRE: YoungHuman is not null
* POST: Returns true if YoungHuman has had a check-up. False if not
*/
public boolean hasHadCheckUp() {
if(this.lastCheckUpDate != null)
return true;
return false;
}
/**
* setCheckUp
* -----------
* PRE: YoungHuman is not null
* POST: sets the lastCheckUpDate to the passed date
*/
public void setCheckUp(Date dateOfCheckUp) {
if(this.birthDate.compareTo(dateOfCheckUp) == -1)
this.lastCheckUpDate = dateOfCheckUp.clone();
else {
System.out.println("Given check-up date is before the birth-date. Check-up date was not changed.");
}
}
/**
* unsetCheckUp
* -----------
* PRE: YoungHuman is not null
* POST: Sets lastCheckUpDate to null
*/
public void unsetCheckUp() {
this.lastCheckUpDate = null;
}
/**
* setBirthDate
* -----------
* PRE: YoungHuman is not null
* POST: Sets the YoungHuman's birthday to the passed date
*/
public void setBirthDate(Date birthDate) {
if(birthDate.compareTo(Date.currentDate) == -1)
this.birthDate = birthDate.clone();
else
System.out.println("Given birth-date was not in correct range. Birth-Date was not changed.");
}
/**
* setWeight
* -----------
* PRE: YoungHuman is not null
* POST: Sets YoungHuman's weight to the passed weight
*/
public void setWeight(Weight amount) {
this.currentWeight = amount.clone();
}
/**
* getWeight
* -----------
* PRE: YoungHuman is not null
* POST: Returns the weight of the YoungHuman
*/
public Weight getWeight() {
return this.currentWeight.clone();
}
/**
* setName
* -----------
* PRE: YoungHuman is not null
* POST: Sets the name of the YoungHuman to the passed strings. (firstName, lastName)
*/
public void setName(String first, String last) {
this.firstName = first;
this.lastName = last;
}
/**
* toString
* -----------
* PRE: YoungHuman is not null
* POST: Returns a string representation of the YoungHuman. ex. "Parth Gupta, 2 lbs. 5 oz. birth date: 11/11/2020, last check-up: 1/1/2021"
*/
public String toString() {
if(this.hasHadCheckUp())
return this.getName() + ", " + this.currentWeight.toString()
+ " birth date: " + this.birthDate.toString() + ", last check-up: "
+ this.lastCheckUpDate.toString();
else
return this.getName() + ", " + this.currentWeight.toString()
+ " birth date: " + this.birthDate.toString() + ", has not had a check-up";
}
/**
* equals
* -----------
* PRE: YoungHuman and parameter are not null
* POST: Returns true if parameter has the same attributes as the YoungHuman. Returns false if not
*/
public boolean equals(Object other) {
if(!(other instanceof YoungHuman))
return false;
YoungHuman otherYoungHuman = (YoungHuman)other;
if(this.currentWeight.equals(otherYoungHuman.currentWeight) &&
this.birthDate.equals(otherYoungHuman.birthDate) &&
this.firstName.equals(otherYoungHuman.firstName) &&
this.lastName.equals(otherYoungHuman.lastName)) {
if(this.birthDate == null && otherYoungHuman.birthDate == null)
return true;
else if(this.birthDate.equals(otherYoungHuman.birthDate))
return true;
}
return false;
}
/**
* clone
* -----
* Returns a deep copy of YoungHuman
*/
@Override
public YoungHuman clone() {
return new YoungHuman(this.currentWeight, this.birthDate, this.firstName, this.lastName);
}
/**
* compareTo
* ---------
* PRE: o is not null
* POST: returns -2 if o is not of type YoungHuman,
* return -1 if this YoungHuman is older than other YoungHuman
* returns 0 if birth-dates are equal
* returns 1 if this YoungHuman is younger than other YoungHuman
*/
@Override
public int compareTo(Object o) {
if(o.getClass() != this.getClass())
return -2;
YoungHuman other = (YoungHuman) o;
if(this.equals(other))
return 0;
if(this.birthDate.compareTo(other.birthDate) == -1)
return -1;
return 1;
}
}