Début monnaie

This commit is contained in:
thatscringebro 2024-09-28 11:49:02 -04:00
parent f8fb4a4821
commit c50ec3ab4a
2 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,6 @@
[package]
name = "monnaie"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,69 @@
use std::{collections::HashMap, io::{self, Write}};
fn main() {
let mut article_price: f32 = 0.0;
let mut paid_price: f32 = 0.0;
let mut monney: HashMap<String, f32> = HashMap::new();
let mut is_int: bool = false;
while !is_int {
print!("Please enter the article's price: ");
let _ = io::stdout().flush();
let mut input_text: String = String::new();
io::stdin()
.read_line(&mut input_text)
.expect("Failed to read from stdin");
match input_text.trim().parse::<f32>() {
Ok(i) => {
is_int = true;
article_price = i;
}
Err(..) => println!("Must be a number!"),
}
}
is_int = false;
while !is_int {
print!("Amount given by the customer: ");
let _ = io::stdout().flush();
let mut input_text: String = String::new();
io::stdin()
.read_line(&mut input_text)
.expect("Failed to read from stdin");
match input_text.trim().parse::<f32>() {
Ok(i) => {
is_int = true;
paid_price = i;
}
Err(..) => println!("Must be a number!"),
}
}
if paid_price < article_price {
println!("The customer has not given enough monney!!");
println!("Press enter to continue...");
let mut buffer = String::new();
std::io::stdin()
.read_line(&mut buffer)
.expect("Failed to read line");
return;
}
let mut remain = paid_price - article_price;
let mut total = 0.0;
for (k, v) in monney.iter() {
if *v != 0.0 {
println!("{} X {} $", v, k);
total += v;
}
}
println!("This makes a total of {:.0} coins and bills", total);
println!("Press enter to continue...");
let mut buffer = String::new();
std::io::stdin()
.read_line(&mut buffer)
.expect("Failed to read line");
return;
}