Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ope.train;
- /**
- * <P>The class <CODE>SleepingCar</CODE> represents the sleeping cars
- * of a train in a train ticket reservation system. Each sleeping car
- * is divided in cabins which have <CODE>SleepingCar.BEDS_PER_CABIN</CODE>
- * places (beds) for passengers. The cabins are numbered from one
- * upwards.</P>
- *
- * <P>This implementation works in such a way that whenever anyone
- * reserves any places in the car, at least one full cabin is reserved.</P>
- *
- * <P>In this simple model, no data is stored about who has reserved
- * which places (beds) in the cars. Cars will only know
- * if places are reserved or not.</P>
- */
- public class SleepingCar implements TrainCar {
- /** The number of places in each sleeping car cabin. */
- public static final int BEDS_PER_CABIN = 3;
- // A boolean array would also do for this simple implementation.
- /** Tells how many beds in each cabin have been reserved. */
- private int[] cabinReservations; // one-way flags, equal to 0 initially, once reserved, equal to BEDS_PER_CABIN
- /**
- * Creates a new sleeping car with the given number of cabins.
- *
- * @param numberOfCabins the number of cabins in the new car
- */
- public SleepingCar(int numberOfCabins) {
- this.cabinReservations = new int[numberOfCabins]; // elements set to zero
- }
- /**
- * Tells how many places (beds) this car has for passengers.
- *
- * @return the total number of passenger places in the car
- */
- public int getNumberOfPlaces() {
- return BEDS_PER_CABIN * this.getNumberOfCabins();
- }
- /**
- * Tells how many percent of the car's passenger places have been reserved.
- *
- * @return the degree of reservation for this car (percentage: 0.0 - 100.0).
- */
- public double getDegreeOfReservation() {
- int cabinCount = this.getNumberOfCabins(); // temporary
- if (this.getNumberOfFreePlaces() == 0){
- return 100.00;
- }
- double temp1 = cabinCount * BEDS_PER_CABIN;
- double temp2 = this.getNumberOfFreePlaces();
- return (temp1 - temp2)/temp1 * 100;
- }
- /**
- * Tells how many free (unreserved) places (beds) this car has for passengers.
- *
- * @return the number of free passenger places in the car
- */
- public int getNumberOfFreePlaces() {
- int freeCount = this.getNumberOfCabins(); // temporary
- int free = 0; // gatherer
- for (int cabinNumber = 1; cabinNumber <= freeCount; cabinNumber++) { // stepper
- free += this.getNumberOfFreeBedsInCabin(cabinNumber);
- }
- return free;
- }
- /**
- * Tells how many cabins there are in this car.
- *
- * @return the number of cabins in the car
- */
- public int getNumberOfCabins() {
- return this.cabinReservations.length;
- }
- /**
- * Tells how many free (unreserved) places (beds) there are in the indicated cabin.
- *
- * @param cabinNumber a cabin number (>=1)
- * @return the number of free places in the cabin
- */
- public int getNumberOfFreeBedsInCabin(int cabinNumber) {
- if (cabinNumber == 0) {
- System.out.println("Error");
- return 0;
- }
- return BEDS_PER_CABIN - this.cabinReservations[cabinNumber-1];
- }
- /**
- * Tells if the indicated cabin is empty. That is, tells whether none of its beds have been reserved or not.
- *
- * @param cabinNumber a cabin number (>=1)
- * @return a boolean value indicating if the cabin is empty
- */
- public boolean cabinIsEmpty(int cabinNumber) {
- return (this.getNumberOfFreeBedsInCabin(cabinNumber) == BEDS_PER_CABIN);
- }
- /**
- * Reserves all the places (beds) in one cabin. If the cabin was not originally empty, this method does nothing but return <CODE>false</CODE>.
- *
- * @param cabinNumber a cabin number (>=1)
- * @return a boolean value indicating if the cabin was successfully reserved
- */
- public boolean reserveCabin(int cabinNumber) {
- if (!this.cabinIsEmpty(cabinNumber)) {
- return false;
- }
- this.cabinReservations[cabinNumber-1] = BEDS_PER_CABIN;
- return true;
- }
- /**
- * Reserves places (beds) for a group. For a sleeping car, a group reservation means that
- * whole (originally empty) cabins are reserved so that all the members of the group fit
- * in them. E.g. if the group size is 7, and each cabin has three beds, three cabins will be
- * reserved.
- * <P>
- * The cabins to be reserved are selected simply so that the smallest possible empty
- * cabin numbers are determined, and those cabins are reserved. The cabins need not be
- * adjacent to each other.
- * <P>
- * If it is not possible to reserve suitable places for all members of the
- * group, no places are reserved at all.
- *
- * @param numberOfPeople the size of the group for which places are to be reserved
- * @return <CODE>true</CODE> if the group reservation was successful, <CODE>false</CODE> if not
- */
- public boolean reservePlaces(int numberOfPeople) {
- int cabins = numberOfPeople / BEDS_PER_CABIN; // stepper
- if (numberOfPeople % BEDS_PER_CABIN != 0) {
- cabins++;
- }
- if (this.countEmptyCabins() < cabins) {
- return false;
- } else if (this.countEmptyCabins() >= cabins) {
- for (int cabinNumber = 1; cabinNumber <= cabins; cabinNumber++) { // stepper
- this.reserveCabin(this.findEmptyCabin());
- }
- return true;
- }
- return true;
- }
- /**
- * Returns the number of empty cabins in the car.
- *
- * @return the number of empty cabins in the car
- */
- private int countEmptyCabins() {
- int emptyCabins = 0; // stepper
- for (int cabinNumber = 1; cabinNumber <= this.getNumberOfCabins(); cabinNumber++) { // stepper
- if (this.cabinIsEmpty(cabinNumber)) {
- emptyCabins++;
- }
- }
- return emptyCabins;
- }
- /**
- * Searches for the first empty cabin in the car.
- *
- * @return the smallest empty cabin number in the car or a negative value, if no empty cabins can be found
- */
- private int findEmptyCabin() {
- for (int cabinNumber = 1; cabinNumber <= this.getNumberOfCabins(); cabinNumber++) { // stepper
- if (this.cabinIsEmpty(cabinNumber)) {
- return cabinNumber;
- }
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement