-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount.js
57 lines (44 loc) · 1.06 KB
/
Account.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
const FileSystem = require('./FileSystem')
module.exports = class Account {
constructor(name) {
this.#name = name
}
#name
#balance
get name() {
return this.#name
}
get balance() {
return this.#balance
}
get filePath() {
return `accounts/${this.name}.txt`
}
async #load() {
this.#balance = parseFloat(await FileSystem.read(this.filePath))
}
static async create(accountName) {
const account = new Account(accountName)
await FileSystem.write(account.filePath, 0)
account.#balance = 0
return account
}
static async find(accountName) {
const account = new Account(accountName)
try {
await account.#load()
return account
} catch (e) {
return
}
}
async deposit(amount) {
await FileSystem.write(this.filePath, this.#balance + amount)
this.#balance = this.#balance + amount
}
async withdraw(amount) {
if (this.balance < amount) throw new Error()
await FileSystem.write(this.filePath, this.#balance - amount)
this.#balance = this.#balance - amount
}
}