-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddress.java
More file actions
48 lines (41 loc) · 1.53 KB
/
Address.java
File metadata and controls
48 lines (41 loc) · 1.53 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
import java.util.Objects;
public class Address implements Cloneable {
private int streetNumber;
private String streetName;
private String city;
private String state;
public Address(int streetNumber, String streetName, String city, String state) {
super();
this.streetNumber = streetNumber;
this.streetName = streetName;
this.city = city;
this.state = state;
}
@Override
public Address clone() {
try {
Address clonedAddress = (Address) super.clone();
// Perform deep copy for reference-type fields
clonedAddress.streetName = new String(this.streetName);
clonedAddress.city = new String(this.city);
clonedAddress.state = new String(this.state);
return clonedAddress;
} catch (CloneNotSupportedException e) {
// Should never happen since Address implements Cloneable
throw new InternalError(e);
}
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
Address address = (Address) other;
return streetNumber == address.streetNumber &&
Objects.equals(streetName, address.streetName) &&
Objects.equals(city, address.city) &&
Objects.equals(state, address.state);
}
public String toString() {
return streetNumber + " " + streetName + " " + city + ", " + state;
}
}