-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
129 lines (111 loc) · 3.59 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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_freq() -> usize {
loop {
println!("Enter the number of times the interest is compounded per year:");
let mut freq = String::new();
io::stdin()
.read_line(&mut freq)
.expect("Failed to read line.");
let freq_trim = freq.trim().parse::<usize>();
if test_int(&freq_trim) { break freq_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() };
}
}
fn calculate_compound_interest(principal: f64, rate: f64, freq: usize, years: usize) -> f64 {
let exp = (freq * years) as f64;
let interest = principal * (1.0 + ((rate / 100.0) / freq as f64)).powf(exp);
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 freq = get_freq();
let years = get_years();
for i in 1..=years {
let interest = calculate_compound_interest(principal, rate, freq, 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);
}
}
}