use std::io::{self, Write};

fn main() {
    let mut object_name: String = String::new();
    let mut posted_price: f32 = 0.0;
    let mut paid_price: f32 = 0.0;

    print!("Enter the object's name: ");
    let _ = io::stdout().flush();
    io::stdin()
        .read_line(&mut object_name)
        .expect("failed to read from stdin");
    object_name = object_name.trim().to_string();

    let mut postedprice_isint: bool = false;
    while !postedprice_isint {
        print!("Enter the posted price: ");
        _ = io::stdout().flush();
        let mut input_text = String::new();
        io::stdin()
            .read_line(&mut input_text)
            .expect("failed to read from stdin");

        match input_text.trim().parse::<f32>() {
            Ok(i) => {
                posted_price = i;
                postedprice_isint = true;
            }
            Err(..) => println!("Input must be an integer!"),
        }
    }

    let mut paidprice_isint: bool = false;
    while !paidprice_isint {
        print!("Enter the paid price: ");
        _ = io::stdout().flush();
        let mut input_text = String::new();
        io::stdin()
            .read_line(&mut input_text)
            .expect("failed to read from stdin");

        match input_text.trim().parse::<f32>() {
            Ok(i) => {
                paid_price = i;
                paidprice_isint = true;
            }
            Err(..) => println!("Input must be an integer!"),
        }
    }

    println!("The object's name is: {}", object_name);
    println!("The posted price is: {:.2} $", posted_price);
    println!("The paid price is: {:.2} $", paid_price);
    let diff: f32 = posted_price - paid_price;
    println!("Difference between the prices: {:.2} $", diff);
    println!("Rebate percentage: {:.2} %", diff / posted_price * 100.0);
}
