-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPerson.java
More file actions
35 lines (30 loc) · 869 Bytes
/
Copy pathPerson.java
File metadata and controls
35 lines (30 loc) · 869 Bytes
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
public class Person {
private final String name;
private int balance;
public Person(String name, int balance) {
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("Name cannot be empty.");
}
if (balance < 0) {
throw new IllegalArgumentException("Balance cannot be negative in Person model.");
}
this.name = name;
this.balance = balance;
}
public String getName() {
return name;
}
public int getBalance() {
return balance;
}
public void addToBalance(int delta) {
this.balance += delta;
if (this.balance < 0) {
throw new IllegalStateException("Balance cannot become negative.");
}
}
@Override
public String toString() {
return name + ":" + balance;
}
}