Bike.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * A bike model in Java.
  3. *
  4. * @author Markus Iser, Martin Thoma
  5. * @version 1.0
  6. */
  7. public class Bike {
  8. private final Gears gears;
  9. private final Wheels wheels;
  10. enum Material {
  11. ALU, STEEL, TITAN
  12. }
  13. private final Material material;
  14. private final String modelId;
  15. private final boolean hasBell;
  16. private final boolean hasLights;
  17. /** Price of the bike in cents. */
  18. private int price;
  19. Bike(String modelId) {
  20. this.gears = null;
  21. this.wheels = null;
  22. this.material = Material.ALU;
  23. this.modelId = modelId;
  24. this.hasBell = true;
  25. this.hasLights = true;
  26. }
  27. Bike(Gears gears, Wheels wheels, Material material,
  28. String modelId, boolean bell, boolean lights) {
  29. this.gears = gears;
  30. this.wheels = wheels;
  31. this.material = material;
  32. switch (material) {
  33. case ALU:
  34. price = 20000;
  35. break;
  36. case STEEL:
  37. price = 30000;
  38. break;
  39. case TITAN:
  40. price = 40000;
  41. break;
  42. }
  43. this.modelId = modelId;
  44. this.hasBell = bell;
  45. this.hasLights = lights;
  46. }
  47. /**
  48. * Check if the bike is legal for usage on streets.
  49. *
  50. * @return {@code true} if the bike has a bell and has lights
  51. */
  52. public boolean isStreetLegal() {
  53. return hasBell && hasLights;
  54. }
  55. /**
  56. * Get the price of the bike.
  57. *
  58. * @return the sum of the bike's base-price and the price of the wheels and
  59. * gears
  60. */
  61. public int getPrice() {
  62. return price + gears.getPrice() + wheels.getPrice();
  63. }
  64. /**
  65. * @return the material of this bike
  66. */
  67. public Material getMaterial() {
  68. return this.material;
  69. }
  70. /**
  71. * @return the model id of this bike
  72. */
  73. public String getModelId() {
  74. return this.modelId;
  75. }
  76. /*
  77. * (non-Javadoc)
  78. *
  79. * @see java.lang.Object#toString()
  80. */
  81. @Override
  82. public String toString() {
  83. return "Bike[" + modelId + "]";
  84. }
  85. }