-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWitch.java
More file actions
74 lines (69 loc) · 2.45 KB
/
Copy pathWitch.java
File metadata and controls
74 lines (69 loc) · 2.45 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 Witch class represents a TrickOrTreater with magical abilities.
* @author mpaulus7
* @version 10.24
*/
public class Witch extends TrickOrTreater implements Robbable {
private String signatureCackle; // The witch's signature cackle
/**
* Constructs a Witch with specified name, age, initial candy count, and signature cackle.
*
* @param name The name of the Witch.
* @param age The age of the Witch.
* @param numCandy The initial candy count of the Witch.
* @param signatureCackle The Witch's signature cackle.
*/
public Witch(String name, int age, int numCandy, String signatureCackle) {
super(name, age, numCandy);
if (signatureCackle == null || signatureCackle.equals("")) {
this.signatureCackle = "Bwahaha";
} else {
this.signatureCackle = signatureCackle;
}
}
/**
* Constructs a default Witch named "Maleficent" with age 7 and no candy, and default signature cackle.
*/
public Witch() {
super("Maleficent", 7, 0);
this.signatureCackle = "Bwahaha";
}
/**
* Represents the action of trick-or-treating for a Witch.
* Acquires two new pieces of candy.
*/
@Override
public void trickOrTreat() {
System.out.printf("%s! I'll get you my pretty!", signatureCackle);
gainCandy(2);
}
/**
* Compares the Witch to another TrickOrTreater based on candy count, age, and signature cackle length.
*
* @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 Witch) {
int witchComparison = super.compareTo(other);
if (witchComparison != 0) {
return witchComparison;
} else {
return Integer.compare(((Witch) other).signatureCackle.length(), this.signatureCackle.length());
}
}
return super.compareTo(other);
}
/**
* Allows the Witch to be robbed. The Witch may lose up to six pieces of candy during the robbery.
*
* @return The number of candies lost during the robbery.
*/
@Override
public int beRobbed() {
int stolenCandy = Math.min(6, this.getNumCandy());
this.loseCandy(stolenCandy);
return stolenCandy;
}
}