-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathscript.js
70 lines (56 loc) · 1.76 KB
/
script.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
function store(name, list, earnings) {
(this.name = name), (this.list = list), (this.earnings = earnings);
}
//create instance of store
let sampleStore = new store("Avion Store", [], 0);
function book(title, quantity, value) {
(this.title = title), (this.quantity = quantity), (this.value = value);
}
let mybook = new book("Harry Potter", 5, 500);
store.prototype.addBook = function(title, quantity, value){
let newBook= new book(title, quantity, value)
this.list.push(newBook);
}
sampleStore.addBook("Cinder", 10, 300);
sampleStore.addBook("The Little Prince", 10, 300);
sampleStore.addBook("Lord of the RIngs", 2, 500);
store.prototype.restockBook = function (title, quantity) {
this.list.some((book) => {
if (book.title === title) {
book.quantity += quantity;
}
});
console.log(this.list);
};
sampleStore.restockBook("Cinder", 5);
sampleStore.restockBook("Harry Potter", 4);
// TODO
//sell book
store.prototype.sellBook = function (title, quantity) {
const bookIndex = this.list.findIndex((book) => book.title === title);
if (bookIndex !== -1) {
const {
title: StoreTitle,
quantity: Stock,
value: Price,
} = this.list[bookIndex];
if (Stock < quantity) {
console.log(`${StoreTitle} has only ${Stock} left`);
} else {
this.list[bookIndex].quantity -= quantity;
this.earnings += quantity * Price;
}
} else {
console.log(`We don't sell that book here`);
}
};
store.prototype.totalEarnings = function () {
console.log(`Store name is ${this.name} with earnings of ${this.earnings}`);
};
sampleStore.totalEarnings();
store.prototype.listInventory = function () {
this.list.map((book) => {
console.log(`${book.title}, ${book.quantity}, ${book.value}`);
});
};
sampleStore.listInventory();