use crate::{
Cash,
sanitize_negative,
};
pub trait Calculate {
fn add (&self, amount: f64) -> Self;
fn subtract (&self, amount: f64) -> Self;
fn multiply (&self, amount: f64) -> Self;
fn divide (&self, amount: f64) -> Self;
}
impl Calculate for Cash {
fn add (&self, amount: f64) -> Self {
let new_amount: f64 = &self.amount + amount;
Self {
currency: String::from(&self.currency),
amount: new_amount,
allow_negatives: self.allow_negatives,
log: self.log.clone(),
newlog: vec![],
}
}
fn subtract (&self, amount: f64) -> Self {
let new_amount: f64 = &self.amount - amount;
let sanitized_amount: f64 = if self.allow_negatives { new_amount } else { sanitize_negative(new_amount) };
Self {
currency: String::from(&self.currency),
amount: sanitized_amount,
allow_negatives: self.allow_negatives,
log: self.log.clone(),
newlog: vec![],
}
}
fn multiply (&self, amount: f64) -> Self {
let new_amount: f64 = &self.amount * amount;
Self {
currency: String::from(&self.currency),
amount: new_amount,
allow_negatives: self.allow_negatives,
log: self.log.clone(),
newlog: vec![],
}
}
fn divide (&self, amount: f64) -> Self {
let new_amount: f64 = &self.amount / amount;
let sanitized_amount: f64 = if self.allow_negatives { new_amount } else { sanitize_negative(new_amount) };
Self {
currency: String::from(&self.currency),
amount: sanitized_amount,
allow_negatives: self.allow_negatives,
log: self.log.clone(),
newlog: vec![],
}
}
}