Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.93 KB | None | 0 0
  1. package ope.exqu;
  2.  
  3. import java.util.ArrayList;
  4.  
  5.  
  6. /**
  7.  * The class <CODE>Excursion</CODE> represents excursions
  8.  * that students can sign up for. Each excursion object has
  9.  * a maximum number of "places" for students, a name and
  10.  * a sequence number which indicates if the excursion is
  11.  * a "re-run" of a previous one. An excursion object keeps
  12.  * track of which students have signed up for the
  13.  * excursion. There may be more students signed up for
  14.  * an excursion than there are places, in the hopes that
  15.  * places are freed by cancellations or more places are
  16.  * added to the excursion. Such students are called "reserves".
  17.  * See the method <CODE>signUp</CODE> for more details
  18.  * about the signing up and queuing process.
  19.  */
  20. public class Excursion {
  21.  
  22.   private String name;                      // fixed value
  23.   private int number;                       // fixed value
  24.   private int places;                       // gatherer
  25.   public ArrayList<Student> students;      // container: all the student who have signed up, including reserves
  26.  
  27.  
  28.   /**
  29.    * Creates a new excursion with the given name, sequence number
  30.    * and number of places. No students are registered yet for
  31.    * the new excursion.
  32.    *
  33.    * @param name   the name of the excursion, e.g. "Nokian aula"
  34.    * @param number the sequence number of the excursion.
  35.    *               1 indicates that the excursion is organized for
  36.    *               the first time, a higher number indicates a
  37.    *               re-run of a previously organized excursion.
  38.    * @param places the number of students that can take part in
  39.    *               the excursion
  40.    */
  41.   public Excursion(String name, int number, int places) {
  42.     this.name = name;
  43.     this.number = number;
  44.     this.places = places;
  45.     this.students = new ArrayList<Student>();
  46.   }
  47.  
  48.  
  49.   /**
  50.    * Returns a string description of the excursion's main data. This description
  51.    * is of the form "<CODE>Name #Number (StudentCount/Places)</CODE>" where
  52.    * <CODE>StudentCount</CODE> is the total number of students who have signed up
  53.    * for the excursion, including reserves. Examples:
  54.    * <UL><LI><CODE>Nokia #1 (25/50)</CODE><LI><CODE>Nokia #3 (53/50)</CODE></UL>
  55.    *
  56.    * @return a string description of the excursion
  57.    */
  58.   public String getDescription() {
  59.    return this.name +" #" + this.number + " (" + this.students.size() + "/" + this.places + ")";
  60.   }
  61.  
  62.  
  63.   /**
  64.    * Adds more places to the excursion.
  65.    *
  66.    * @param increase a positive number indicating how many more
  67.    *                 students the excursion can now accommodate
  68.    */
  69.   public void addPlaces(int increase) {
  70.     if (increase >= 0){
  71.       this.places = this.places + increase;
  72.     }
  73.   }
  74.  
  75.  
  76.   /**
  77.    * Returns the number of students that can take part in the
  78.    * excursion.
  79.    *
  80.    * @return number of places
  81.    */
  82.   public int getPlaces() {
  83.     return this.places;
  84.   }
  85.  
  86.  
  87.   /**
  88.    * Returns a boolean value indicating if, as matters currently
  89.    * stand, the student will be able to take part
  90.    * in the excursion. That is, a return value of <CODE>true</CODE>
  91.    * means that the student will be able to participate if
  92.    * not too many younger students sign up later. A return value
  93.    * of <CODE>false</CODE> means that either the given student has not
  94.    * even tried to sign up for the excursion, or that the student is
  95.    * a "reserve" and will not be able to participate unless places are
  96.    * freed due to cancellations or more places are added to the excursion.
  97.    *
  98.    * @param student a student
  99.    * @return a boolean value indicating the student's queuing status,
  100.    *         as described above
  101.    */
  102.   public boolean hasPlace(Student student) {
  103.     if (this.students.contains(student))
  104.     {
  105.     int index = this.students.indexOf(student)+1;
  106.     if (index <= this.places)
  107.     { return true;
  108.     }
  109.     }
  110.     return false;
  111.   }
  112.  
  113.  
  114.   /**
  115.    * Determines if the given student is a "reserve", that is,
  116.    * if the student has signed up but currently does not have
  117.    * a place available. If so, determines the position of the
  118.    * student in the queue of reserves. A positive return value
  119.    * indicates the students position: 1 means the first of the
  120.    * reserves (the one who will be the first to get a place if
  121.    * someone cancels), 2 means the next reserve, and so on.
  122.    *
  123.    * @param student a student
  124.    * @return zero if the student is not a reserve, a positive number
  125.    *              indicating the student's position amongst the
  126.    *              reserves otherwise.
  127.    */
  128.   public int getReservePosition(Student student) {
  129.     int index = this.students.indexOf(student) + 1;
  130.     if (index > this.places)
  131.     {
  132.       return index-this.places;
  133.     }
  134.     return 0;
  135.   }
  136.  
  137.   /**
  138.    * Signs up the given student for the excursion.
  139.    * If the given student was already signed up for the excursion,
  140.    * nothing needs to be done. Otherwise, the excursion determines
  141.    * the position in which the student is placed amongst other
  142.    * students who have signed up. This is done according
  143.    * to two rules:
  144.    * <OL>
  145.    * <LI>Younger students (in terms of years studied) always
  146.    *     have the preference over older students. That is,
  147.    *     a younger student will be able to "cut the line" and
  148.    *     get in before older ones.
  149.    * <LI>Of the students with the same year of study, the one
  150.    *     that signs up first will have the preference.
  151.    * </OL>
  152.    *
  153.    * @param newStudent the student who is signing up
  154.    */
  155.   public void signUp(Student newStudent) {
  156.     if (this.students.size()>0)
  157.     {
  158.      if (false == this.students.contains(newStudent))
  159.      {
  160.       for (int i=0; i<this.students.size(); i++)
  161.       {
  162.        if (i == this.students.size()-1)
  163.        {
  164.         if (newStudent.getYearOfStudy() < this.students.get(i).getYearOfStudy() )
  165.          {
  166.           this.students.add(i, newStudent);
  167.           break;
  168.          }
  169.          else
  170.          {
  171.           this.students.add(newStudent);
  172.           break;
  173.          }
  174.         }
  175.      
  176.      else
  177.       {
  178.        if (newStudent.getYearOfStudy() < this.students.get(i).getYearOfStudy() )
  179.       {
  180.       this.students.add(i, newStudent);
  181.       break;
  182.     }
  183.     }
  184.     }
  185.   }
  186. }
  187.     else
  188.     {
  189.       this.students.add(newStudent);
  190.     }
  191.   }
  192.  
  193.   /**
  194.    * Cancels the given students registration for the excursion.
  195.    *
  196.    * @param student the cancelling student
  197.    */
  198.   public void cancel(Student student) {
  199.     this.students.remove(student);
  200.   }
  201.  
  202.  
  203.   /**
  204.    * Returns a listing of the students who currently have places
  205.    * on the excursion, in order. The listing is prefaced by a generic
  206.    * description of the excursion obtained by calling
  207.    * <CODE>this.getDescription()</CODE> The rest of the listing consists
  208.    * of lines of the form " - StudentName (YearOfStudy)". E.g.
  209.    * <PRE>
  210.    * MiniExqu #1 (5/3)
  211.    *  - Bob (1)
  212.    *  - Adam (1)
  213.    *  - Cecilia (3)
  214.    * </PRE>
  215.    * Note that students in reserve are not included in this listing.
  216.    *
  217.    * @return a multi-line participant listing
  218.    * @see #hasPlace(Student)
  219.    * @see Student#getDescription()
  220.    */
  221.   public String getParticipantListing() {
  222.     String listing = this.getDescription() + "\n";
  223.     if (this.students.size()!=0)
  224.     {
  225.     for (int i=0; i<this.places; i++)
  226.     {if (this.students.size() == i)
  227.     {
  228.       break;
  229.     }
  230.       listing += " - " + this.students.get(i).getDescription() + "\n";
  231.    
  232.     }
  233.     }
  234.     return listing;
  235.   }
  236.  
  237.  
  238.   /**
  239.    * Returns the students who are in reserve for this excursion. That is,
  240.    * returns a list containing the students who are queuing up in hopes of
  241.    * cancellations or additional available places.
  242.    *
  243.    * @return a list of the reserves, in queueing order (if there are none, an empty list is returned)
  244.    */
  245.   public ArrayList<Student> getReserves() {
  246.     ArrayList<Student> queue = new ArrayList<Student>();    
  247.    queue.addAll(this.students);
  248.  
  249.    if (this.places <= queue.size())
  250.    {
  251.    queue.subList(0, this.places).clear();
  252.    }
  253.    else
  254.    {
  255.      queue.subList(0, queue.size()).clear();
  256.    }
  257.     return queue;
  258.    
  259.   }
  260.    
  261.    
  262.  
  263.  
  264.  
  265.   /**
  266.    * Creates and returns a new excursion which is a "re-run" of this
  267.    * excursion. The new excursion has the same name as this one,
  268.    * a sequence number which is higher by one than this one's, and the
  269.    * given number of places available. Furthermore, all this excursion's
  270.    * reserve students are moved (in the same order) to the newly created
  271.    * re-run excursion. ("Moving" means that they are removed from the old
  272.    * excursion and added to the new one.)
  273.    *
  274.    * @param places
  275.    * @return the new re-run excursion
  276.    */
  277.   public Excursion redo(int places) {
  278.     Excursion rerun = new Excursion(this.name, this.number+1, places);
  279.    rerun.students.addAll(this.getReserves());
  280.    this.students.removeAll(this.getReserves());
  281.     return rerun;
  282.   }
  283.  
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement