summaryrefslogtreecommitdiff
path: root/learn_3/src/main.rs
blob: c588672d314b1c427e3b488df64ed961f4fe9ec3 (plain)
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
struct Account {
    id: u32,
    balance: i64,
}

struct Bank {
    pub accounts: Vec<Account>,
}

impl Bank {
    pub fn new() -> Self {
        Self{
            accounts: Vec::new(),
        }
    }

    pub fn add(&mut self, account: Account) {
        if self.accounts.iter().any(|e| e.id == account.id) {
            // account is already added
            return;
        }
        self.accounts.push(account);
    }

    pub fn get(&self, id: u32) -> Option<&Account> {
        self.accounts.iter().find(|&x| x.id == id)
    }

    pub fn get_mut(&mut self, id: u32) -> Option<&mut Account> {
        self.accounts.iter_mut().find(|x| x.id == id)
    }

    pub fn transfer(&mut self, from: u32, to: u32, amount: i64) -> bool {
        if from == to || amount < 1 || self.get(to).is_none() {
            return false;
        }
        let from_account = match self.get_mut(from) {
            Some(account) => account,
            _ => return false,
        };
        if from_account.balance < amount {
            return false;
        }
        from_account.balance -= amount;        
        let to_account = match self.get_mut(to) {
            Some(account) => account,
            _ => return false,
        };
        if let Some(new_balance) = to_account.balance.checked_add(amount) {
            to_account.balance += amount;
            return true
        }
        false
    }
}

fn main() {
    ()
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn test_new() {
        assert_eq!(Bank::new().accounts.len(), 0)
    }

    #[test]
    fn test_add() {
        let mut bank = Bank::new();
        bank.add(Account{
            id:1,
            balance:1,
        });
        assert_eq!(bank.accounts.len(), 1);
        bank.add(Account{
            id:1,
            balance:1,
        });
        assert_eq!(bank.accounts.len(), 1);
        bank.add(Account{
            id:2,
            balance:1,
        });
        assert_eq!(bank.accounts.len(), 2);
    }

    #[test]
    fn test_get() {
        let mut bank = Bank::new();
        bank.add(Account{
            id:1,
            balance:1,
        });
        assert!(bank.get(1 as u32).is_some());
        assert!(bank.get(2 as u32).is_none());
    }

    #[test]
    fn test_get_mut() {
        let mut bank = Bank::new();
        bank.add(Account{
            id:1,
            balance:1,
        });
        assert!(bank.get_mut(1 as u32).is_some());
        assert!(bank.get_mut(2 as u32).is_none());
    }

    #[test]
    fn test_transfer() {
        let mut bank = Bank::new();
        let from_id: u32 = 1;
        bank.add(Account{
            id:from_id,
            balance:1,
        });
        let to_id: u32 = 2;
        bank.add(Account{
            id:to_id,
            balance:1,
        });

        assert_eq!(bank.transfer(from_id,to_id,1), true);
        assert!(bank.get(from_id).is_some_and(|a| a.balance == 0));
        assert!(bank.get(to_id).is_some_and(|a| a.balance == 2));

        // zero transfers not allowed
        assert_eq!(bank.transfer(from_id,to_id,0), false);

        // negative transfers not allowed
        assert_eq!(bank.transfer(from_id,to_id,-1), false);
        
        // from account depleted
        assert_eq!(bank.transfer(from_id,to_id,1), false);

        // account numbers the same
        assert_eq!(bank.transfer(from_id,from_id,1), false);

        // from account not found
        assert_eq!(bank.transfer(99,to_id,1), false);

        // to account not found
        assert_eq!(bank.transfer(from_id, 99, 1), false);

        let maxed_id: u32 = 3;
        bank.add(Account{
            id:to_id,
            balance:i64::MAX,
        });

        // do not let accounts overflow
        assert_eq!(bank.transfer(to_id, maxed_id, 1), false);

    }
    
}