diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 47 | ||||
-rw-r--r-- | src/main.rs | 12 |
2 files changed, 38 insertions, 21 deletions
@@ -12,12 +12,13 @@ pub struct Mortgage { impl fmt::Display for Mortgage { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( + writeln!( f, - "{:3} | {:5} | {:4.2}\t{:.2}\t{:.2}\t{:.2}", + "{:4} | {:5} | {:4.2}\t({:.2} + {:.2})\t{:.2}", if self.period % 12 == 0 { format!("Y{}", (self.period / 12) + 1) } else { + // return Ok(()); "".to_string() }, self.period, @@ -42,7 +43,7 @@ impl Mortgage { } } - pub fn run(&mut self, updates: HashMap<u32, MortgageUpdate>) { + pub fn run(&mut self, updates: MortgageUpdates) { let principal = self.principal; let topay_total = self.quota * self.quotas as f64; @@ -53,9 +54,29 @@ impl Mortgage { let mut payed_interest = 0.; let mut payed_amortized = 0.; - println!("Año | Cuota | c\t\tIn\tAn\tCn"); + println!("\n========================================================"); + println!( + "=== HIPOTECA: {}€, A {} AÑOS, INTERÉS FIJO {:.2}% ===", + self.principal, + self.quotas / 12, + self.i12 * 12. * 100., + ); + println!("========================================================"); + + println!("\n\n# A PRIORI\n"); + println!( + "== Total a pagar: {:.2} ({} cap + {topay_interest:.2} int)", + topay_total, principal + ); + println!( + "== Los intereses suponen un {:.2}% del total", + 100. * topay_interest / topay_total + ); + + println!("\n\n# SIMULACIÓN CUOTAS\n"); + println!("Year | Month | Quota\t(Intrst + Amrtzd)\tPrincipal"); while self.quotas > 0 && self.principal > 0. { - println!("{self}"); + print!("{self}"); if let Some(update) = updates.get(&self.period) { payed_amortized += self.update(update); } @@ -67,17 +88,7 @@ impl Mortgage { payed_total += payed_amortized; - println!("\n# A PRIORI"); - println!( - "== Total a pagar: {:.2} ({} cap + {topay_interest:.2} int)", - topay_total, principal - ); - println!( - "== Los intereses suponen un {:.2}% del total", - 100. * topay_interest / topay_total - ); - - println!("\n# A POSTERIORI"); + println!("\n\n# RESULTADO FINAL\n"); println!( "== Total pagado: {:.2} ({:.2} cap + {:.2} int + {:.2} amortizado)", payed_total, payed_principal, payed_interest, payed_amortized @@ -86,7 +97,7 @@ impl Mortgage { "== Los intereses suponen un {:.2}% del total", 100. * payed_interest / payed_total ); - println!("\n"); + println!(); } fn update(&mut self, update: &MortgageUpdate) -> f64 { @@ -120,6 +131,8 @@ impl Mortgage { } } +pub type MortgageUpdates = HashMap<u32, MortgageUpdate>; + pub enum MortgageUpdate { Amortize(f64), } diff --git a/src/main.rs b/src/main.rs index bbe01af..c114f2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,16 @@ use hiccup::Mortgage; -use hiccup::MortgageUpdate::{self, *}; +use hiccup::MortgageUpdate::*; +use hiccup::MortgageUpdates; use std::collections::HashMap; fn main() { - let mut updates: HashMap<u32, MortgageUpdate> = - HashMap::from_iter((1..29).map(|y| (y * 12, Amortize(24_000.)))); - updates.insert(0, Amortize(15_000.)); let m = Mortgage::new(390_000., 0.028, 30); + + let updates: MortgageUpdates = HashMap::from_iter((0..29).map(|y| match y { + 0 => (0, Amortize(30_000.)), + _ => (y * 12, Amortize(12_000.)), + })); m.clone().run(updates); + // m.clone().run(HashMap::new()); } |