-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud_oop.js
More file actions
544 lines (440 loc) · 15.1 KB
/
Copy pathcloud_oop.js
File metadata and controls
544 lines (440 loc) · 15.1 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//ALERTS
const accountCreatedAlert = "Account was added."
const accountAlreadyExistsAlert = "Account already exists."
const noAccountAlert = "Account does not exist."
const noAccountsAlert = "No accounts."
const accountWithLeastSpaceAlert = "Account with least free space:"
const uploadSuccessAlert = "File uploaded into account."
const fileAlreadyExistsAlert = "File already exists in the account."
const fileTooLargeAlert = "File size exceeds account capacity."
const fileSharedAlert = "File was shared."
const noFileAlert = "File does not exist."
const shareNotAllowedAlert = "Account does not allow file sharing."
const fileAlreadySharedAlert = "File already shared."
const fileWasNotSharedAlert = "File not shared."
const displayAllFilesAlert = "Account files:"
const displayAllAccountsAlert = "All accounts:"
const updateSuccessAlert = "File was updated."
const displayLastUpdateAlert = "Last update:"
const exitAlert = "Exiting..."
const invalidCommandAlert = "Invalid command."
//PROMPS
const addPrompt = "ADD: Create a new user account"
const uploadPrompt = "UPLOAD: Add a file to an account"
const sharePrompt = "SHARE: Share a file with another user"
const minspacePrompt = "MINSPACE: Find the account with the least free space"
const listfilesPrompt = "LISTFILES: Display a user's files"
const listallPrompt = "LISTALL: View all registered accounts"
const updatePrompt = "UPDATE: Simulate updating a file"
const lastUpdatePrompt = "LASTUPDATE: Retrieve the account that most recently updated a file"
const exitPrompt = "EXIT: Terminate the application"
const promptMsg = addPrompt+"\n"+uploadPrompt+"\n"+sharePrompt+"\n"+minspacePrompt+"\n"+listfilesPrompt+"\n"+listallPrompt+"\n"+updatePrompt+"\n"+lastUpdatePrompt+"\n"+exitPrompt
class Cloud {
#accounts = null;
constructor() {
this.#accounts = [];
}
get accounts() {
return this.#accounts;
}
getAccount(email) {
return this.accounts.find((account) => account.email === email)
}
addAccount(email, accountType) {
const newId = this.getIdNumber()
let newAccount
if (accountType === "basic") {
newAccount = new BasicAccount(email, newId)
} else if (accountType === "premium") {
newAccount = new PremiumAccount(email, newId)
}
this.accounts.push(newAccount);
}
displayAllAccounts() {
const listToDisplay = this.accounts.map((account) => `${account.email} (${account instanceof BasicAccount ? "Basic" : "Premium"})`);
return listToDisplay.join("\n");
}
getAccountWithLeastSpace() {
const accountWithLeastSpace = this.accounts.toSorted((accountA, accountB) => accountA.getAccountSpace() - accountB.getAccountSpace() || accountA.id - accountB.id)
return accountWithLeastSpace.length === 0 ? false : accountWithLeastSpace[0].email
}
getMostRecentId() {
return this.accounts.toSorted((accountA, accountB) => accountB.id - accountA.id)[0]
}
getIdNumber() {
const allAccounts = this.accounts
return allAccounts.length > 0 ? this.getMostRecentId(allAccounts).id + 1 : 0
}
getNewPrompt() {
const newInput = new Prompt();
this.useCommand(newInput);
}
addCommand(data) {
const [accountEmail, accountType] = data
const account = this.getAccount(accountEmail)
if (account) {
alert(accountAlreadyExistsAlert)
alert("")
} else {
this.addAccount(accountEmail, accountType)
alert(accountCreatedAlert)
alert("")
}
}
uploadCommand(data) {
const [fileOwnerEmail, fileName, fileSize] = data
const fileOwner = this.getAccount(fileOwnerEmail)
if (!fileOwner) {
alert(noAccountAlert)
alert("")
} else {
const uploadCheck = fileOwner.uploadFile(fileName, fileSize, fileOwner.email)
if (uploadCheck.error === true) {
alert(uploadCheck.errorMessage)
alert("")
} else {
alert(uploadSuccessAlert)
alert("")
}
}
}
shareCommand(data) {
const [ownerAccountEmail, receivingAccountEmail, sharedFileName] = data
const ownerAccount = this.getAccount(ownerAccountEmail)
const receivingAccount = this.getAccount(receivingAccountEmail)
if (!ownerAccount || !receivingAccount) {
alert(noAccountAlert)
alert("")
} else {
const sharedFile = ownerAccount.shareFile(sharedFileName)
if (sharedFile.error === true) {
alert(sharedFile.errorMessage)
alert("")
} else {
const fileReceivingCheck = receivingAccount.receiveSharedFile(sharedFile)
if (fileReceivingCheck.error === true) {
alert(fileReceivingCheck.errorMessage)
alert("")
} else {
alert(fileSharedAlert)
alert("")
}
}
}
}
minspaceCommand() {
const accountWithLeastSpace = this.getAccountWithLeastSpace();
if (!accountWithLeastSpace) {
alert(noAccountsAlert)
alert("")
} else {
alert(accountWithLeastSpaceAlert+" "+accountWithLeastSpace)
alert("")
}
}
listFilesCommand(data) {
const [filesOwnerEmail] = data
const filesOwner = this.getAccount(filesOwnerEmail)
if (!filesOwner) {
alert(noAccountAlert)
alert("")
} else {
const filesList = filesOwner.displayAccountFiles()
alert(displayAllFilesAlert+"\n"+filesList);
alert("")
}
}
listAllAccountsCommand() {
const accountsList = this.displayAllAccounts()
alert(displayAllAccountsAlert+"\n"+accountsList);
alert("")
}
updateFileCommand(data) {
const [fileOwnerEmail, updatingAccountEmail, fileName] = data
const fileOwner = this.getAccount(fileOwnerEmail);
const updatingAccount = this.getAccount(updatingAccountEmail);
if (!fileOwner || !updatingAccount) {
alert(noAccountAlert);
alert("")
} else if (!fileOwner.hasFile(fileName, fileOwner.email)) {
alert(noFileAlert);
alert("")
} else {
const file = fileOwner.getFile(fileName);
const updateFileCheck = updatingAccount.updateFile(file)
if (updateFileCheck) {
alert(updateSuccessAlert)
alert("")
} else {
alert(fileWasNotSharedAlert);
alert("")
}
}
}
lastUpdateCommand(data) {
const [fileOwnerEmail, fileName] = data
const fileOwner = this.getAccount(fileOwnerEmail)
if (!fileOwner) {
alert(noAccountAlert);
alert("")
} else {
const lastUpdateAuthor = fileOwner.getLastUpdateAuthor(fileName)
if (lastUpdateAuthor.error) {
alert(lastUpdateAuthor.errorMessage)
alert("")
} else {
alert(displayLastUpdateAlert+" "+lastUpdateAuthor)
alert("")
}
}
}
useCommand(input) {
const command = input.command
const data = input.data
switch(command) {
case "ADD":
this.addCommand(data)
break;
case "UPLOAD":
this.uploadCommand(data)
break;
case "SHARE":
this.shareCommand(data)
break;
case "MINSPACE":
this.minspaceCommand()
break;
case "LISTFILES":
this.listFilesCommand(data)
break;
case "LISTALL":
this.listAllAccountsCommand()
break;
case "UPDATE":
this.updateFileCommand(data)
break;
case "LASTUPDATE":
this.lastUpdateCommand(data)
break;
case "EXIT":
alert(exitAlert);
alert("")
return
default:
alert(invalidCommandAlert);
break;
}
const newInput = new Prompt()
this.useCommand(newInput)
}
}
class Prompt {
#prompt = null
#command = null
#data= null
constructor() {
this.#prompt = this.askPrompt();
this.#command = this.extractCommand(this.prompt)
this.#data = this.extractData(this.prompt)
}
get prompt() {
return this.#prompt
}
set command(newCommand){
this.#command = newCommand;
}
get command() {
return this.#command
}
set data(newData) {
this.#data = newData;
}
get data() {
return this.#data
}
askPrompt() {
const newPrompt = prompt(promptMsg)
return newPrompt
}
splitPrompt() {
return this.prompt.split(" ");
}
extractCommand() {
return this.prompt ? this.splitPrompt(this.prompt)[0] : false;
}
extractData() {
return this.prompt ? this.splitPrompt(this.prompt).slice(1) : false;
}
}
class Account {
#email = null;
#accountSpace = null;
#sharedFileWeight = null;
#id = null;
#files = null;
constructor(email, id) {
this.#email = email;
this.#accountSpace = 0;
this.#sharedFileWeight = 1;
this.#id = id;
this.#files = []
}
get email() {
return this.#email;
}
get files() {
return this.#files;
}
get accountSpace() {
return this.#accountSpace;
}
set accountSpace(space) {
this.#accountSpace = space;
}
get sharedFileWeight() {
return this.#sharedFileWeight
}
set sharedFileWeight(weight) {
this.#sharedFileWeight = weight
}
get id() {
return this.#id;
}
addFileToAccount(file) {
this.#files.push(file);
}
uploadFile(fileName, fileSize, fileOwnerEmail) {
if (this.hasFile(fileName, fileOwnerEmail)) {
return {error: true, errorMessage: fileAlreadyExistsAlert}
} else if (this.checkAvailableSpace(fileSize)) {
return {error: true, errorMessage: fileTooLargeAlert}
} else {
let file = new File(fileName, fileSize, fileOwnerEmail)
this.addFileToAccount(file)
return {error: false}
}
}
shareFile(sharedFileName) {
if (this instanceof BasicAccount) {
return {error: true, errorMessage: shareNotAllowedAlert}
}
const sharedFile = this.getFile(sharedFileName)
if (sharedFile) {
return sharedFile
} else {
return {error: true, errorMessage: noFileAlert}
}
}
getLastUpdateAuthor(fileName) {
if (!this.hasFile(fileName, this.email)) {
return {error: true, errorMessage: noFileAlert}
}
const file = this.getFile(fileName)
const lastUpdate = file.getMostRecentRecord()
return lastUpdate ? lastUpdate.author : this.email
}
hasFile(fileName, ownerEmail) {
const file = this.files.find((file) => file.name === fileName && file.ownerEmail === ownerEmail)
return file ? true : false
}
getFile(fileName) {
return this.filterOwnFiles().find((file) => file.name === fileName)
}
checkAvailableSpace(size) {
return this.getAccountSpace() < size
}
receiveSharedFile(file) {
const fileWeight = this.calculateSharedFileWeight(file.size)
if (this.hasFile(file.name, file.ownerEmail)) {
return {error: true, errorMessage: fileAlreadySharedAlert}
}
const fileTooLarge = this.checkAvailableSpace(fileWeight)
if (fileTooLarge) {
return {error: true, errorMessage: fileTooLargeAlert}
} else {
this.addFileToAccount(file)
return {error: false}
}
}
displayAccountFiles() {
const listToDisplay = this.files.map((file) => {
let shareStatus = ""
if (file.ownerEmail !== this.email) {
shareStatus = " (shared)"
}
return `${file.name} (${file.size} MB)${shareStatus}`
})
return listToDisplay.join("\n");
}
filterOwnFiles() {
return this.files.filter((file) => file.ownerEmail === this.email);
}
filterSharedFiles() {
return this.files.filter((file) => file.ownerEmail !== this.email);
}
getAccountSpace() {
const ownWeight = this.filterOwnFiles().reduce((accWeight, file) => accWeight + file.size, 0)
const sharedWeight = this.filterSharedFiles().reduce((accWeight, file) => accWeight + this.calculateSharedFileWeight(file.size), 0);
return this.accountSpace - (ownWeight + sharedWeight)
}
updateFile(file) {
if (!this.hasFile(file.name, file.ownerEmail)) {
return false
} else {
file.updateFileHistory(this.email);
return true
}
}
calculateSharedFileWeight(fileSize) {
return fileSize * this.sharedFileWeight
}
}
class BasicAccount extends Account{
constructor(email, id) {
super(email, id)
this.accountSpace = 2048
this.sharedFileWeight = 0.5
}
}
class PremiumAccount extends Account {
constructor(email, id) {
super(email, id)
this.accountSpace = 5120
this.sharedFileWeight = 0
}
}
class File {
#name = null;
#size = null;
#ownerEmail = null;
#updateHistory = null;
constructor(name, size, ownerEmail) {
this.#name = name;
this.#size = parseFloat(size);
this.#ownerEmail = ownerEmail;
this.#updateHistory = [];
}
get name() {
return this.#name;
}
get size() {
return this.#size;
}
get ownerEmail() {
return this.#ownerEmail;
}
get updateHistory() {
return this.#updateHistory;
}
updateFileHistory(accountEmail) {
const lastUpdate = this.getMostRecentRecord();
let nextId
lastUpdate ? nextId = lastUpdate.id + 1 : nextId = 0
this.#updateHistory.push({id: nextId, author: accountEmail})
}
getMostRecentRecord() {
return this.updateHistory.toSorted((recordA, recordB) => recordB.id - recordA.id)[0]
}
}
const newCloud = new Cloud()
newCloud.getNewPrompt()