-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGhost.java
More file actions
74 lines (68 loc) · 2.33 KB
/
Copy pathGhost.java
File metadata and controls
74 lines (68 loc) · 2.33 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
/**
* The Ghost class represents a mischievous trick-or-treater.
* @author mpaulus7
* @version 10.24
*/
public class Ghost extends TrickOrTreater {
private int robberiesConducted;
/**
* Constructs a Ghost with the specified name, age, and initial candy count.
* @param name The name of the Ghost.
* @param age The age of the Ghost.
* @param numCandy The initial candy count of the Ghost.
*/
public Ghost(String name, int age, int numCandy) {
super(name, age, numCandy);
this.robberiesConducted = 0;
}
/**
* Constructs a default Ghost named "Casper the Unfriendly Ghost" with default age and no candy.
*/
public Ghost() {
super("Casper the Unfriendly Ghost", 12, 0);
this.robberiesConducted = 0;
}
/**
* Simulates trick-or-treating. The Ghost gains two pieces of candy.
*/
public void trickOrTreat() {
System.out.println("Boo! Trick or treat!");
gainCandy(2);
}
/**
* Attempts to rob another TrickOrTreater.
* @param robbed The TrickOrTreater to be robbed.
*/
public void rob(TrickOrTreater robbed) {
if (robbed instanceof Robbable) {
int stolenCandy = ((Robbable) robbed).beRobbed();
gainCandy(stolenCandy);
robberiesConducted++;
}
}
/**
* Returns a formatted String representing the Ghost's attributes.
* @return A formatted String in the format "name/age/numCandy/robberiesConducted".
*/
@Override
public String toString() {
return String.format("%s/%d/%d/%d", super.toString(), robberiesConducted);
}
/**
* Compares the Ghost to another TrickOrTreater, considering robberies conducted.
* @param other The TrickOrTreater to compare to.
* @return An int as defined in the Comparable API.
*/
@Override
public int compareTo(TrickOrTreater other) {
if (other instanceof Ghost) {
int ghostComparison = super.compareTo(other);
if (ghostComparison != 0) {
return ghostComparison;
} else {
return Integer.compare(((Ghost) other).robberiesConducted, this.robberiesConducted);
}
}
return super.compareTo(other);
}
}