-
Notifications
You must be signed in to change notification settings - Fork 11
/
coffee_machine.ts
121 lines (93 loc) · 1.96 KB
/
coffee_machine.ts
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
import { Lang, en, fr } from "./languages";
export class CoffeeMachine {
started: boolean;
coffeeServed: boolean;
settingsDisplayed: boolean;
message: String;
lang: Lang;
water: number;
beans: number;
grounds: number;
countdownToDescale: number;
waterHardness: number;
grinder: String;
public constructor() {
this.started = false;
this.coffeeServed = false;
this.message = "";
this.lang = en;
this.settingsDisplayed = false;
this.grounds = 0;
this.beans = 40;
this.water = 60;
this.countdownToDescale = 500;
this.waterHardness = 2;
this.grinder = "medium";
}
start(lang: String) {
this.started = true;
this.lang = this.getLang(lang);
}
getLang(lang: String) {
if (lang == "fr") {
return fr;
}
return en;
}
stop() {
this.started = false;
}
getMessage() {
if (! this.started ) {
return '';
}
if (this.settingsDisplayed) {
return this.lang.settings;
}
if (this.water <= 10) {
return this.lang.tank;
}
if (this.beans < 3) {
return this.lang.beans;
}
if (this.grounds >= 30) {
return this.lang.grounds;
}
if (this.isDescalingNeeded()) {
return this.lang.descale;
}
return this.lang.ready;
}
takeCoffee() {
if (this.water == 0 || this.beans == 0) {
this.coffeeServed = false;
} else {
this.coffeeServed = true;
this.water -= 1;
this.beans -= 1;
this.grounds += 1;
this.countdownToDescale -= 1;
}
}
emptyGrounds() {
this.grounds = 0;
}
fillBeans() {
this.beans = 40;
}
fillTank() {
this.water = 60;
}
showSettings() {
this.settingsDisplayed = true;
}
getSettings() {
let settings = new Map();
settings.set('waterHardness', this.waterHardness);
settings.set('grinder', this.grinder);
return settings;
}
isDescalingNeeded() {
return this.countdownToDescale <= 0
}
}