Début monnaie
This commit is contained in:
parent
f8fb4a4821
commit
c50ec3ab4a
6
Session1/TP1/Monnaie/Cargo.toml
Normal file
6
Session1/TP1/Monnaie/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "monnaie"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
69
Session1/TP1/Monnaie/src/main.rs
Normal file
69
Session1/TP1/Monnaie/src/main.rs
Normal 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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user