-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
143 lines (127 loc) · 4.18 KB
/
main.js
File metadata and controls
143 lines (127 loc) · 4.18 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
const prompt = require('prompt-sync')();
const {PartitionedAcct, Bucket, api} = require('./account.js');
class ControlPanel {
constructor() {
this.account = null;
}
setAccount() {
this.id = prompt('What is your account ID? ');
console.log(`Loading account #${this.id}...\n`);
try {
this.account = PartitionedAcct.loadFromFile(`accounts/acct${this.id}.json`);
return true;
} catch (error) {
console.log("Account does not exist!");
let inputOption = prompt("Would you like to make one with this id? [y/n/q]: ");
switch (inputOption.toLowerCase()) {
case 'y':
return this.makeAcct();
case 'q':
return false;
default:
return this.setAccount();
}
}
}
printMenu() {
console.log("\n-=+=-")
console.log("Menu:");
console.log("p - Print Account");
console.log("m - Make Account");
console.log("a - Add Bucket")
console.log("r - Remove Bucket")
console.log("t - Transfer Funds")
console.log("c - Call API");
console.log("q - Save and Quit\n");
}
printAcct() {
console.log(this.account.toString())
}
makeAcct() {
this.account = new PartitionedAcct();
this.addBucket()
while(true) {
let userChoice = prompt("Create another bucket? [y/n]: ");
if (userChoice.toLowerCase() != "y") break;
this.addBucket()
}
return true;
}
addBucket() {
let bucketName = prompt("Bucket Name? ");
let bucketPercentage = parseInt(prompt("Bucket's Percentage of Account? (0, 100) "));
try {
this.account.addBucket(bucketName.toLowerCase(), bucketPercentage, 0);
console.log("Bucket added successfully!")
} catch (error) {
console.log(`${error}`)
}
}
removeBucket() {
let bucketName = prompt("Name of Bucket You'd Like to Remove? ");
if (prompt("Are you sure you want to delete this bucket? [y/n] ").toLowerCase() != 'y') {
console.log("Remove Process Aborted!");
return;
}
try {
this.account.removeBucket(bucketName.toLowerCase())
console.log("Bucket Removed Successfully!");
} catch (error) {
console.log(`${error}`);
}
}
transferFunds() {
let bucketFrom = prompt("Transfering From (Bucket Name): ").toLowerCase();
let bucketTo = prompt("To: ").toLowerCase();
let amount = parseFloat(prompt("Amount? ")) * 100;
try {
this.account.transferFunds(bucketFrom, bucketTo, amount)
console.log("Transfer Successful!")
} catch (error) {
console.log(`${error}`)
}
}
callAPI() {
api(this.account);
}
run() {
console.log("Welcome to Bucketdrop!")
if (!this.setAccount()) return;
this.printAcct();
this.printMenu();
while (true) {
let inputOption = prompt("Enter an option: ");
switch (inputOption.toLowerCase()) {
case 'p':
this.printAcct();
break;
case 'm':
this.makeAcct();
break;
case 'a':
this.addBucket();
break;
case 'r':
this.removeBucket();
break;
case 't':
this.transferFunds();
break;
case 'c':
this.callAPI();
break;
case 'q':
case 's':
this.account.saveToFile(`accounts/acct${this.id}.json`);
console.log("Account Saved")
return;
default:
console.log("Invalid option. Try again.");
continue;
}
this.printMenu();
}
}
}
terminal = new ControlPanel();
terminal.run();