-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddress.java
More file actions
55 lines (45 loc) · 1.46 KB
/
Copy pathAddress.java
File metadata and controls
55 lines (45 loc) · 1.46 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
package model.entity;
public class Address {
// 1. Fields (private, final where appropriate)
private final String city;
private final String state;
private final int pinCode;
// 2. Constructor
public Address(String city, String state, int pinCode) {
this.city = city != null ? city.trim() : "Unknown";
this.state = state != null ? state.trim() : "Unknown";
this.pinCode = Math.max(100000, Math.min(999999, pinCode)); // Valid PIN range
}
// 3. Getters (immutable - no setters needed)
public String getCity() {
return city;
}
public String getState() {
return state;
}
public int getPinCode() {
return pinCode;
}
// 4. toString() - Professional format
@Override
public String toString() {
return String.format("%s, %s - %06d", city, state, pinCode);
}
// 5. equals() + hashCode() for comparisons
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Address)) return false;
Address other = (Address) obj;
return pinCode == other.pinCode &&
city.equals(other.city) &&
state.equals(other.state);
}
@Override
public int hashCode() {
int result = city.hashCode();
result = 31 * result + state.hashCode();
result = 31 * result + pinCode;
return result;
}
}