2023-09-26 09:29:00 +02:00
|
|
|
// ANCHOR: solution
|
|
|
|
// ANCHOR: setup
|
2023-11-29 17:39:24 +02:00
|
|
|
|
|
|
|
#![allow(dead_code)]
|
2023-06-21 16:08:37 +02:00
|
|
|
pub struct User {
|
2022-12-21 17:36:30 +02:00
|
|
|
name: String,
|
|
|
|
age: u32,
|
2023-07-12 00:01:49 +02:00
|
|
|
height: f32,
|
|
|
|
visit_count: usize,
|
|
|
|
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)>,
|
2022-12-21 17:36:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl User {
|
2023-07-12 00:01:49 +02:00
|
|
|
pub fn new(name: String, age: u32, height: f32) -> Self {
|
2023-09-26 09:29:00 +02:00
|
|
|
Self {
|
|
|
|
name,
|
|
|
|
age,
|
|
|
|
height,
|
|
|
|
visit_count: 0,
|
|
|
|
last_blood_pressure: None,
|
|
|
|
}
|
2022-12-21 17:36:30 +02:00
|
|
|
}
|
2023-11-29 17:39:24 +02:00
|
|
|
// ANCHOR_END: setup
|
2023-07-12 00:01:49 +02:00
|
|
|
|
2023-09-26 09:29:00 +02:00
|
|
|
// ANCHOR: User_visit_doctor
|
2023-07-12 00:01:49 +02:00
|
|
|
pub fn visit_doctor(&mut self, measurements: Measurements) -> HealthReport {
|
2023-09-26 09:29:00 +02:00
|
|
|
// ANCHOR_END: User_visit_doctor
|
|
|
|
self.visit_count += 1;
|
|
|
|
let bp = measurements.blood_pressure;
|
|
|
|
let report = HealthReport {
|
|
|
|
patient_name: &self.name,
|
|
|
|
visit_count: self.visit_count as u32,
|
|
|
|
height_change: measurements.height - self.height,
|
|
|
|
blood_pressure_change: match self.last_blood_pressure {
|
2023-11-29 17:39:24 +02:00
|
|
|
Some(lbp) => {
|
|
|
|
Some((bp.0 as i32 - lbp.0 as i32, bp.1 as i32 - lbp.1 as i32))
|
|
|
|
}
|
2023-09-26 09:29:00 +02:00
|
|
|
None => None,
|
2023-11-29 17:39:24 +02:00
|
|
|
},
|
2023-09-26 09:29:00 +02:00
|
|
|
};
|
|
|
|
self.height = measurements.height;
|
|
|
|
self.last_blood_pressure = Some(bp);
|
|
|
|
report
|
2022-12-21 17:36:30 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-26 09:29:00 +02:00
|
|
|
|
|
|
|
// ANCHOR: main
|
|
|
|
fn main() {
|
|
|
|
let bob = User::new(String::from("Bob"), 32, 155.2);
|
2023-11-29 17:39:24 +02:00
|
|
|
println!("I'm {} and my age is {}", bob.name, bob.age);
|
2023-09-26 09:29:00 +02:00
|
|
|
}
|
|
|
|
// ANCHOR_END: main
|
|
|
|
|
|
|
|
// ANCHOR: tests
|
|
|
|
#[test]
|
|
|
|
fn test_visit() {
|
|
|
|
let mut bob = User::new(String::from("Bob"), 32, 155.2);
|
2023-11-29 17:39:24 +02:00
|
|
|
assert_eq!(bob.visit_count, 0);
|
2023-09-26 09:29:00 +02:00
|
|
|
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)));
|
|
|
|
}
|
|
|
|
// ANCHOR_END: tests
|