From ebbded265979cf8cf77d1233babad9af237c4813 Mon Sep 17 00:00:00 2001 From: markadrian6399 Date: Fri, 6 Mar 2026 13:00:59 +0100 Subject: [PATCH 1/2] food reg assigment --- src/food_reg.rs | 32 ++++++ src/main.rs | 268 ++++++++++++++++++++++++++----------------- src/mk_calculator.rs | 41 +++++++ 3 files changed, 234 insertions(+), 107 deletions(-) create mode 100644 src/food_reg.rs create mode 100644 src/mk_calculator.rs 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 e04bbfe..fb30fa1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,117 +1,171 @@ -const X: u32 = 5; +// mod mk_calculator; +mod food_reg; +use std::time::SystemTime; -fn shadowing() { - let x = 5; - let x = 5 + 4; - let x = x * 3; - println!("{}", x); -} - -fn main() { - let mut name = "John"; - println!("Hello, world! {}", X); - println!("{}", name); - - // let y = x + 23; - // X = 23; - name = "Jane"; - println!("{}", name); - println!(" ------ {}", X); - - // shadowing(); - let name = String::from("martin"); - // user_name(name); - // user_name("Chris".to_string()); - // user_name("Emma".to_string()); +use food_reg::{FoodType, categories1}; +// const X: u32 = 5; - // 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!("My user name is {}, \n age is {}, \n email is {}, \n is_active is {}", name, age, email, is_active); - return name.to_string(); -} - -fn conditionals() { - let age = 20; - - if age > 18 { - println!("You are an adult"); - } else if age == 18 { - println!("You just became an adult") - } else { - println!("you are a minor") - } -} - -fn school_conditionals() { - - let time: u32 = 19; - - if time < 8 { - println!("You're early!") - } else if time > 8 && time < 10 { - println!("You're late and should be punished!") - } else if time == 10 { - println!("It's break time!") - } else if time == 11 { - println!("Break Over, Go back to class!") - } else if time > 11 && time < 15 { - println!("You should be in class!") - } else if time == 15 { - println!("It's Closing time!") - } else { - println!("You can do what ever you want after closing!") - } -} - -fn loops() { - let mut count = 0; +fn main() { + // let mut name = "John"; + // println!("Hello, world! {}", X); + // println!("{}", name); + + // // let y = x + 23; + // // X = 23; + // name = "Jane"; + // println!("{}", name); + // println!(" ------ {}", X); + + // // shadowing(); + // let name = String::from("martin"); + // // user_name(name); + // // 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 user(name: &str, age: u32, email: String, is_active: bool) -> String { + // println!("My user name is {}, \n age is {}, \n email is {}, \n is_active is {}", name, age, email, is_active); + // return name.to_string(); + // } + + // fn conditionals() { + // let age = 20; + + // if age > 18 { + // println!("You are an adult"); + // } else if age == 18 { + // println!("You just became an adult") + // } else { + // println!("you are a minor") + // } + // } + + // fn school_conditionals() { + + // let time: u32 = 19; + + // if time < 8 { + // println!("You're early!") + // } else if time > 8 && time < 10 { + // println!("You're late and should be punished!") + // } else if time == 10 { + // println!("It's break time!") + // } else if time == 11 { + // println!("Break Over, Go back to class!") + // } else if time > 11 && time < 15 { + // println!("You should be in class!") + // } else if time == 15 { + // println!("It's Closing time!") + // } else { + // println!("You can do what ever you want after closing!") + // } + // } + + // fn loops() { + // let mut count = 0; + + // let result = loop { + // count += 1; + + // if count == 10 { + // break count * 2; + // } + // println!("Infinite loop {}", count); + // }; + + // println!("The result is {:?}", result); + // } + + // fn while_loop() { + // let mut count = 6; + + // while count != 0 { + // println!("The count is {}", count); + // count -= 1; + // } + + println!("food is ready"); + let foodcart1 = FoodType { + name: String::from("rice"), + price: 1000, + is_vegan: false, + category: categories1::rice, + expiration: SystemTime::now() + std::time::Duration::from_secs(60 * 60 * 24), // Expires in 1 day + }; - 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), } -} \ No newline at end of file +} 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 From bcc46379ef44aece31ec79f74581f9968bf28329 Mon Sep 17 00:00:00 2001 From: markadrian6399 Date: Fri, 6 Mar 2026 13:32:15 +0100 Subject: [PATCH 2/2] Add calculator functionality to main application --- src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.rs b/src/main.rs index fb30fa1..af4262a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ // mod mk_calculator; mod food_reg; +mod mk_calculator; use std::time::SystemTime; use food_reg::{FoodType, categories1}; @@ -121,6 +122,7 @@ fn main() { // } println!("food is ready"); + mk_calculator::run(); let foodcart1 = FoodType { name: String::from("rice"), price: 1000,