Skip to content

Commit

Permalink
A trainee project for my Rust study which calculates BMI for each mmb…
Browse files Browse the repository at this point in the history
…er of a gim
  • Loading branch information
Mina-Nadifi committed Sep 20, 2023
1 parent e9c4583 commit b86945a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "Rust_BMI"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
74 changes: 74 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::io;
#[derive(Debug)]
struct Client {
name: String,
weight: f32,
height: f32,
diet: String,
condition: String,
}
impl Client {
fn bmi(&self) -> i32 {
//return self.weight / self.height.powf(2.0);
(self.weight * 10000.0 / (self.height * self.height)) as i32
}
/*fn longevity(&self) -> bool {
if self.diet == "vegan" {
true
} else {
false
}
}*/
fn health(&mut self) {
match self.bmi() {
x if x > 0 && x <= 18 => self.condition = "underweight range".to_string(),
x if x > 18 && x <= 25 => self.condition = "Healthy Weight range".to_string(),
x if x > 25 && x <= 30 => self.condition = "overweight range".to_string(),
_ => self.condition = "error".to_string(),
}
}
pub fn new() -> Self {
Self {
name: String::new(),
weight: 0.0,
height: 0.0,
diet: String::new(),
condition: String::new(),
}
}
}
fn main() {
let mut members = String::new();
let mut collection = Vec::new();
println!("Number of Clients you have:");
io::stdin()
.read_line(&mut members)
.expect("Entered members are invalid");
let members: u8 = members.trim().parse().unwrap();
for _i in 0..members {
let mut babe = Client::new();
let mut weight = String::new();
println!("Please enter the client's name");
io::stdin()
.read_line(&mut babe.name)
.expect("Entered name is invalid");
println!("Please enter the client weight");
io::stdin()
.read_line(&mut weight)
.expect("Entered weight is invalid");
babe.weight = weight.trim().parse().unwrap();
let mut height = String::new();
println!("Please enter the client height");
io::stdin()
.read_line(&mut height)
.expect("Entered height is invalid");
babe.height = height.trim().parse().unwrap();
println!("Please enter the client diet");
io::stdin()
.read_line(&mut babe.diet)
.expect("Entered diet is invalid");
babe.health();
collection.push(babe);
}
println!("{:?}", collection);
}

0 comments on commit b86945a

Please sign in to comment.