-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInitialCarRental.js
104 lines (88 loc) · 2.21 KB
/
InitialCarRental.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
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
class Car {
constructor(title, priceCode) {
this._title = title || null;
this._priceCode = priceCode || 0;
}
getPriceCode() {
return this._priceCode;
}
setPriceCode(arg) {
this._priceCode = arg;
}
getTitle() {
return this._title;
}
static get MUSCLE() {
return 2;
}
static get ECONOMY() {
return 0;
}
static get SUPERCAR() {
return 1;
}
}
class Rental {
constructor(car, daysRented) {
this._car = car || null;
this._daysRented = daysRented || 0;
}
getDaysRented() {
return this._daysRented;
}
getCar() {
return this._car;
}
}
class Customer {
constructor(name) {
this._name = name || null;
this._rentals = [];
}
addRental(rental) {
this._rentals.push(rental);
}
getName() {
return this._name;
}
billingStatement() {
let totalAmount = 0;
let frequentRenterPoints = 0;
let result = `Rental Record for ${this._name}\n`;
for (const rental of this._rentals) {
let thisAmount = 0;
switch (rental.getCar().getPriceCode()) {
case Car.ECONOMY:
thisAmount += 80;
if (rental.getDaysRented() > 2)
thisAmount += (rental.getDaysRented() - 2) * 30;
break;
case Car.SUPERCAR:
thisAmount += rental.getDaysRented() * 200;
break;
case Car.MUSCLE:
thisAmount += 200;
if (rental.getDaysRented() > 3)
thisAmount += (rental.getDaysRented() - 3) * 50;
break;
}
frequentRenterPoints++;
if (
rental.getCar().getPriceCode() === Car.SUPERCAR &&
rental.getDaysRented() > 1
)
frequentRenterPoints++;
result += `\t${rental.getCar().getTitle()}\t${thisAmount}\n`;
totalAmount += thisAmount;
}
result += `Final rental payment owed ${totalAmount}\n`;
result += `You received an additional ${frequentRenterPoints} frequent customer points`;
return result;
}
}
const rental1 = new Rental(new Car('Mustang', Car.MUSCLE), 5);
const rental2 = new Rental(new Car('Lambo', Car.SUPERCAR), 20);
const customer = new Customer('Ayran');
customer.addRental(rental1);
customer.addRental(rental2);
console.info(customer.billingStatement());