forked from bitlather/academy-pgh-sessions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2017-02-28-oop-chaining.js
47 lines (40 loc) · 1.05 KB
/
2017-02-28-oop-chaining.js
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
class CoffeeOrder {
constructor() {
this.sugar = 0;
this.cream = 0;
this.base_price = 1.00;
this.sugar_price = 0.15;
this.cream_price = 0.30;
}
static Make() {
return new CoffeeOrder();
}
AddSugar() {
this.sugar++;
return this;
}
AddCream() {
this.cream++;
return this;
}
PrintReceipt() {
var value = "Coffee ($" + this.base_price.toFixed(2) + ")";
var total_price = this.base_price;
for (var i = 0; i < this.sugar; i++) {
value += "\n + Sugar ($" + this.sugar_price.toFixed(2) + ")";
total_price += this.sugar_price;
}
for (var i = 0; i < this.cream; i++) {
value += "\n + Cream ($" + this.cream_price.toFixed(2) + ")";
total_price += this.cream_price;
}
value += "\n = TOTAL: $" + total_price.toFixed(2);
alert(value);
}
}
var c = CoffeeOrder
.Make()
.AddSugar()
.AddCream()
.AddSugar()
.PrintReceipt();