Kodomo

Пользователь

Учебная страница курса биоинформатики,
год поступления 2010

   1 package lesson2;
   2 
   3 public class Boss
   4 {
   5         // Boss name
   6         String name;
   7         
   8         // Boss surname
   9         String surname;
  10         
  11         // probability that boss is sleeping
  12         double sleeping_probability = 0.5;
  13         
  14         // Engaged secretary (actually its a link to a secretary object); if it is null nobody is engaged
  15         Secretary secretary;
  16         
  17         /**
  18          * Constructor
  19          * @param name
  20          * @param surname
  21          */
  22         Boss(String name, String surname)
  23         {
  24                 // set the name and surname of the Boss object 
  25                 this.name=name;
  26                 this.surname=surname;
  27         }
  28         
  29         /**
  30          * @return full name of the Boss
  31          */
  32         String getFullName()
  33         {
  34                 return name+" "+surname;
  35         }
  36 
  37         /**
  38          * @return true if boss is sleeping, false elsewise
  39          */
  40         boolean isSleeping()
  41         {
  42                 if(Math.random() < sleeping_probability)
  43                 {
  44                         System.out.println("Boss is sleeping");
  45                         return true;
  46                 }
  47                 return false;
  48         }
  49         /**
  50          * Check if a secretary works for the Boss. And print some of his thoughts and phrases.
  51          * @return true if the secretary is available, false elsewise
  52          */
  53         boolean checkSecretaryAvailability()
  54         {
  55                 speak("I have a job for a secretary. Hm... What is her name?..");
  56                 if(secretary==null)
  57                 {
  58                         // if secretary == null, it means the Boss doesn't have any engaged secretary 
  59                         speak("Oh, I have no secretary!.. I should engage one.");
  60                         return false;
  61                 }
  62                 speak(secretary.getFullName()+", come to my room, please. I have a job for you.");
  63                 return true;
  64         }
  65         
  66         /**
  67          * Check if a secretary works for the Boss. If not, choose one of the candidate list and engage her.<br>
  68          * If there is no appropriate secretary in the candidate list, nobody is engaged 
  69          * and null is returned.
  70          * @param secretaries array of secretaries that want to work for the Boss
  71          * @return secretary that has been engaged (just now or earlier) or null if nobody has been engaged
  72          */
  73         Secretary chooseAndAppointSecretary(Secretary[] secretaries)
  74         {
  75                 // this is just for checking
  76                 if(secretary!=null)
  77                 {
  78                         // if boss have a secretary he needn't engade one more
  79                         System.out.println("Secretary is already appointed.");
  80                         return secretary;
  81                 }
  82                 // choosing a secretary with the shortest nails
  83                 Secretary best_secr = null;
  84                 //============================================================================
  85                 // TODO: 
  86                 // напишите блок, в результате работы которого в переменной best_secr будет  
  87                 // находиться секретарша (т.е. объект класса Secretary) из массива secretaries 
  88                 // с самыми короткими ногтями. Причем такая, которая еще не увольнялась из  
  89                 // этого офиса (т.е. best_secr.isDischarged()==false). 
  90                 //============================================================================
  91                 this.secretary=best_secr;
  92                 // now secretary is engaged (i.e. link "secretary" refers to the secretary object)
  93                 
  94                 return best_secr;
  95         }
  96         
  97         /**
  98          * Force the secretary to print documents
  99          * @return true if secretary suceeded, false elsewise
 100          */
 101         boolean secretaryToPrintDocuments()
 102         {
 103                 speak(secretary.getFullName()+", print me these documents, please.");
 104                 
 105                 return secretary.printDocuments();
 106         }
 107         
 108         /**
 109          * Discharge secretary
 110          */
 111         void dischargeSecretary()
 112         {
 113                 speak("Useless secretary! You are fired!!!");
 114                 // mark this secretary by was-discharged-mark (for the Boss will never engade her again)
 115                 secretary.dischargeFromOffice();
 116                 // remove the link to this secretary: she is fired 
 117                 secretary=null;
 118         }
 119         
 120         /**
 121          * Print Boss's phrase in a dialog form 
 122          * @param phrase phrase to be printed
 123          */
 124         void speak(String phrase)
 125         {
 126                 System.out.println("Boss:\t-"+phrase);
 127         }
 128 }