Created
April 29, 2021 21:44
-
-
Save sameo/0f493b27fce56d70b6cb7070c9d56f55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Clone, Debug, Default)] | |
struct Account { | |
transactions: Vec<i32>, | |
balance: i32, | |
} | |
impl Account { | |
fn new() -> Self { | |
Account { | |
balance: 0, | |
..Default::default() | |
} | |
} | |
fn withdrawal(&mut self, request: u32) -> u32 { | |
if self.balance <= 0 { | |
return 0; | |
} | |
let balance = self.balance as u32; | |
let withdrawal = if balance <= request { | |
self.balance | |
} else { | |
request as i32 | |
}; | |
self.transactions.push(-withdrawal); | |
self.balance -= withdrawal; | |
withdrawal as u32 | |
} | |
fn deposit(&mut self, deposit: i32) { | |
self.transactions.push(deposit); | |
self.balance += deposit; | |
} | |
fn balance(&self) -> i32 { | |
self.balance | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment