- Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple-bank-system.rs
61 lines (52 loc) · 1.6 KB
/
simple-bank-system.rs
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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fnmain(){}
structSolution;
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
/**
* Your Bank object will be instantiated and called as such:
* let obj = Bank::new(balance);
* let ret_1: bool = obj.transfer(account1, account2, money);
* let ret_2: bool = obj.deposit(account, money);
* let ret_3: bool = obj.withdraw(account, money);
*/
structBank{
balance:Vec<i64>,
}
implBank{
fnnew(balance:Vec<i64>) -> Self{
Self{ balance }
}
fntransfer(&mutself,account1:i32,account2:i32,money:i64) -> bool{
if account1 asusize > self.balance.len() || account2 asusize > self.balance.len(){
returnfalse;
}
ifself.balance[account1 asusize - 1] >= money {
self.balance[account2 asusize - 1] += money;
self.balance[account1 asusize - 1] -= money;
true
}else{
false
}
}
fndeposit(&mutself,account:i32,money:i64) -> bool{
if account asusize > self.balance.len(){
returnfalse;
}
self.balance[account asusize - 1] += money;
returntrue;
}
fnwithdraw(&mutself,account:i32,money:i64) -> bool{
if account asusize > self.balance.len(){
returnfalse;
}
ifself.balance[account asusize - 1] >= money {
self.balance[account asusize - 1] -= money;
true
}else{
false
}
}
}