diff --git a/src/food_reg.rs b/src/food_reg.rs new file mode 100644 index 0000000..cae0f01 --- /dev/null +++ b/src/food_reg.rs @@ -0,0 +1,32 @@ +use std::time::{Duration, SystemTime}; + +#[derive(Debug, Clone)] +#[allow(dead_code)] +pub enum categories1 { + rice, + beans, + drinks, + snacks, + swallow, +} + +#[derive(Debug, Clone)] +#[allow(dead_code)] +pub struct FoodType { + pub name: String, + pub price: u32, + pub is_vegan: bool, + pub category: categories1, + pub expiration: SystemTime, +} + +pub fn expiration(Food_list: Vec) -> Result<(), String> { + let current_time = SystemTime::now(); + + for food in Food_list { + if current_time > food.expiration { + return Err(String::from("Food has expired")); + } + } + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 4ed90b2..5266a4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -82,35 +82,12 @@ fn main() { // user_name("Chris".to_string()); // user_name("Emma".to_string()); - // sub(20, 10); - - // user("Mark", 23, "mark@gmail.co".to_string(), true); - - // conditionals(); - - school_conditionals(); - loops(); - while_loop(); -} - -fn user_name(name: String) { - println!("My user name is {}", name) -} - -fn add(a: u32, b: u32) -> u32 { - let sum = a + b; - - println!("The sum of {a} and {b} is {sum}"); - return sum; -} - -fn sub(a: u32, b: u32) -> u32 { - let sum = a - b; - add(a, b); - - println!("The sum of {a} and {b} is {sum}"); - return sum; -} +// fn shadowing() { +// let x = 5; +// let x = 5 + 4; +// let x = x * 3; +// println!("{}", x); +// } fn user(name: &str, age: u32, email: String, is_active: bool) -> String { println!( @@ -155,23 +132,43 @@ fn school_conditionals() { fn loops() { let mut count = 0; - let result = loop { - count += 1; + let foodcart2 = FoodType { + name: String::from("beans"), + price: 2000, + is_vegan: true, + category: categories1::beans, + expiration: SystemTime::now() + std::time::Duration::from_secs(60 * 60 * 24), // Expires in 1 day + }; - if count == 10 { - break count * 2; - } - println!("Infinite loop {}", count); + let foodcart3 = FoodType { + name: String::from("snacks"), + price: 2000, + is_vegan: false, + category: categories1::snacks, + expiration: SystemTime::now() + std::time::Duration::from_secs(60 * 60 * 24), // Expires in 1 day }; - println!("The result is {:?}", result); -} + let foodcart4 = FoodType { + name: String::from("coke"), + price: 2000, + is_vegan: true, + category: categories1::drinks, + expiration: SystemTime::now() + std::time::Duration::from_secs(60 * 60 * 24), // Expires in 1 day + }; -fn while_loop() { - let mut count = 6; + let foodcart5 = FoodType { + name: String::from("amala"), + price: 2000, + is_vegan: false, + category: categories1::swallow, + expiration: SystemTime::now() + std::time::Duration::from_secs(60 * 60 * 24), // Expires in 1 day + }; - while count != 0 { - println!("The count is {}", count); - count -= 1; + let foods_list: Vec = vec![foodcart1, foodcart2, foodcart3, foodcart4, foodcart5]; + //let food_category: Vec = foods_list.into_iter().map(|food| food.category).collect(); + //println!("categories: {:?}", food_category); + match food_reg::expiration(foods_list) { + Ok(_) => println!("All food items are fresh!"), + Err(e) => println!("Error: {}", e), } } diff --git a/src/mk_calculator.rs b/src/mk_calculator.rs new file mode 100644 index 0000000..f3dc515 --- /dev/null +++ b/src/mk_calculator.rs @@ -0,0 +1,41 @@ +use std::io; + +pub fn run() { + println! ("Enter calculation (5 + 3):"); + + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Fail to read input"); + let parts: Vec<&str> = input.trim().split_whitespace().collect(); + + if parts.len() != 3 { + println!("Invalid fomat. Use: number operator number"); + return; + } + + let num1: f64 = parts[0].parse().expect("Invalid number"); + let operator = parts[1]; + let num2: f64 = parts[2].parse().expect("Invalid number"); + + let result = match operator { + "+" => num1 + num2, + "-" => num1 - num2, + "*" => num1 * num2, + "/" => { + if num2 == 0.0{ + println!("Cannot divide by zero"); + return; + } + num1 / num2 + } + _=> { + println!("Invalide Operator"); + return; + } + + }; + + println! ("Result: {}", result); + +} \ No newline at end of file