use std::io::{self, stdin, Write};
mod help;
struct Info {
data: String,
num: f32,
}
impl Info {
fn is_empty(&mut self) -> bool {
if self.data.len() <= 1 {
self.data = "".to_string();
return true;
}
false
}
fn listen(&mut self) {
if stdin().read_line(&mut self.data).is_err() || self.is_empty() {
self.listen();
}
}
fn convert_to_int(&mut self) -> () {
match self.data.trim().parse::<f32>() {
Ok(num) => {
self.num = num;
}
Err(_) => {
println!("Number parsing error. Please use only digit values");
self.data = String::new();
self.listen();
self.convert_to_int();
}
};
}
fn run(&mut self, text: String) -> () {
println!("{text}");
self.listen();
self.convert_to_int();
}
fn new() -> Info {
Info {
data: String::new(),
num: 0.0,
}
}
}
#[allow(dead_code)]
fn clear(print_text: String) {
print!("\x1B[2J\x1B[H");
println!("{}", print_text);
io::stdout().flush().ok();
}
fn calc((balance, percent, times): (Info, Info, Info)) -> String {
let mut result = balance.num.clone();
for _ in 0..times.num as u32 {
result += (result * percent.num) / 100.0;
}
format!("{:.2} $", result)
}
fn main() {
let is_show = help::main();
if is_show { return (); };
let mut balance = Info::new();
let mut percent = Info::new();
let mut times = Info::new();
balance.run("Balance $".to_string());
percent.run("Percent %".to_string());
times.run("Times".to_string());
clear(calc((balance, percent, times)));
}