Gears.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * This class models the gears. It is restricted to derailleur gears.
  3. *
  4. * @author Markus Iser, Martin Thoma
  5. * @version 1.0
  6. */
  7. public class Gears {
  8. public static final int MIN_FRONT_SPROCKETS = 1;
  9. public static final int MAX_FRONT_SPROCKETS = 3;
  10. public static final int MIN_REAR_SPROCKETS = 1;
  11. public static final int MAX_REAR_SPROCKETS = 10;
  12. private int frontSprockets;
  13. private int rearSprockets;
  14. /** Price in cents. */
  15. private final int price;
  16. Gears(int frontSprockets, int rearSprockets, int price) {
  17. setSprockets(frontSprockets, rearSprockets);
  18. this.price = price;
  19. }
  20. /**
  21. * Sets the sprocket numbers. Uses default-values if consisteny criteria are
  22. * not met.
  23. *
  24. * @param sprockets
  25. * @param rearSprockets
  26. */
  27. void setSprockets(int sprockets, int rearSprockets) {
  28. this.frontSprockets = sprockets;
  29. this.rearSprockets = rearSprockets;
  30. if (!(this.frontSprockets >= 1)) { // A.1
  31. this.frontSprockets = 1;
  32. } else if (!(this.frontSprockets < 4)) { // A.2
  33. this.frontSprockets = 3;
  34. }
  35. if (this.rearSprockets < 1) { // B.1
  36. this.rearSprockets = this.frontSprockets;
  37. }
  38. if (this.rearSprockets > 9) { // B.2
  39. this.rearSprockets = this.frontSprockets * 3;
  40. }
  41. if (this.rearSprockets < this.frontSprockets) { // C.1
  42. this.rearSprockets = this.frontSprockets;
  43. } else if (this.rearSprockets > 3 * this.frontSprockets) { // C.2
  44. this.rearSprockets = 3 * this.frontSprockets;
  45. }
  46. }
  47. /**
  48. * @return the number of gears as the number of sprocket-combinations
  49. */
  50. int getNumberOfGears() {
  51. return frontSprockets * rearSprockets;
  52. }
  53. /**
  54. * @return the price of the gears
  55. */
  56. int getPrice() {
  57. return price;
  58. }
  59. }