Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ope.exqu;
- import java.util.ArrayList;
- /**
- * The class <CODE>Excursion</CODE> represents excursions
- * that students can sign up for. Each excursion object has
- * a maximum number of "places" for students, a name and
- * a sequence number which indicates if the excursion is
- * a "re-run" of a previous one. An excursion object keeps
- * track of which students have signed up for the
- * excursion. There may be more students signed up for
- * an excursion than there are places, in the hopes that
- * places are freed by cancellations or more places are
- * added to the excursion. Such students are called "reserves".
- * See the method <CODE>signUp</CODE> for more details
- * about the signing up and queuing process.
- */
- public class Excursion {
- private String name; // fixed value
- private int number; // fixed value
- private int places; // gatherer
- public ArrayList<Student> students; // container: all the student who have signed up, including reserves
- /**
- * Creates a new excursion with the given name, sequence number
- * and number of places. No students are registered yet for
- * the new excursion.
- *
- * @param name the name of the excursion, e.g. "Nokian aula"
- * @param number the sequence number of the excursion.
- * 1 indicates that the excursion is organized for
- * the first time, a higher number indicates a
- * re-run of a previously organized excursion.
- * @param places the number of students that can take part in
- * the excursion
- */
- public Excursion(String name, int number, int places) {
- this.name = name;
- this.number = number;
- this.places = places;
- this.students = new ArrayList<Student>();
- }
- /**
- * Returns a string description of the excursion's main data. This description
- * is of the form "<CODE>Name #Number (StudentCount/Places)</CODE>" where
- * <CODE>StudentCount</CODE> is the total number of students who have signed up
- * for the excursion, including reserves. Examples:
- * <UL><LI><CODE>Nokia #1 (25/50)</CODE><LI><CODE>Nokia #3 (53/50)</CODE></UL>
- *
- * @return a string description of the excursion
- */
- public String getDescription() {
- return this.name +" #" + this.number + " (" + this.students.size() + "/" + this.places + ")";
- }
- /**
- * Adds more places to the excursion.
- *
- * @param increase a positive number indicating how many more
- * students the excursion can now accommodate
- */
- public void addPlaces(int increase) {
- if (increase >= 0){
- this.places = this.places + increase;
- }
- }
- /**
- * Returns the number of students that can take part in the
- * excursion.
- *
- * @return number of places
- */
- public int getPlaces() {
- return this.places;
- }
- /**
- * Returns a boolean value indicating if, as matters currently
- * stand, the student will be able to take part
- * in the excursion. That is, a return value of <CODE>true</CODE>
- * means that the student will be able to participate if
- * not too many younger students sign up later. A return value
- * of <CODE>false</CODE> means that either the given student has not
- * even tried to sign up for the excursion, or that the student is
- * a "reserve" and will not be able to participate unless places are
- * freed due to cancellations or more places are added to the excursion.
- *
- * @param student a student
- * @return a boolean value indicating the student's queuing status,
- * as described above
- */
- public boolean hasPlace(Student student) {
- if (this.students.contains(student))
- {
- int index = this.students.indexOf(student)+1;
- if (index <= this.places)
- { return true;
- }
- }
- return false;
- }
- /**
- * Determines if the given student is a "reserve", that is,
- * if the student has signed up but currently does not have
- * a place available. If so, determines the position of the
- * student in the queue of reserves. A positive return value
- * indicates the students position: 1 means the first of the
- * reserves (the one who will be the first to get a place if
- * someone cancels), 2 means the next reserve, and so on.
- *
- * @param student a student
- * @return zero if the student is not a reserve, a positive number
- * indicating the student's position amongst the
- * reserves otherwise.
- */
- public int getReservePosition(Student student) {
- int index = this.students.indexOf(student) + 1;
- if (index > this.places)
- {
- return index-this.places;
- }
- return 0;
- }
- /**
- * Signs up the given student for the excursion.
- * If the given student was already signed up for the excursion,
- * nothing needs to be done. Otherwise, the excursion determines
- * the position in which the student is placed amongst other
- * students who have signed up. This is done according
- * to two rules:
- * <OL>
- * <LI>Younger students (in terms of years studied) always
- * have the preference over older students. That is,
- * a younger student will be able to "cut the line" and
- * get in before older ones.
- * <LI>Of the students with the same year of study, the one
- * that signs up first will have the preference.
- * </OL>
- *
- * @param newStudent the student who is signing up
- */
- public void signUp(Student newStudent) {
- if (this.students.size()>0)
- {
- if (false == this.students.contains(newStudent))
- {
- for (int i=0; i<this.students.size(); i++)
- {
- if (i == this.students.size()-1)
- {
- if (newStudent.getYearOfStudy() < this.students.get(i).getYearOfStudy() )
- {
- this.students.add(i, newStudent);
- break;
- }
- else
- {
- this.students.add(newStudent);
- break;
- }
- }
- else
- {
- if (newStudent.getYearOfStudy() < this.students.get(i).getYearOfStudy() )
- {
- this.students.add(i, newStudent);
- break;
- }
- }
- }
- }
- }
- else
- {
- this.students.add(newStudent);
- }
- }
- /**
- * Cancels the given students registration for the excursion.
- *
- * @param student the cancelling student
- */
- public void cancel(Student student) {
- this.students.remove(student);
- }
- /**
- * Returns a listing of the students who currently have places
- * on the excursion, in order. The listing is prefaced by a generic
- * description of the excursion obtained by calling
- * <CODE>this.getDescription()</CODE> The rest of the listing consists
- * of lines of the form " - StudentName (YearOfStudy)". E.g.
- * <PRE>
- * MiniExqu #1 (5/3)
- * - Bob (1)
- * - Adam (1)
- * - Cecilia (3)
- * </PRE>
- * Note that students in reserve are not included in this listing.
- *
- * @return a multi-line participant listing
- * @see #hasPlace(Student)
- * @see Student#getDescription()
- */
- public String getParticipantListing() {
- String listing = this.getDescription() + "\n";
- if (this.students.size()!=0)
- {
- for (int i=0; i<this.places; i++)
- {if (this.students.size() == i)
- {
- break;
- }
- listing += " - " + this.students.get(i).getDescription() + "\n";
- }
- }
- return listing;
- }
- /**
- * Returns the students who are in reserve for this excursion. That is,
- * returns a list containing the students who are queuing up in hopes of
- * cancellations or additional available places.
- *
- * @return a list of the reserves, in queueing order (if there are none, an empty list is returned)
- */
- public ArrayList<Student> getReserves() {
- ArrayList<Student> queue = new ArrayList<Student>();
- queue.addAll(this.students);
- if (this.places <= queue.size())
- {
- queue.subList(0, this.places).clear();
- }
- else
- {
- queue.subList(0, queue.size()).clear();
- }
- return queue;
- }
- /**
- * Creates and returns a new excursion which is a "re-run" of this
- * excursion. The new excursion has the same name as this one,
- * a sequence number which is higher by one than this one's, and the
- * given number of places available. Furthermore, all this excursion's
- * reserve students are moved (in the same order) to the newly created
- * re-run excursion. ("Moving" means that they are removed from the old
- * excursion and added to the new one.)
- *
- * @param places
- * @return the new re-run excursion
- */
- public Excursion redo(int places) {
- Excursion rerun = new Excursion(this.name, this.number+1, places);
- rerun.students.addAll(this.getReserves());
- this.students.removeAll(this.getReserves());
- return rerun;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement