-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPizza.java
135 lines (115 loc) · 3.19 KB
/
Pizza.java
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package pizza;
import java.util.Date;
abstract public class Pizza {
protected String size;
protected String toppings;
private Date time_order;
protected double cost;
public static final double DELIVERY_CHARGE = 5;
protected Pizza(String sz) { // only allow sub-classes to instantiate
size = sz;
time_order = new Date();
toppings = "";
}
public String get_order() {
String result;
if (toppings.isEmpty()){
result = String.format("At %s a %s pizza was ordered. The total is $%.2f", time_order, this.size, this.cost);
}
else{
result = String.format("At %s a %s pizza was ordered with: %s . The total is $%.2f", time_order, this.size, this.toppings, this.cost);
}
return result;
}
public String getSize(){
return this.size;
}
public Double getCost(){
return cost;
}
public String getToppings(){
return toppings;
}
public abstract void addTopping(String topping); //subclass must implement this method
public abstract void removeTopping(String topping); //subclass must implement this method
}
class SmallPizza extends Pizza {
public SmallPizza(){
super("Small");
super.cost = 8;
}
@Override
public void addTopping(String topping){
// Adds a topping and increments price accordingly
super.toppings += topping + " ";
cost += .25;
}
@Override
public void removeTopping(String topping) {
// Remove a topping and decrements price accordingly
toppings = toppings.replace(topping + " ",""); // remove the topping
if(cost > 8){ // use this to block ItemStateListener from reducing price below base price
cost -= .25;
}
}
}
class MediumPizza extends Pizza {
public MediumPizza(){
super("Medium");
super.cost = 10;
}
@Override
public void addTopping(String topping) {
// Adds a topping and increments price accordingly
super.toppings += topping + " ";
cost += .50;
}
@Override
public void removeTopping(String topping) {
// Remove a topping and decrements price accordingly
toppings = toppings.replace(topping,""); // remove the topping
if(cost > 10){ // use this to block ItemStateListener from reducing price below base price
cost -= .50;
}
}
}
class LargePizza extends Pizza {
public LargePizza(){
super("Large");
super.cost = 12;
}
@Override
public void addTopping(String topping) {
// Adds a topping and increments price accordingly
super.toppings += topping + " ";
cost += .75;
}
@Override
public void removeTopping(String topping) {
// Remove a topping and decrements price accordingly
toppings = toppings.replace(topping,""); // remove the topping
if(cost > 12){ // use this to block ItemStateListener from reducing price below base price
cost -= .75;
}
}
}
class XLargePizza extends Pizza {
public XLargePizza(){
super("X-Large");
super.cost = 14;
}
@Override
public void addTopping(String topping) {
// Adds a topping and increments price accordingly
super.toppings += topping + " ";
cost += 1;
}
@Override
public void removeTopping(String topping) {
// Remove a topping and decrements price accordingly
toppings = toppings.replace(topping,""); // remove the topping
if(cost > 14){ // use this to block ItemStateListener from reducing price below base price
cost -= 1;
}
}
}