Self-Checkout
Create a simple self-checkout system. Prompt for the prices and quantities of several items. Calculate the subtotal, tax, and total using a tax rate of 5.5%.
This is a simple exercise. The main challenge is to decide when to stop reading user input.
Here we use match clause to check for invalid input and stop:
loop {
println!("Enter the price of item {}", count + 1);
let price = match read_value::<f64>(&mut stdin()) {
Ok(price) => price,
_ => break,
};
println!("Enter the quantity of item {}", count + 1);
let quantity = match read_value::<f64>(&mut stdin()) {
Ok(q) => q,
_ => break,
};
subtotal += price * quantity;
count += 1;
}