Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/food_reg.rs
Original file line number Diff line number Diff line change
@@ -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<FoodType>) -> 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(())
}
81 changes: 39 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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<FoodType> = vec![foodcart1, foodcart2, foodcart3, foodcart4, foodcart5];
//let food_category: Vec<categories1> = 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),
}
}
41 changes: 41 additions & 0 deletions src/mk_calculator.rs
Original file line number Diff line number Diff line change
@@ -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);

}