-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurchase.js
149 lines (133 loc) · 6.06 KB
/
purchase.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
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
class Purchase {
constructor(totalPrice, isInternetConnection, phoneLines, selectedCellPhones) {
this.totalPrice = totalPrice;
this.isInternetConnection = isInternetConnection;
this.phoneLines = phoneLines;
this.selectedCellPhones = selectedCellPhones;
}
internetConnection(isInternetConnectionChecked) {
const internetConnectionPrice = 200;
if (typeof isInternetConnectionChecked !== 'boolean') {
throw new Error('isInternetConnectionChecked must be a boolean.');
}
this.isInternetConnection = isInternetConnectionChecked; // assign value to class attribute
if (this.isInternetConnection) {
this.totalPrice += internetConnectionPrice;
} else if (!this.isInternetConnection && this.totalPrice > 0) {
this.totalPrice -= internetConnectionPrice;
}
return this.totalPrice;
}
addPhoneLines() {
const phoneLinePrice = 150;
// non numerical and decimal numbers
if (typeof this.phoneLines !== 'number' || !Number.isInteger(this.phoneLines)) {
throw new Error('phoneLines must be an integer between 0 and 8.');
}
if (this.phoneLines < 0) {
throw new Error('The minimum number of phone lines that can be hired is 0.');
}
// covers cases between 0 and 7
if(this.phoneLines < 8) {
this.phoneLines++;
this.totalPrice += phoneLinePrice;
} else {
// > 8
throw new Error('The maximum number of phone lines that can be hired is 8.');
}
return this.totalPrice;
}
removePhoneLines() {
const phoneLinePrice = 150;
// non numerical and decimal numbers
if (typeof this.phoneLines !== 'number' || !Number.isInteger(this.phoneLines)) {
throw new Error('phoneLines must be an integer between 0 and 8.');
}
if (this.phoneLines > 8) {
throw new Error('The maximum number of phone lines that can be hired is 8.');
}
// covers cases between 0 and 8
if(this.phoneLines > 0) {
this.phoneLines--;
this.totalPrice -= phoneLinePrice;
} else {
// < 0
throw new Error('The minimum number of phone lines that can be hired is 0.');
}
return this.totalPrice;
}
selectCellPhone(modelName) {
const cellPhoneNames = ["Motorola G99", "iPhone 99", 'Samsung Galaxy 99', "Sony Xperia 99", "Huawei 99"];
const cellPhonePrices = [800, 6000, 1000, 900, 900]; // frequency array
// if the parameter received is not a string, then throw an error
if (typeof modelName !== 'string') {
throw new Error('The parameter modelName must be a string.');
}
// if the parameter received is not included in the array of Available Phones (cellPhoneNames), then throw an error
if(!cellPhoneNames.includes(modelName)) {
throw new Error('The Model Name must be one of the 5 available Models!');
}
// iterate through the cellPhoneNames array
// if we find an element in the array with the same name as the newly selected phone,
// then add the newly selected phone name in the array of selectedCellPhones
// increase the total price with the corresponding cost of the selected phone
for (let i = 0; i < cellPhoneNames.length; i ++) {
if (modelName === cellPhoneNames[i]) {
this.selectedCellPhones.push(modelName);
this.totalPrice += cellPhonePrices[i];
break;
}
}
return this.totalPrice;
}
unselectCellPhone(modelName) {
const cellPhoneNames = ["Motorola G99", "iPhone 99", 'Samsung Galaxy 99', "Sony Xperia 99", "Huawei 99"];
const cellPhonePrices = [800, 6000, 1000, 900, 900]; // frequency array
// if the parameter received is not a string, then throw an error
if (typeof modelName !== 'string') {
throw new Error('The parameter modelName must be a string.');
}
// if the parameter received is not included in the array of Available Phones (cellPhoneNames), then throw an error
if(!cellPhoneNames.includes(modelName)) {
throw new Error('The Model Name must be one of the 5 available Models!');
}
// iterate through the selectedCellPhones array
// if we find an element in the array with the same name as the newly selected phone,
// then delete the newly selected phone name in the array of selectedCellPhones
for (let i = 0; i < this.selectedCellPhones.length; i ++) {
if (modelName === this.selectedCellPhones[i]) {
this.selectedCellPhones.splice(i, 1); // delete an element from array at index i
break;
}
}
// iterate through the cellPhoneNames array
// if we find an element in the array with the same name as the newly selected phone,
// decrease the total price with the corresponding cost of the selected phone
for (let i = 0; i < cellPhoneNames.length; i ++) {
if (modelName === cellPhoneNames[i]) {
this.totalPrice -= cellPhonePrices[i];
break;
}
}
return this.totalPrice;
}
showBuyingReceipt() {
let receiptMessage = "";
if (this.isInternetConnection) {
receiptMessage += 'Internet Connection: ' + this.isInternetConnection + '\n';
}
if (this.phoneLines > 0) {
receiptMessage += 'Number of Phone Lines: ' + this.phoneLines + '\n';
}
if (this.selectedCellPhones.length > 0) {
receiptMessage += 'Cell Phones: ' + this.selectedCellPhones + '\n';
}
if (this.totalPrice !== 0) {
receiptMessage += 'Total Price: ' + this.totalPrice + ' DKK';
return receiptMessage;
}
receiptMessage += "Nothing is selected! Please select an item!";
return receiptMessage;
}
}
module.exports = Purchase;