diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | Cargo.toml | 6 | ||||
-rw-r--r-- | src/lib.rs | 68 | ||||
-rw-r--r-- | src/main.rs | 4 |
5 files changed, 86 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..47e9b51 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "hiccup" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..645dcaa --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hiccup" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..a0e1e61 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,68 @@ +use std::fmt; + +#[derive(Debug)] +pub struct MortgageState { + period: u32, + capital: f64, + i12: f64, + quota: f64, + quotas: u32, +} + +impl fmt::Display for MortgageState { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "{:5} | {:4.2}\t{:.2}\t{:.2}\t{:.2}", + self.period, + self.quota, + self.interests(), + self.amortized(), + self.capital + ) + } +} + +pub fn quota(capital: f64, i12: f64, quotas: u32) -> f64 { + capital * i12 / (1.0 - (1.0 + i12).powi(-(quotas as i32))) +} + +impl MortgageState { + fn new(capital: f64, i1: f64, years: u32) -> Self { + let quotas = years * 12; + let i12 = i1 / 12.0; + MortgageState { + period: 0, + capital, + i12, + quota: quota(capital, i12, quotas), + quotas, + } + } + + pub fn step(&mut self) { + self.period += 1; + self.quotas -= 1; + self.capital = (1.0 + self.i12) * self.capital - self.quota; + } + + pub fn interests(&self) -> f64 { + self.i12 * self.capital + } + + pub fn amortized(&self) -> f64 { + self.quota - self.interests() + } +} + +pub fn run_mortgage(capital: f64, i12: f64, years: u32) { + let mut state = MortgageState::new(capital, i12, years); + + println!("Total a pagar: {}\n\n", state.quota * state.quotas as f64); + + println!("Cuota | c\tIn\tAn\tCn"); + while state.quotas > 0 { + println!("{state}"); + state.step(); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d4ee7bc --- /dev/null +++ b/src/main.rs @@ -0,0 +1,4 @@ +fn main() { + // hiccup::run_mortgage(390_000., 0.028, 30); + hiccup::run_mortgage(200_000., 0.01621, 30); +} |