-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomer.java
More file actions
86 lines (68 loc) · 2.14 KB
/
Copy pathCustomer.java
File metadata and controls
86 lines (68 loc) · 2.14 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
package model.entity;
import java.util.Objects;
public class Customer {
// 1. Fields (private, final everywhere)
private final int custId;
private String custName;
private final BankAccount custAcc;
private Address custAddr;
// 2. Constructor with validation
public Customer(int custId, String custName, BankAccount custAcc, Address custAddr) {
if (custId <= 0) throw new IllegalArgumentException("Customer ID must be positive");
if (custName == null || custName.trim().isEmpty()) {
throw new IllegalArgumentException("Customer name cannot be empty");
}
if (custAcc == null) throw new IllegalArgumentException("Account required");
if (custAddr == null) throw new IllegalArgumentException("Address required");
this.custId = custId;
this.custName = custName.trim();
this.custAcc = custAcc;
this.custAddr = custAddr;
}
// 3. Getters
public int getCustId() {
return custId;
}
public String getCustName() {
return custName;
}
public BankAccount getCustAcc() {
return custAcc;
}
public Address getCustAddr() {
return custAddr;
}
//4.setters
public void setCustName(String custName) {
if (custName == null || custName.trim().isEmpty() || custName.length() < 2 || custName.length() > 50) {
throw new IllegalArgumentException("Valid name (2-50 chars) required");
}
this.custName = custName.trim();
System.out.println("✅ Name updated to: " + this.custName);
}
public void setCustAddr(Address custAddr) {
if (custAddr == null) throw new IllegalArgumentException("Address required");
this.custAddr = custAddr;
System.out.println("✅ Address updated");
}
// 5. Professional toString()
@Override
public String toString() {
return String.format("ID:%-4d %-15s | %-35s | %s",
custId, custName, custAcc, custAddr);
}
// 6. equals() + hashCode() for uniqueness
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Customer)) return false;
return custId == ((Customer) obj).custId;
}
@Override
public int hashCode() {
return Objects.hash(custId);
}
public int compareTo(Customer other) {
return Integer.compare(this.custId, other.custId);
}
}