diff --git a/exercises/quiz3.rs b/exercises/quiz3.rs index 15dc4699..94753e53 100644 --- a/exercises/quiz3.rs +++ b/exercises/quiz3.rs @@ -14,18 +14,38 @@ // Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE +pub trait Grade { + fn to_string(&self) -> String; +} -pub struct ReportCard { - pub grade: f32, +pub struct StrGrade(String); +pub struct NumGrade(f32); + +impl Grade for StrGrade { + fn to_string(&self) -> String { + self.0.to_string() + } +} +impl Grade for NumGrade { + fn to_string(&self) -> String { + self.0.to_string() + } +} + +pub struct ReportCard { + pub grade: G, pub student_name: String, pub student_age: u8, } -impl ReportCard { +impl ReportCard { pub fn print(&self) -> String { - format!("{} ({}) - achieved a grade of {}", - &self.student_name, &self.student_age, &self.grade) + format!( + "{} ({}) - achieved a grade of {}", + &self.student_name, + &self.student_age, + &self.grade.to_string() + ) } } @@ -36,7 +56,7 @@ mod tests { #[test] fn generate_numeric_report_card() { let report_card = ReportCard { - grade: 2.1, + grade: NumGrade(2.1), student_name: "Tom Wriggle".to_string(), student_age: 12, }; @@ -50,7 +70,7 @@ mod tests { fn generate_alphabetic_report_card() { // TODO: Make sure to change the grade here after you finish the exercise. let report_card = ReportCard { - grade: 2.1, + grade: StrGrade(String::from("A+")), student_name: "Gary Plotter".to_string(), student_age: 11, };