Gears.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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.
  22. * Uses default-values if consisteny criteria are not met.
  23. * @param sprockets
  24. * @param rearSprockets
  25. */
  26. void setSprockets(int sprockets, int rearSprockets) {
  27. this.frontSprockets = sprockets;
  28. this.rearSprockets = rearSprockets;
  29. if (!(this.frontSprockets >= 1)) { // A.1
  30. this.frontSprockets = 1;
  31. } else if (!(this.frontSprockets < 4)) { // A.2
  32. this.frontSprockets = 3;
  33. }
  34. // B.1, B.2
  35. if (this.rearSprockets < 1 || this.rearSprockets > 9) {
  36. this.rearSprockets = this.frontSprockets * 3;
  37. }
  38. if (this.rearSprockets < this.frontSprockets) { // C.1
  39. this.rearSprockets = this.frontSprockets;
  40. } else if (this.rearSprockets > 3 * this.frontSprockets) { // C.2
  41. this.rearSprockets = 3 * this.frontSprockets;
  42. }
  43. }
  44. /**
  45. * @return the number of gears as the number of sprocket-combinations
  46. */
  47. int getNumberOfGears() {
  48. return frontSprockets * rearSprockets;
  49. }
  50. /**
  51. * @return the price of the gears
  52. */
  53. int getPrice() {
  54. return price;
  55. }
  56. }