Exercise: Elevator Events

We will create a data structure to represent an event in an elevator control system. It is up to you to define the types and functions to construct various events. Use #[derive(Debug)] to allow the types to be formatted with {:?}.

This exercise only requires creating and populating data structures so that main runs without errors. The next part of the course will cover getting data out of these structures.

  1. #[derive(Debug)]
  2. /// An event in the elevator system that the controller must react to.
  3. enum Event {
  4.     // TODO: add required variants
  5. }
  6. /// A direction of travel.
  7. #[derive(Debug)]
  8. enum Direction {
  9.     Up,
  10.     Down,
  11. }
  12. /// The car has arrived on the given floor.
  13. fn car_arrived(floor: i32) -> Event {
  14.     todo!()
  15. }
  16. /// The car doors have opened.
  17. fn car_door_opened() -> Event {
  18.     todo!()
  19. }
  20. /// The car doors have closed.
  21. fn car_door_closed() -> Event {
  22.     todo!()
  23. }
  24. /// A directional button was pressed in an elevator lobby on the given floor.
  25. fn lobby_call_button_pressed(floor: i32, dir: Direction) -> Event {
  26.     todo!()
  27. }
  28. /// A floor button was pressed in the elevator car.
  29. fn car_floor_button_pressed(floor: i32) -> Event {
  30.     todo!()
  31. }
  32. fn main() {
  33.     println!(
  34.         "A ground floor passenger has pressed the up button: {:?}",
  35.         lobby_call_button_pressed(0, Direction::Up)
  36.     );
  37.     println!("The car has arrived on the ground floor: {:?}", car_arrived(0));
  38.     println!("The car door opened: {:?}", car_door_opened());
  39.     println!(
  40.         "A passenger has pressed the 3rd floor button: {:?}",
  41.         car_floor_button_pressed(3)
  42.     );
  43.     println!("The car door closed: {:?}", car_door_closed());
  44.     println!("The car has arrived on the 3rd floor: {:?}", car_arrived(3));
  45. }