Skip to content

Commit ea0e231

Browse files
authored
Create simple-bank-system.cpp
1 parent c485a98 commit ea0e231

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

C++/simple-bank-system.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Time: ctor: O(1)
2+
// transer: O(1)
3+
// deposit: O(1)
4+
// withdraw: O(1)
5+
// Space: O(1)
6+
7+
class Bank {
8+
public:
9+
Bank(vector<long long>& balance) : balance_(balance) {
10+
11+
}
12+
13+
bool transfer(int account1, int account2, long long money) {
14+
if (1 <= account2 && account2 <= size(balance_) && withdraw(account1, money)) {
15+
return deposit(account2, money);
16+
}
17+
return false;
18+
}
19+
20+
bool deposit(int account, long long money) {
21+
if (1 <= account && account <= size(balance_)) {
22+
balance_[account - 1] += money;
23+
return true;
24+
}
25+
return false;
26+
}
27+
28+
bool withdraw(int account, long long money) {
29+
if (1 <= account && account <= size(balance_) && balance_[account - 1] >= money) {
30+
balance_[account - 1] -= money;
31+
return true;
32+
}
33+
return false;
34+
}
35+
36+
private:
37+
vector<long long>& balance_;
38+
};

0 commit comments

Comments
 (0)