-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDessertItem.java
More file actions
93 lines (82 loc) · 1.88 KB
/
DessertItem.java
File metadata and controls
93 lines (82 loc) · 1.88 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
87
88
89
90
91
92
93
/**
* @author XiuNhon
*
* An abstract class DessertItem to create new object implementing
* polymorphism, inheritance, interface subclasses: Candy, Cookie,
* IceCream, Sundae
*/
public abstract class DessertItem implements Comparable<DessertItem> {
protected String name;
protected int calories;
/**
* Null constructor for DessertItem class
*/
public DessertItem() {
name = "";
calories = 0;
}
/**
* Initializes DessertItem data
*/
public DessertItem(String name, int calories) {
this.name = name;
this.calories = calories;
}
/**
* setters for name and calories
*/
public void setName(String name) {
this.name = name;
}
public void setCalories(int calories) {
this.calories = calories;
}
/**
* Returns name of DessertItem
*
* @return name of DessertItem
*/
public final String getName() {
return name;
}
public final int getCalories() {
return calories;
}
/**
* Returns cost of DessertItem
*
* @return cost of DessertItem
*/
public abstract double getCost();
/**
* compare this DessertItem's calories with the other's
*
* @param other
* @return 1 if this > other
* @return -1 if this < other
* @return 0 if this == other
*/
public int compareTo(DessertItem other) {
if (getCalories() > other.getCalories())
return 1;
else if (getCalories() < other.getCalories())
return -1;
else
return 0;
}
/**
* using the result of compareTo() to determine the DessertItem with bigger
* calories
*
* @param d1
* @param d2
* @return DessertItem with bigger calories if equal, return null
*/
public static DessertItem max(DessertItem d1, DessertItem d2) {
if (d1.compareTo(d2) == 1) // d1 > d2
return d1;
else if (d1.compareTo(d2) == -1)
return d2;
return null;
}
}// end of class