Skip to main content

elevate_lib/
elevators.rs

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