Skip to content

Instantly share code, notes, and snippets.

@jayhuang75
Last active November 27, 2022 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayhuang75/71629f6a7c3df8c173bfb648871f8ef1 to your computer and use it in GitHub Desktop.
Save jayhuang75/71629f6a7c3df8c173bfb648871f8ef1 to your computer and use it in GitHub Desktop.
mediator_airport
pub struct Airport {
name: String,
aircrafts: HashMap<String, Box<dyn Aircraft>>,
aircraft_on_gate: HashMap<i8, String>,
aircraft_queue: VecDeque<String>,
gates: HashMap<i8, bool>,
}
impl Airport {
pub fn new(name: impl Into<String>, total_gates: i8) -> Self {
if total_gates == 0 {
panic!("gates can't be empty!");
}
let name = name.into();
let aircrafts: HashMap<String, Box<dyn Aircraft>> = HashMap::new();
let aircraft_on_gate : HashMap<i8, String> = HashMap::new();
let aircraft_queue: VecDeque<String> = VecDeque::new();
let mut gates: HashMap<i8, bool> = HashMap::new();
for x in 1..total_gates + 1 {
gates.insert(x, true);
}
Airport {
name,
aircrafts,
aircraft_on_gate,
aircraft_queue,
gates
}
}
pub fn land(&mut self, mut aircraft: impl Aircraft + 'static) {
/*
There are 3 steps for notification
1. check if the aircraft have already arrived
2. go over the aricraft arrival process.
3. insert to the aricrafts.
*/
if self.aircrafts.contains_key(aircraft.id()) {
println!("{:?} has already arrived", aircraft.id());
return;
}
aircraft.arrive(self);
self.aircrafts.insert(aircraft.id().to_owned(), Box::new(aircraft));
}
pub fn depart(&mut self, aircraft_id: &str) {
let aircraft = self.aircrafts.remove(aircraft_id.into());
if let Some(mut aircraft) = aircraft {
aircraft.depart(self);
} else {
println!("'{}' is not on the airport!", aircraft_id);
}
}
pub fn dashboard(&self) -> Option<HashMap<i8, String>> {
if self.aircraft_on_gate.len() == 0 {
return None
}
return Some(self.aircraft_on_gate.clone())
}
pub fn aircraft_queue(&self) -> Option<VecDeque<String>>{
Some(self.aircraft_queue.clone())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment