/tutorial/parking/parking.e

http://github.com/tybor/Liberty · Specman e · 141 lines · 112 code · 20 blank · 9 comment · 3 complexity · 844702f9e91e163aa02cf251a5dc11a8 MD5 · raw file

  1. class PARKING
  2. creation {ANY}
  3. make
  4. feature {ANY}
  5. lower_level: INTEGER is
  6. -- The actual lowest level number.
  7. do
  8. Result := level_list.lower
  9. end
  10. upper_level: INTEGER is
  11. -- The actual upper most level number.
  12. do
  13. Result := level_list.upper
  14. end
  15. hour_price: REAL
  16. -- The day time price to pay per hour.
  17. clock: DATE
  18. -- The parking `clock'.
  19. occupied_slot_count: INTEGER is
  20. -- Total number of cars inside the parking.
  21. local
  22. i: INTEGER
  23. do
  24. from
  25. i := lower_level
  26. until
  27. i > upper_level
  28. loop
  29. Result := Result + level_occupied_slot_count(i)
  30. i := i + 1
  31. end
  32. end
  33. level_occupied_slot_count (level_number: INTEGER): INTEGER is
  34. -- Total number of cars in the given `level_number'.
  35. require
  36. level_number.in_range(lower_level, upper_level)
  37. do
  38. Result := level_list.item(level_number).occupied_slot_count
  39. ensure
  40. Result >= 0
  41. end
  42. feature {ANY} -- Modifications:
  43. arrival: INTEGER is
  44. -- Arrival of a new car. The `Result' is the number of the new car or 0 when the parking is full.
  45. local
  46. i: INTEGER; car: CAR
  47. do
  48. from
  49. i := lower_level
  50. until
  51. i > upper_level or else not level_list.item(i).is_full
  52. loop
  53. i := i + 1
  54. end
  55. if i > upper_level or else level_list.item(i).is_full then
  56. Result := 0
  57. else
  58. last_car_number := last_car_number + 1
  59. create car.make(last_car_number, clock.twin)
  60. level_list.item(i).arrival(car)
  61. Result := last_car_number
  62. end
  63. ensure
  64. Result >= 0
  65. end
  66. departure (car: INTEGER): REAL is
  67. -- Gives the price to pay or -1 when cannot be found in the parking.
  68. require
  69. car > 0
  70. local
  71. i: INTEGER; stop: BOOLEAN; c: like clock
  72. do
  73. from
  74. i := lower_level
  75. stop := level_list.count <= 0
  76. Result := -1
  77. c := clock.twin
  78. until
  79. stop
  80. loop
  81. Result := level_list.item(i).departure(car, c, hour_price)
  82. i := i + 1
  83. stop := Result >= 0 or i > upper_level
  84. end
  85. end
  86. add_time (incr: INTEGER) is
  87. do
  88. clock.add_time(incr)
  89. end
  90. set_hour_price (hp: REAL) is
  91. require
  92. hp >= 0
  93. do
  94. hour_price := hp
  95. ensure
  96. hour_price = hp
  97. end
  98. feature {}
  99. default_hour_price: REAL is 1.50
  100. make (ll: like level_list) is
  101. require
  102. ll /= Void
  103. do
  104. sedb_breakpoint
  105. create clock.make(0, 360)
  106. hour_price := default_hour_price
  107. level_list := ll
  108. ensure
  109. hour_price = default_hour_price
  110. level_list = ll
  111. last_car_number = 0
  112. end
  113. level_list: ARRAY[LEVEL]
  114. last_car_number: INTEGER
  115. -- Used to give different numbers for cars.
  116. invariant
  117. valid_price: hour_price >= 0
  118. clock /= Void
  119. last_car_number >= 0
  120. level_list /= Void
  121. end -- class PARKING