-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoffeeMakerApp.java
More file actions
215 lines (183 loc) · 6.43 KB
/
Copy pathCoffeeMakerApp.java
File metadata and controls
215 lines (183 loc) · 6.43 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import java.util.concurrent.locks.ReentrantLock;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
abstract class Appliance {
private String brand;
private boolean isOn;
public Appliance(String brand) {
this.brand = brand;
this.isOn = false;
}
public void turnOn() {
isOn = true;
System.out.println(brand + " appliance is now ON.");
}
public void turnOff() {
isOn = false;
System.out.println(brand + " appliance is now OFF.");
}
public boolean isOn() {
return isOn;
}
public String getBrand() {
return brand;
}
public abstract void operate();
}
class HeatingElement {
public void heat() {
System.out.println("Heating element is heating...");
}
}
class WaterTank {
private int waterLevel; // in milliliters
public WaterTank(int waterLevel) {
this.waterLevel = waterLevel;
}
public int getWaterLevel() {
return waterLevel;
}
public void addWater(int amount) {
waterLevel += amount;
System.out.println(amount + "ml water added. Current level: " + waterLevel + "ml.");
}
public void useWater(int amount) {
if (waterLevel >= amount) {
waterLevel -= amount;
System.out.println(amount + "ml water used. Remaining level: " + waterLevel + "ml.");
} else {
System.out.println("Not enough water!");
}
}
}
class CoffeeMaker extends Appliance {
private HeatingElement heatingElement;
private WaterTank waterTank;
private String brewStrength;
private final ReentrantLock operationLock = new ReentrantLock();
public CoffeeMaker(String brand, WaterTank waterTank) {
super(brand);
this.heatingElement = new HeatingElement();
this.waterTank = waterTank;
this.brewStrength = "Regular";
}
public String getBrewStrength() {
return brewStrength;
}
public void setBrewStrength(String brewStrength) {
this.brewStrength = brewStrength;
}
@Override
public void operate() {
if (!isOn()) {
System.out.println("Please turn ON the coffee maker first!");
return;
}
if (waterTank.getWaterLevel() < 100) {
System.out.println("Not enough water to brew coffee!");
return;
}
heatingElement.heat();
System.out.println("Brewing " + brewStrength + " coffee...");
waterTank.useWater(100);
logOperation("Brewed a cup of " + brewStrength + " coffee.");
}
public void logOperation(String message) {
try (FileWriter fw = new FileWriter("operations_log.txt", true);
PrintWriter pw = new PrintWriter(fw)) {
pw.println(message);
} catch (IOException e) {
System.out.println("Logging failed: " + e.getMessage());
}
}
public WaterTank getWaterTank() {
return waterTank;
}
public void performDiagnostics() {
new Thread(() -> {
operationLock.lock();
try {
checkWaterQuality();
checkFilter();
checkHeatingElement();
logOperation("Diagnostics completed successfully.");
} finally {
operationLock.unlock();
}
}).start();
}
private void checkWaterQuality() {
System.out.println("Checking water quality...");
if (waterTank.getWaterLevel() < 50) {
System.out.println("Warning: Low water quality detected (too little water)!");
} else {
System.out.println("Water quality is good.");
}
}
private void checkFilter() {
System.out.println("Checking coffee filter...");
System.out.println("Coffee filter is clean.");
}
private void checkHeatingElement() {
System.out.println("Checking heating element...");
System.out.println("Heating element functioning properly.");
}
}
public class CoffeeMakerApp {
public static void main(String[] args) {
WaterTank tank = new WaterTank(500);
CoffeeMaker maker = new CoffeeMaker("BrewMaster", tank);
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("\n===== Coffee Maker Menu =====");
System.out.println("1. Turn ON");
System.out.println("2. Turn OFF");
System.out.println("3. Brew Coffee");
System.out.println("4. Set Brew Strength");
System.out.println("5. Add Water");
System.out.println("6. Perform Diagnostics");
System.out.println("7. Exit");
System.out.print("Choose option: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
maker.turnOn();
maker.logOperation("Coffee maker turned ON.");
break;
case 2:
maker.turnOff();
maker.logOperation("Coffee maker turned OFF.");
break;
case 3:
maker.operate();
break;
case 4:
System.out.print("Enter brew strength (Mild/Regular/Strong): ");
scanner.nextLine();
String strength = scanner.nextLine();
maker.setBrewStrength(strength);
maker.logOperation("Brew strength set to: " + strength);
break;
case 5:
System.out.print("Enter water amount (ml): ");
int water = scanner.nextInt();
maker.getWaterTank().addWater(water);
maker.logOperation("Added " + water + "ml water.");
break;
case 6:
maker.performDiagnostics();
break;
case 7:
exit = true;
break;
default:
System.out.println("Invalid option!");
}
}
System.out.println("Goodbye!");
scanner.close();
}
}