SHARE
TWEET

Untitled

a guest Oct 17th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Call Center: Imagine you have a call center with three levels of
  2. // employees: respondent, manager, and director. An incoming telephone
  3. // call must be first allocated to a respondent who is free. If the
  4. // respondent can't handle the call, he or she must escalate the call
  5. // to a manager. If the manager is not free or not able to handle it,
  6. // then the call should be escalated to a director. Design the classes
  7. // and data structures for this problem. Implement a method
  8. // dispatchCall() which assigns a call to the first available
  9. // employee.
  10.  
  11. #[derive(Debug, PartialEq, Eq)]
  12. enum Role {
  13.     Respondent,
  14.     Manager,
  15.     Director,
  16. }
  17.  
  18. #[derive(Debug, PartialEq, Eq)]
  19. struct Employee {
  20.     is_available: bool,
  21.     role: Role,
  22.     name: String,
  23. }
  24.  
  25. impl Employee {
  26.     fn set_busy(&mut self) {
  27.         self.is_available = false;
  28.     }
  29. }
  30.  
  31. struct CallCenter {
  32.     employees: Vec<Employee>,
  33. }
  34.  
  35. impl CallCenter {
  36.     fn _find_available_employee<'a>(&'a mut self, role: Role) -> Option<&'a mut Employee> {
  37.         self.employees
  38.             .iter_mut()
  39.             .find(|e| e.role == role && e.is_available)
  40.     }
  41.  
  42.     fn dispatch_call<'a>(&'a mut self) -> Option<&'a Employee> {
  43.         if let Some(ref mut found) = self._find_available_employee(Role::Respondent) {
  44.             found.set_busy();
  45.             Some(found) // ERROR: returning this value requires that `*self` is borrowed for `'a` [E0499]
  46.         } else if let Some(ref mut found) = self._find_available_employee(Role::Manager) { // ERROR: cannot borrow `*self` as mutable more than once at a time (second mutable borrow occurs here) [E0499]
  47.             found.set_busy();
  48.             Some(found)
  49.         } else if let Some(ref mut found) = self._find_available_employee(Role::Director) {
  50.             found.set_busy();
  51.             Some(found)
  52.         } else {
  53.             // if no one is available, return None
  54.             None
  55.         }
  56.     }
  57. }
  58.  
  59.  
  60. fn main() {
  61.     let mut call_center = CallCenter {
  62.         employees: vec![Employee {
  63.             role: Role::Director,
  64.             is_available: false,
  65.             name: String::from("Mr. Director"),
  66.         }],
  67.     };
  68.     call_center.dispatch_call();
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top