Skip to content

Instantly share code, notes, and snippets.

@sameo
Created April 29, 2021 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sameo/0f493b27fce56d70b6cb7070c9d56f55 to your computer and use it in GitHub Desktop.
Save sameo/0f493b27fce56d70b6cb7070c9d56f55 to your computer and use it in GitHub Desktop.
#[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