-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathstore.js
More file actions
98 lines (83 loc) · 2.29 KB
/
store.js
File metadata and controls
98 lines (83 loc) · 2.29 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
"use strict";
class Customer {
constructor(data, movies) {
this._data = data;
this._movies = movies;
}
get name() {return this._data.name; }
get rentals() {return this._data.rentals.map(r => new Rental(r, this._movies)); }
get totalFrequentRenterPoints() {
return this.rentals.map(r => r.frequentRentalPoints )
.reduce( (a, b) => a + b );
}
get totalAmount() {
return this.rentals.reduce((total, r) => total + r.amount, 0);
}
}
class Rental {
constructor(data, movies) {
this._data = data;
this._movies = movies;
}
get days() {return this._data.days;}
get movieID() {return this._data.movieID; }
get movie() {
return this._movies[this.movieID]
}
get frequentRentalPoints() {
return (this.movie.code === "new" && this.days > 2) ? 2 : 1;
}
get amount() {
let thisAmount = 0;
switch (this.movie.code) {
case "regular":
thisAmount = 2;
if (this.days > 2) {
thisAmount += (this.days - 2) * 1.5;
}
break;
case "new":
thisAmount = this.days * 3;
break;
case "childrens":
thisAmount = 1.5;
if (this.days > 3) {
thisAmount += (this.days - 3) * 1.5;
}
break;
}
return thisAmount;
}
}
function statement(customerArg, movies) {
const customer = new Customer(customerArg, movies);
let result = `Rental Record for ${customer.name}\n`;
for (let rental of customer.rentals) {
result += `\t${rental.movie.title}\t${rental.amount}\n`;
}
result += `Amount owed is ${customer.totalAmount}\n`;
result += `You earned ${customer.totalFrequentRenterPoints} frequent renter points\n`;
return result;
}
let customer = {
name: "martin",
rentals: [{
"movieID": "F001",
"days": 3
}, {
"movieID": "F002",
"days": 1
},]
};
let movies = {
"F001": {
"title": "Ran",
"code": "regular"
},
"F002": {
"title": "Trois Couleurs: Bleu",
"code": "regular"
},
// etc
};
console.log(statement(customer, movies));