Changed es5

This commit is contained in:
2024-05-27 11:52:51 +02:00
parent 6030ba0b73
commit aad7db4a55

View File

@@ -71,22 +71,20 @@ impl BankAccount {
pub fn deposit(&mut self, amount: f32) { pub fn deposit(&mut self, amount: f32) {
if amount > 0.0 { if amount > 0.0 {
self.balance += amount; self.state.clone().deposit(self, amount);
self.set_state(); self.set_state();
} }
} }
pub fn withdraw(&mut self, amount: f32) { pub fn withdraw(&mut self, amount: f32) {
if amount > 0.0 { if amount > 0.0 {
let state = Rc::clone(&mut self.state); self.state.clone().withdraw(self, amount);
state.withdraw(self, amount);
self.set_state(); self.set_state();
} }
} }
pub fn pay_interest(&mut self) { pub fn pay_interest(&mut self) {
let state = Rc::clone(&mut self.state); self.state.clone().pay_interest(self);
state.pay_interest(self);
self.set_state(); self.set_state();
} }
@@ -110,16 +108,16 @@ pub mod states {
use super::BankAccount; use super::BankAccount;
pub trait AccountState: Debug { pub trait AccountState: Debug {
fn withdraw(&self, account: &mut BankAccount, amount: f32); fn deposit(&self, account: &mut BankAccount, amount: f32) {
fn pay_interest(&self, account: &mut BankAccount); account.balance += amount;
}
fn withdraw(&self, _account: &mut BankAccount, _amount: f32) {}
fn pay_interest(&self, _account: &mut BankAccount) {}
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Red; pub struct Red;
impl AccountState for Red { impl AccountState for Red {}
fn withdraw(&self, _account: &mut BankAccount, _amount: f32) {}
fn pay_interest(&self, _account: &mut BankAccount) {}
}
#[derive(Debug)] #[derive(Debug)]
pub struct Silver; pub struct Silver;
@@ -127,7 +125,6 @@ pub mod states {
fn withdraw(&self, account: &mut BankAccount, amount: f32) { fn withdraw(&self, account: &mut BankAccount, amount: f32) {
account.balance -= amount; account.balance -= amount;
} }
fn pay_interest(&self, _account: &mut BankAccount) {}
} }
#[derive(Debug)] #[derive(Debug)]