diff --git a/clock/src/lib.rs b/clock/src/lib.rs index feb8098..519d728 100644 --- a/clock/src/lib.rs +++ b/clock/src/lib.rs @@ -1,15 +1,36 @@ -pub struct Clock; +pub struct Clock{ + hours:i32, + minutes:i32 +} + +impl std::fmt::Display for Clock{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f,"{:0>2}:{:0>2}", self.hours, self.minutes) + } +} + +impl std::fmt::Debug for Clock{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f,"{:0>2}:{:0>2}", self.hours, self.minutes) + } +} + +impl PartialEq for Clock{ + fn eq(&self, other: &Self) -> bool { + self.minutes == other.minutes && self.hours == other.hours + } +} + +impl Eq for Clock{} impl Clock { pub fn new(hours: i32, minutes: i32) -> Self { - unimplemented!( - "Construct a new Clock from {} hours and {} minutes", - hours, - minutes - ); + Self{hours,minutes} } pub fn add_minutes(&self, minutes: i32) -> Self { - unimplemented!("Add {} minutes to existing Clock time", minutes); + let new_minutes = (self.minutes + minutes) % 60; + let new_hours = (self.hours + ((self.minutes + minutes) / 60)) % 24; + Self{hours:new_hours,minutes:new_minutes} } } diff --git a/clock/tests/clock.rs b/clock/tests/clock.rs index 445d13d..afb4312 100644 --- a/clock/tests/clock.rs +++ b/clock/tests/clock.rs @@ -10,85 +10,71 @@ fn test_on_the_hour() { } #[test] -#[ignore] fn test_past_the_hour() { assert_eq!(Clock::new(11, 9).to_string(), "11:09"); } #[test] -#[ignore] fn test_midnight_is_zero_hours() { assert_eq!(Clock::new(24, 0).to_string(), "00:00"); } #[test] -#[ignore] fn test_hour_rolls_over() { assert_eq!(Clock::new(25, 0).to_string(), "01:00"); } #[test] -#[ignore] fn test_hour_rolls_over_continuously() { assert_eq!(Clock::new(100, 0).to_string(), "04:00"); } #[test] -#[ignore] fn test_sixty_minutes_is_next_hour() { assert_eq!(Clock::new(1, 60).to_string(), "02:00"); } #[test] -#[ignore] fn test_minutes_roll_over() { assert_eq!(Clock::new(0, 160).to_string(), "02:40"); } #[test] -#[ignore] fn test_minutes_roll_over_continuously() { assert_eq!(Clock::new(0, 1723).to_string(), "04:43"); } #[test] -#[ignore] fn test_hours_and_minutes_roll_over() { assert_eq!(Clock::new(25, 160).to_string(), "03:40"); } #[test] -#[ignore] fn test_hours_and_minutes_roll_over_continuously() { assert_eq!(Clock::new(201, 3001).to_string(), "11:01"); } #[test] -#[ignore] fn test_hours_and_minutes_roll_over_to_exactly_midnight() { assert_eq!(Clock::new(72, 8640).to_string(), "00:00"); } #[test] -#[ignore] fn test_negative_hour() { assert_eq!(Clock::new(-1, 15).to_string(), "23:15"); } #[test] -#[ignore] fn test_negative_hour_roll_over() { assert_eq!(Clock::new(-25, 00).to_string(), "23:00"); } #[test] -#[ignore] fn test_negative_hour_roll_over_continuously() { assert_eq!(Clock::new(-91, 00).to_string(), "05:00"); } #[test] -#[ignore] fn test_negative_minutes() { assert_eq!(Clock::new(1, -40).to_string(), "00:20"); }