-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
65 lines (60 loc) · 1.65 KB
/
Player.java
File metadata and controls
65 lines (60 loc) · 1.65 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
/**
* Represents a single Player in a game
*/
public class Player {
/**
* name of the Player
*/
private final String name;
/**
* current score of the Player
*/
private int score;
/**
* Constructor
* @param name name of the Player; cannot be null or blank. Cannot be changed.
*/
public Player(String name) {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("name cannot be null or blank");
}
this.name = name;
this.score = 0;
}
/**
* Gets the name of the Player
* @return the Player's name
*/
public String getName() {
return name;
}
/**
* Gets the score of the Player
* @return the Player's current score
*/
public int getScore() {
return score;
}
/**
* Increases the Player's score by a whole number greater than 0
* @param increaseBy amount to increase the score by; must be greater than 0
* @return the updated score
*/
public int increaseScore(int increaseBy) {
if (increaseBy <= 0) {
throw new IllegalArgumentException("increaseBy must be more than 0");
}
return score += increaseBy;
}
/**
* Checks for equality, true if name and score are the same, else false
*/
@Override
public boolean equals(Object player2) {
if (!(player2 instanceof Player)) {
return false;
}
return ((Player)player2).getName().equals(name) &&
((Player)player2).getScore() == score;
}
}