/tutorial/parking/level.e

http://github.com/tybor/Liberty · Specman e · 100 lines · 81 code · 13 blank · 6 comment · 3 complexity · 8c9d5988bb3672c1e21860f239055a9c MD5 · raw file

  1. class LEVEL
  2. creation {ANY}
  3. make
  4. feature {ANY}
  5. occupied_slot_count: INTEGER
  6. -- Total count of occupied places in the `Current' level.
  7. free_slot_count: INTEGER is
  8. -- Total count of free places in the `Current' level.
  9. do
  10. Result := car_slots.count - occupied_slot_count
  11. end
  12. capacity: INTEGER is
  13. -- Of the level.
  14. do
  15. Result := car_slots.count
  16. end
  17. is_full: BOOLEAN is
  18. -- Is this level full?
  19. do
  20. Result := occupied_slot_count = capacity
  21. end
  22. feature {ANY} -- Modifications:
  23. make (max_cars: INTEGER) is
  24. require
  25. max_cars > 0
  26. do
  27. create car_slots.make(1, max_cars)
  28. occupied_slot_count := 0
  29. ensure
  30. occupied_slot_count = 0
  31. end
  32. arrival (car: CAR) is
  33. require
  34. car /= Void
  35. not is_full
  36. local
  37. i: INTEGER
  38. do
  39. from
  40. i := car_slots.lower
  41. until
  42. car_slots.item(i) = Void
  43. loop
  44. i := i + 1
  45. end
  46. car_slots.put(car, i)
  47. occupied_slot_count := occupied_slot_count + 1
  48. ensure
  49. occupied_slot_count >= old occupied_slot_count + 1
  50. end
  51. departure (car_number: INTEGER; departure_time: DATE; hour_price: REAL): REAL is
  52. -- Gives price to pay or -1 when car is not at this level.
  53. require
  54. car_number > 0
  55. local
  56. i: INTEGER; car: CAR
  57. do
  58. from
  59. i := car_slots.lower
  60. until
  61. car /= Void or else i > car_slots.upper
  62. loop
  63. car := car_slots.item(i)
  64. if car /= Void and then car.number /= car_number then
  65. car := Void
  66. end
  67. i := i + 1
  68. end
  69. if car = Void then
  70. Result := -1
  71. else
  72. Result := car.price(departure_time, hour_price)
  73. check
  74. car_slots.item(i - 1) = car
  75. end
  76. car_slots.put(Void, i - 1)
  77. occupied_slot_count := occupied_slot_count - 1
  78. end
  79. end
  80. feature {}
  81. car_slots: ARRAY[CAR]
  82. -- Memory of occupied slots of this level.
  83. invariant
  84. occupied_slot_count.in_range(0, capacity)
  85. free_slot_count.in_range(0, capacity)
  86. occupied_slot_count + free_slot_count = capacity
  87. end -- class LEVEL