-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPizza.java
147 lines (112 loc) · 2.99 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
136
137
138
139
140
141
142
143
144
145
146
147
package Java_programs;
// Decorator abstract class
public abstract class Pizza {
// it is an abstract Decorator
String name;
String description;
public Pizza(String n, String d) {
this.name = n;
this.description = d;
}
public String getDescription() {
return description;
}
public String getName() {
return name;
}
public abstract int getCost();
}
// The decorator class : It extends Pizza to be
// interchangable with it topings decorator can
// also be implemented as an interface
abstract class ToppingsDecorator extends Pizza {
public ToppingsDecorator(String n, String d) {
super(n, d);
}
public abstract String getDescription();
}
class Margherita extends Pizza {
public Margherita() {
super("Margherita Pizza", "Margherita");
}
public int getCost() {
return 100;
}
}
class Chicken extends Pizza {
public Chicken() {
super("Chicken Pizza", "Chicken");
}
public int getCost() {
return 200;
}
}
class Meat extends Pizza {
public Meat() {
super("Meat Pizza", "Meat");
}
public int getCost() {
return 350;
}
}
// Concrete toppings classes
class Tomato extends ToppingsDecorator {
// we need a reference to obj we are decorating
Pizza pizza;
int unit;
public Tomato(Pizza pizza, int unit) {
super(pizza.name, pizza.description);
this.pizza = pizza;
this.unit = unit;
}
public String getDescription() {
return pizza.getDescription() + " ," + unit + " Tomato";
}
public int getCost() {
return unit * 40 + pizza.getCost();
}
}
class Onion extends ToppingsDecorator {
Pizza pizza;
int unit;
public Onion(Pizza pizza, int unit) {
super(pizza.name, pizza.description);
this.pizza = pizza;
this.unit = unit;
}
public String getDescription() {
return pizza.getDescription() + " ," + unit + " Onion";
}
public int getCost() {
return unit * 40 + pizza.getCost();
}
}
class Oliven extends ToppingsDecorator {
Pizza pizza;
int unit;
public Oliven(Pizza pizza, int unit) {
super(pizza.name, pizza.description);
this.pizza = pizza;
this.unit = unit;
}
public String getDescription() {
return pizza.getDescription() + " ," + unit + " Oliven";
}
public int getCost() {
return (unit) * 30 + pizza.getCost();
}
}
// Driver class and method
class PizzaStore {
public static void main(String args[]) {
// create new margherita pizza
Pizza pizza = new Meat();
// decorate it with 1 tomato topping
pizza = new Tomato(pizza, 1);
// decorate it with 2 Onion topping
pizza = new Onion(pizza, 2);
// decorate it with 2 Oliven topping
pizza = new Oliven(pizza, 2);
System.out.println(pizza.getName() + "\n" + pizza.getDescription() + "\nCost :" + pizza.getCost());
}
}