-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
115 lines (100 loc) · 3.3 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::{io, num::{ParseFloatError, ParseIntError}};
fn get_principal() -> f64 {
loop {
println!("Enter the principal:");
let mut principal = String::new();
io::stdin()
.read_line(&mut principal)
.expect("Failed to read line.");
let principal_trim = principal.trim().parse::<f64>();
if test_fin_float(&principal_trim) { break principal_trim.unwrap() };
}
}
fn get_rate() -> f64 {
loop {
println!("Enter interest rate as %:");
let mut rate = String::new();
io::stdin()
.read_line(&mut rate)
.expect("Failed to read line.");
let rate_trim = rate.trim().parse::<f64>();
if test_float(&rate_trim) { break rate_trim.unwrap() };
}
}
fn get_years() -> usize {
loop {
println!("Enter whole number of years:");
let mut years = String::new();
io::stdin()
.read_line(&mut years)
.expect("Failed to read line.");
let years_trim = years.trim().parse::<usize>();
if test_int(&years_trim) { break years_trim.unwrap() };
}
}
// Because this is 'simple interest' it does not take into account compounding like in real life
fn calculate_simple_interest(principal: f64, rate: f64, years: usize) -> f64 {
let interest = principal * (1.0 + ((rate / 100.0) * years as f64));
interest
}
fn test_fin_float(input: &Result<f64, ParseFloatError>) -> bool {
match input {
Ok(ok) => if ok > &0.0 {
if ok.to_string().contains('.') && ok.to_string().split('.').last().unwrap().len() > 2 {
println!("Number must not have more than 2 decimal places.");
return false;
} else {
return true;
}
} else {
println!("Number must positive.");
return false;
},
Err(..) => {
println!("Input is not a number.");
return false;
}
}
}
fn test_float(input: &Result<f64, ParseFloatError>) -> bool {
match input {
Ok(ok) => if ok > &0.0 {
return true;
} else {
println!("Number must positive.");
return false;
},
Err(..) => {
println!("Input is not a number.");
return false;
}
}
}
fn test_int(input: &Result<usize, ParseIntError>) -> bool {
match input {
Ok(ok) => if ok > &0 {
return true
} else {
println!("Number must be a positive whole number.");
return false;
},
Err(..) => {
println!("Input is not a number.");
return false;
}
}
}
fn main() {
let principal = get_principal();
let rate = get_rate();
let years = get_years();
// Supposed to round up, but even at 64-bit precision there are very tiny errors which should be 0
for i in 1..=years {
let interest = calculate_simple_interest(principal, rate, i);
if i == 1 {
println!("After {} year at {}%, the investment will be worth ${:.2}", i, rate, interest);
} else {
println!("After {} years at {}%, the investment will be worth ${:.2}", i, rate, interest);
}
}
}