-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbookStore.js
More file actions
executable file
·227 lines (221 loc) · 7.23 KB
/
bookStore.js
File metadata and controls
executable file
·227 lines (221 loc) · 7.23 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/node
const specialChars = [
'!', '@', '#', '$', '^', '&', '%',
'*', '(', ')', '+', '=', '-', '[',
']', '', '/', '{', '}', '|', ':',
'<', '>', '?', ',', '.', '_', '1',
'2', '3', '4', '5', '6', '7', '8',
'9', '0'
];
const fs = require("fs");
const jsonFileName = "./storeDate.json";
const firstUpper = (text) => text[0].toUpperCase() + text.slice(1);
const getJson = () => JSON.parse(fs.readFileSync(jsonFileName));
const saveJson = (json) => fs.writeFileSync(jsonFileName, JSON.stringify(json, null, 2));
class BookStore {
/*
With this class, you can create a store, display, sell and add books to it
*/
#minCharacters;
#name;
constructor(sName) { // store name
this.doneMakeDb = false;
this.#minCharacters = 6;
this.name = sName;
this.#addStore();
}
set name(newName) {
newName = firstUpper(newName);
if (newName.length >= this.#minCharacters && specialChars.indexOf(newName[0]) == -1) { // name bigger or equal minCharacters and first char in newName not specialChar
if (this.doneMakeDb){
if (!this.#storeIsExist(newName)) {
let data = getJson();
let storeIndex = this.#getStoreIndex();
data.stores[storeIndex].name = newName;
saveJson(data);
} else {
console.error("Name already exists");
return;
}
} else {
// pass
}
this.#name = newName;
} else {
return `Invalid name, '${newName}' must bigger or equal ${this.#minCharacters} character, and first character not special character or number`;
}
}
get name() {
return this.#name;
}
addBook(title, author, price, quantity) {
let checkArray = [title, author, price, quantity].map(
item => item != undefined); // Check if there is an missing parameter
if (!checkArray.every((i) => i == true)) {
return 'There is a missing parameter';
} else {
if (title.length < 5 || author.length < 5) {
return "title and author must biger 5";
} else if (price < 0 || quantity < 0){
return "price and quantity must biger 0";
} else {
let bookId = this.#getBookId();
let bookOb = {
id: bookId,
title: title,
author: author,
price: price,
quantity: quantity
}
this.#pushBook(bookOb);
return bookOb;
}
}
}
getBookById(id){
let books = this.getBooks();
let book = books.find(b => { // b = book
return b.id == id;
})
return book == undefined
? null
: book;
}
getBookByTitle(title){
let books = this.getBooks();
let book = books.find(b => { // b = book
return b.title == title;
})
return book == undefined
? null
: book;
}
getBooksByAuthor(author){
let books = this.getBooks();
let book = books.filter(b => { // b = book
return b.author == author;
})
return book.length == 0
? null
: book;
}
getBooks() {
let data = getJson();
let storeIndex = this.#getStoreIndex();
return data.stores[storeIndex].books;
}
updateBookPrice(idOrTitle, newPrice) {
if (newPrice >= 0) {
let data = getJson();
let storeIndex = this.#getStoreIndex();
let bookIndex = this.#getBookIndex(idOrTitle);
if (bookIndex != null) {
data.stores[storeIndex].books[bookIndex].price = newPrice;
saveJson(data);
return data.stores[storeIndex].books[bookIndex];
} else {
return "book not found";
}
} else {
return "Invalid price"
}
}
updateBookQuantity(idOrTitle, newQuantity) {
if (newQuantity >= 0) {
let data = getJson();
let storeIndex = this.#getStoreIndex();
let bookIndex = this.#getBookIndex(idOrTitle);
if (bookIndex != null) {
data.stores[storeIndex].books[bookIndex].quantity = newQuantity;
saveJson(data);
return data.stores[storeIndex].books[bookIndex];
} else {
return "book not found";
}
} else {
return "Invalid quantity"
}
}
sellBook(idOrTitle) {
let data = getJson();
let storeIndex = this.#getStoreIndex();
let bookIndex = this.#getBookIndex(idOrTitle);
let book = data.stores[storeIndex].books[bookIndex];
if (bookIndex != null) {
let quantity = data.stores[storeIndex].books[bookIndex].quantity;
if (quantity == 0) {
return "The book is out of stock";
} else {
data.stores[storeIndex].books[bookIndex].quantity--;
saveJson(data);
return this.#makeInvoice(book);
}
} else {
return "book not found";
}
}
#makeInvoice(book) {
return `Done sell.
\rTitle: ${book.title}
\rPrice: ${book.price}$
\rAuthor: ${book.author}`;
}
#pushBook(bookOb) {
let data = getJson();
let storeIndex = this.#getStoreIndex();
let booksTitle = this.getBooks().map(book => {
return book.title;
})
if (!booksTitle.includes(bookOb.title)) {
data.stores[storeIndex].books.push(bookOb);
} else {
return null;
}
saveJson(data);
}
#getStoreIndex = () => getJson().stores.findIndex(
store => store.name == this.name);
#getBookIndex = (idOrTitle) => {
let storeIndex = this.#getStoreIndex();
let stores = getJson().stores;
let booksIdOrTitle = stores[storeIndex].books.map(book => {
return (Number.isIntegar(idOrTitle))
? book.id
: book.title
})
let bookIndex = booksIdOrTitle.indexOf(idOrTitle);
return bookIndex == -1
? null
:bookIndex
}
#storeIsExist(storeName) {
storeName = (storeName == undefined) ? this.name :storeName;
let data = getJson();
let isExist = data.stores.some(store => store.name == storeName);
return isExist;
}
#addStore() {
let storeOb = {
name: this.name,
books: []
}
this.doneMakeDb = true;
let data = getJson();
if (!this.#storeIsExist()){
data.stores.push(storeOb);
saveJson(data);
return storeOb;
} else {
return null;
}
}
#getBookId() {
let books = this.getBooks();
if (books.length == 0) {
return 1;
}else {
let lastBookId = books[books.length - 1].id;
return lastBookId+1;
}
}
}