Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. package ope.train;
  2.  
  3. /**
  4. * <P>The class <CODE>SleepingCar</CODE> represents the sleeping cars
  5. * of a train in a train ticket reservation system. Each sleeping car
  6. * is divided in cabins which have <CODE>SleepingCar.BEDS_PER_CABIN</CODE>
  7. * places (beds) for passengers. The cabins are numbered from one
  8. * upwards.</P>
  9. *
  10. * <P>This implementation works in such a way that whenever anyone
  11. * reserves any places in the car, at least one full cabin is reserved.</P>
  12. *
  13. * <P>In this simple model, no data is stored about who has reserved
  14. * which places (beds) in the cars. Cars will only know
  15. * if places are reserved or not.</P>
  16. */
  17. public class SleepingCar implements TrainCar {
  18.  
  19. /** The number of places in each sleeping car cabin. */
  20. public static final int BEDS_PER_CABIN = 3;
  21.  
  22.  
  23. // A boolean array would also do for this simple implementation.
  24. /** Tells how many beds in each cabin have been reserved. */
  25. private int[] cabinReservations; // one-way flags, equal to 0 initially, once reserved, equal to BEDS_PER_CABIN
  26.  
  27. /**
  28. * Creates a new sleeping car with the given number of cabins.
  29. *
  30. * @param numberOfCabins the number of cabins in the new car
  31. */
  32. public SleepingCar(int numberOfCabins) {
  33. this.cabinReservations = new int[numberOfCabins]; // elements set to zero
  34. }
  35.  
  36. /**
  37. * Tells how many places (beds) this car has for passengers.
  38. *
  39. * @return the total number of passenger places in the car
  40. */
  41. public int getNumberOfPlaces() {
  42. return BEDS_PER_CABIN * this.getNumberOfCabins();
  43. }
  44.  
  45.  
  46. /**
  47. * Tells how many percent of the car's passenger places have been reserved.
  48. *
  49. * @return the degree of reservation for this car (percentage: 0.0 - 100.0).
  50. */
  51. public double getDegreeOfReservation() {
  52. int cabinCount = this.getNumberOfCabins(); // temporary
  53.  
  54. if (this.getNumberOfFreePlaces() == 0){
  55. return 100.00;
  56. }
  57. double temp1 = cabinCount * BEDS_PER_CABIN;
  58. double temp2 = this.getNumberOfFreePlaces();
  59. return (temp1 - temp2)/temp1 * 100;
  60. }
  61.  
  62.  
  63. /**
  64. * Tells how many free (unreserved) places (beds) this car has for passengers.
  65. *
  66. * @return the number of free passenger places in the car
  67. */
  68. public int getNumberOfFreePlaces() {
  69. int freeCount = this.getNumberOfCabins(); // temporary
  70. int free = 0; // gatherer
  71. for (int cabinNumber = 1; cabinNumber <= freeCount; cabinNumber++) { // stepper
  72. free += this.getNumberOfFreeBedsInCabin(cabinNumber);
  73. }
  74. return free;
  75. }
  76.  
  77.  
  78. /**
  79. * Tells how many cabins there are in this car.
  80. *
  81. * @return the number of cabins in the car
  82. */
  83. public int getNumberOfCabins() {
  84. return this.cabinReservations.length;
  85. }
  86.  
  87.  
  88. /**
  89. * Tells how many free (unreserved) places (beds) there are in the indicated cabin.
  90. *
  91. * @param cabinNumber a cabin number (>=1)
  92. * @return the number of free places in the cabin
  93. */
  94. public int getNumberOfFreeBedsInCabin(int cabinNumber) {
  95. if (cabinNumber == 0) {
  96. System.out.println("Error");
  97. return 0;
  98. }
  99. return BEDS_PER_CABIN - this.cabinReservations[cabinNumber-1];
  100. }
  101.  
  102.  
  103. /**
  104. * Tells if the indicated cabin is empty. That is, tells whether none of its beds have been reserved or not.
  105. *
  106. * @param cabinNumber a cabin number (>=1)
  107. * @return a boolean value indicating if the cabin is empty
  108. */
  109. public boolean cabinIsEmpty(int cabinNumber) {
  110. return (this.getNumberOfFreeBedsInCabin(cabinNumber) == BEDS_PER_CABIN);
  111. }
  112.  
  113.  
  114. /**
  115. * Reserves all the places (beds) in one cabin. If the cabin was not originally empty, this method does nothing but return <CODE>false</CODE>.
  116. *
  117. * @param cabinNumber a cabin number (>=1)
  118. * @return a boolean value indicating if the cabin was successfully reserved
  119. */
  120. public boolean reserveCabin(int cabinNumber) {
  121. if (!this.cabinIsEmpty(cabinNumber)) {
  122. return false;
  123. }
  124. this.cabinReservations[cabinNumber-1] = BEDS_PER_CABIN;
  125. return true;
  126. }
  127.  
  128.  
  129. /**
  130. * Reserves places (beds) for a group. For a sleeping car, a group reservation means that
  131. * whole (originally empty) cabins are reserved so that all the members of the group fit
  132. * in them. E.g. if the group size is 7, and each cabin has three beds, three cabins will be
  133. * reserved.
  134. * <P>
  135. * The cabins to be reserved are selected simply so that the smallest possible empty
  136. * cabin numbers are determined, and those cabins are reserved. The cabins need not be
  137. * adjacent to each other.
  138. * <P>
  139. * If it is not possible to reserve suitable places for all members of the
  140. * group, no places are reserved at all.
  141. *
  142. * @param numberOfPeople the size of the group for which places are to be reserved
  143. * @return <CODE>true</CODE> if the group reservation was successful, <CODE>false</CODE> if not
  144. */
  145. public boolean reservePlaces(int numberOfPeople) {
  146. int cabins = numberOfPeople / BEDS_PER_CABIN; // stepper
  147. if (numberOfPeople % BEDS_PER_CABIN != 0) {
  148. cabins++;
  149. }
  150.  
  151. if (this.countEmptyCabins() < cabins) {
  152. return false;
  153. } else if (this.countEmptyCabins() >= cabins) {
  154.  
  155. for (int cabinNumber = 1; cabinNumber <= cabins; cabinNumber++) { // stepper
  156. this.reserveCabin(this.findEmptyCabin());
  157. }
  158. return true;
  159. }
  160. return true;
  161. }
  162.  
  163.  
  164.  
  165. /**
  166. * Returns the number of empty cabins in the car.
  167. *
  168. * @return the number of empty cabins in the car
  169. */
  170. private int countEmptyCabins() {
  171. int emptyCabins = 0; // stepper
  172. for (int cabinNumber = 1; cabinNumber <= this.getNumberOfCabins(); cabinNumber++) { // stepper
  173. if (this.cabinIsEmpty(cabinNumber)) {
  174. emptyCabins++;
  175. }
  176. }
  177. return emptyCabins;
  178. }
  179.  
  180.  
  181. /**
  182. * Searches for the first empty cabin in the car.
  183. *
  184. * @return the smallest empty cabin number in the car or a negative value, if no empty cabins can be found
  185. */
  186. private int findEmptyCabin() {
  187. for (int cabinNumber = 1; cabinNumber <= this.getNumberOfCabins(); cabinNumber++) { // stepper
  188. if (this.cabinIsEmpty(cabinNumber)) {
  189. return cabinNumber;
  190. }
  191. }
  192. return -1;
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement