-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment5pt3.java
166 lines (136 loc) · 4.72 KB
/
assignment5pt3.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class Furniture {
String name;
String style;
String material;
float price;
public Furniture(String name, String style, String material, float price) {
this.name = name;
this.style = style;
this.material = material;
this.price = price;
}
@Override
public String toString() {
return "Furniture{" +
"name='" + name + '\'' +
", style='" + style + '\'' +
", material='" + material + '\'' +
", price=" + price +
'}';
}
}
abstract class FurnitureFactory {
public abstract Chair createChair();
public abstract Table createTable();
public abstract Sofa createSofa();
}
class ModernWoodFactory extends FurnitureFactory {
public Chair createChair() {
return new Chair("Modern Chair", "Modern", "Wood", 150);
}
public Table createTable() {
return new Table("Modern Table", "Modern", "Wood", 300);
}
public Sofa createSofa() {
return new Sofa("Modern Sofa", "Modern", "Wood", 500);
}
}
class TraditionalMetalFactory extends FurnitureFactory {
public Chair createChair() {
return new Chair("Traditional Chair", "Traditional", "Metal", 100);
}
public Table createTable() {
return new Table("Traditional Table", "Traditional", "Metal", 250);
}
public Sofa createSofa() {
return new Sofa("Traditional Sofa", "Traditional", "Metal", 450);
}
}
class IndustrialGlassFactory extends FurnitureFactory {
public Chair createChair() {
return new Chair("Industrial Chair", "Industrial", "Glass", 120);
}
public Table createTable() {
return new Table("Industrial Table", "Industrial", "Glass", 280);
}
public Sofa createSofa() {
return new Sofa("Industrial Sofa", "Industrial", "Glass", 480);
}
}
class Chair {
String name;
String style;
String material;
float price;
public Chair(String name, String style, String material, float price) {
this.name = name;
this.style = style;
this.material = material;
this.price = price;
}
}
class Table {
String name;
String style;
String material;
float price;
public Table(String name, String style, String material, float price) {
this.name = name;
this.style = style;
this.material = material;
this.price = price;
}
}
class Sofa {
String name;
String style;
String material;
float price;
public Sofa(String name, String style, String material, float price) {
this.name = name;
this.style = style;
this.material = material;
this.price = price;
}
}
class FurnitureCreator {
private FurnitureFactory factory;
public void setFactory(FurnitureFactory factory) {
this.factory = factory;
}
public Chair createChair() {
return factory.createChair();
}
public Table createTable() {
return factory.createTable();
}
public Sofa createSofa() {
return factory.createSofa();
}
}
public class assignment5pt3 {
public static void main(String[] args) {
FurnitureCreator creator = new FurnitureCreator();
creator.setFactory(new ModernWoodFactory());
Chair modernWoodChair = creator.createChair();
Table modernWoodTable = creator.createTable();
Sofa modernWoodSofa = creator.createSofa();
creator.setFactory(new TraditionalMetalFactory());
Chair traditionalMetalChair = creator.createChair();
Table traditionalMetalTable = creator.createTable();
Sofa traditionalMetalSofa = creator.createSofa();
creator.setFactory(new IndustrialGlassFactory());
Chair industrialGlassChair = creator.createChair();
Table industrialGlassTable = creator.createTable();
Sofa industrialGlassSofa = creator.createSofa();
System.out.println("Modern Wood Chair: " + modernWoodChair);
System.out.println("Modern Wood Table: " + modernWoodTable);
System.out.println("Modern Wood Sofa: " + modernWoodSofa);
System.out.println("Traditional Metal Chair: " + traditionalMetalChair);
System.out.println("Traditional Metal Table: " + traditionalMetalTable);
System.out.println("Traditional Metal Sofa: " + traditionalMetalSofa);
System.out.println("Industrial Glass Chair: " + industrialGlassChair);
System.out.println("Industrial Glass Table: " + industrialGlassTable);
System.out.println("Industrial Glass Sofa: " + industrialGlassSofa);
}
}