elevate_lib/elevators.rs
1//Import source modules
2use crate::elevator::Elevator;
3use crate::person::Person;
4use crate::people::People;
5
6/// # `Elevators` trait
7///
8/// A `Elevators` implementation is representative of a collection of `Elevator`s.
9/// It is implemented by the `Building` struct.
10pub trait Elevators {
11 fn get_dest_floors(&self) -> Vec<usize>;
12
13 fn get_energy_spent(&mut self) -> f64;
14
15 fn flush_people_leaving_elevators(&mut self) -> Vec<Vec<Person>>;
16
17 fn update_floors(&mut self);
18
19 fn increment_wait_times(&mut self);
20
21 fn append_elevator(&mut self, energy_up: f64, energy_down: f64, energy_coef: f64);
22}
23
24//Implementation of elevators trait for Vec<Elevators>
25impl Elevators for Vec<Elevator> {
26 /// Get an aggregated list of destination floors across the vector of elevators.
27 fn get_dest_floors(&self) -> Vec<usize> {
28 //Initialize a vector of usizes to track the overall dest floors
29 let mut dest_floors: Vec<usize> = Vec::new();
30
31 //Loop through the elevators and get the dest floor vectors
32 for elevator in self.iter() {
33 //Get the dest floors of the elevator
34 let elevator_dest_floors: Vec<usize> = elevator.get_dest_floors();
35
36 //Append the dest floors to the list of dest floors if not contained
37 for dest_floor in elevator_dest_floors.iter() {
38 if dest_floors.contains(dest_floor) {
39 continue;
40 }
41 dest_floors.push(*dest_floor);
42 }
43 }
44
45 //Return the dest floors
46 dest_floors
47 }
48
49 /// Calculate the total energy spent across the vector of elevators.
50 fn get_energy_spent(&mut self) -> f64 {
51 //Initialize an f64 to aggregate the total energy spent
52 let mut energy_spent: f64 = 0.0_f64;
53
54 //Loop through the elevators and calculate their energy spent
55 for elevator in self.iter_mut() {
56 let elevator_energy_spent: f64 = elevator.get_energy_spent();
57
58 //Add the energy spent to the total
59 energy_spent += elevator_energy_spent;
60 }
61
62 //Return the aggregate energy spent
63 energy_spent
64 }
65
66 /// For each elevator, flush anyone leaving the elevator and aggregate each
67 /// resulting `Vec<Person>` into a `Vec<Vec<Person>>`.
68 fn flush_people_leaving_elevators(&mut self) -> Vec<Vec<Person>> {
69 //Initialize a vector of vectors of people
70 let mut people_leaving_elevators: Vec<Vec<Person>> = Vec::new();
71
72 //Loop through all the elevators and flush the people leaving the elevator
73 for elevator in self.iter_mut() {
74 let people_leaving_elevator: Vec<Person> = elevator.flush_people_leaving_elevator();
75
76 //Append to the list of people leaving the elevators
77 people_leaving_elevators.push(people_leaving_elevator);
78 }
79
80 //Return the vector of vectors of people leaving each elevator
81 people_leaving_elevators
82 }
83
84 /// For each elevator, update its floor based on its `stopped` boolean and its
85 /// `moving_up` boolean.
86 fn update_floors(&mut self) {
87 for elevator in self.iter_mut() {
88 elevator.update_floor();
89 }
90 }
91
92 /// For each elevator, increment the wait times of the people on the elevator if
93 /// they are not on their desired floor.
94 fn increment_wait_times(&mut self) {
95 for elevator in self.iter_mut() {
96 elevator.increment_wait_times();
97 }
98 }
99
100 /// Appends a new elevator to the collection of elevators
101 fn append_elevator(&mut self, energy_up: f64, energy_down: f64, energy_coef: f64) {
102 self.push(Elevator::from(energy_up, energy_down, energy_coef));
103 }
104}