Created
July 17, 2023 13:15
-
-
Save michaellee8/52a8a508f1dfa474b5b88675f23838dc to your computer and use it in GitHub Desktop.
Comprehensive Rust Day 2 Morning Exercise Health Statistics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TODO: remove this when you're done with your implementation. | |
#[derive(Default)] | |
pub struct User { | |
name: String, | |
age: u32, | |
height: f32, | |
visit_count: u32, | |
last_blood_pressure: Option<(u32, u32)>, | |
} | |
pub struct Measurements { | |
height: f32, | |
blood_pressure: (u32, u32), | |
} | |
pub struct HealthReport<'a> { | |
patient_name: &'a str, | |
visit_count: u32, | |
height_change: f32, | |
blood_pressure_change: Option<(i32, i32)>, | |
} | |
trait ConvertInto<T> { | |
fn try_convert(&self) -> Option<T>; | |
} | |
impl ConvertInto<(i32, i32)> for (u32, u32) { | |
fn try_convert(&self) -> Option<(i32, i32)> { | |
Some((self.0.try_into().ok()?, self.1.try_into().ok()?)) | |
} | |
} | |
fn subtract_blood_pressure(bp1: Option<(u32, u32)>, bp2: (u32, u32)) -> Option<(i32, i32)> { | |
let bp1 = bp1?.try_convert()?; | |
let bp2 = bp2.try_convert()?; | |
Some((bp2.0 - bp1.0, bp2.1 - bp1.1)) | |
} | |
impl User { | |
pub fn new(name: String, age: u32, height: f32) -> Self { | |
Self { | |
name: name, | |
age: age, | |
height: height, | |
..Self::default() | |
} | |
} | |
pub fn name(&self) -> &str { | |
&self.name | |
} | |
pub fn age(&self) -> u32 { | |
self.age | |
} | |
pub fn height(&self) -> f32 { | |
self.height | |
} | |
pub fn doctor_visits(&self) -> u32 { | |
self.visit_count | |
} | |
pub fn set_age(&mut self, new_age: u32) { | |
self.age = new_age | |
} | |
pub fn set_height(&mut self, new_height: f32) { | |
self.height = new_height | |
} | |
pub fn visit_doctor(&mut self, measurements: Measurements) -> HealthReport { | |
self.visit_count += 1; | |
let height_change = measurements.height - self.height; | |
self.height = measurements.height; | |
let blood_pressure_change: Option<(i32, i32)> = | |
subtract_blood_pressure(self.last_blood_pressure, measurements.blood_pressure); | |
self.last_blood_pressure = Some(measurements.blood_pressure); | |
HealthReport { | |
patient_name: &self.name, | |
visit_count: self.visit_count, | |
height_change, | |
blood_pressure_change, | |
} | |
} | |
} | |
fn main() { | |
let bob = User::new(String::from("Bob"), 32, 155.2); | |
println!("I'm {} and my age is {}", bob.name(), bob.age()); | |
} | |
#[test] | |
fn test_height() { | |
let bob = User::new(String::from("Bob"), 32, 155.2); | |
assert_eq!(bob.height(), 155.2); | |
} | |
#[test] | |
fn test_set_age() { | |
let mut bob = User::new(String::from("Bob"), 32, 155.2); | |
assert_eq!(bob.age(), 32); | |
bob.set_age(33); | |
assert_eq!(bob.age(), 33); | |
} | |
#[test] | |
fn test_visit() { | |
let mut bob = User::new(String::from("Bob"), 32, 155.2); | |
assert_eq!(bob.doctor_visits(), 0); | |
let report = bob.visit_doctor(Measurements { | |
height: 156.1, | |
blood_pressure: (120, 80), | |
}); | |
assert_eq!(report.patient_name, "Bob"); | |
assert_eq!(report.visit_count, 1); | |
assert_eq!(report.blood_pressure_change, None); | |
let report = bob.visit_doctor(Measurements { | |
height: 156.1, | |
blood_pressure: (115, 76), | |
}); | |
assert_eq!(report.visit_count, 2); | |
assert_eq!(report.blood_pressure_change, Some((-5, -4))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment