/tutorial/parking/parking_simulator.e

http://github.com/tybor/Liberty · Specman e · 147 lines · 130 code · 8 blank · 9 comment · 8 complexity · 50c4fa9b8d665f2cfdcafbdd21d4fc46 MD5 · raw file

  1. class PARKING_SIMULATOR
  2. --
  3. -- Main command-loop to simulate the PARKING example (compile and run this file).
  4. --
  5. create {ANY}
  6. make
  7. feature {}
  8. make
  9. local
  10. parking: PARKING; command: STRING; price: REAL; i, value: INTEGER; stop: BOOLEAN; split: ARRAY[STRING]
  11. do
  12. from
  13. create parking.make({ARRAY[LEVEL] 1, << create {LEVEL}.make(10), create {LEVEL}.make(8), create {LEVEL}.make(4) >> })
  14. io.put_string("%NParking simulator command loop.%N%N")
  15. io.put_string(help_text)
  16. until
  17. stop
  18. loop
  19. io.put_string(once "Type your command: ")
  20. io.flush
  21. io.read_line
  22. command := io.last_string.twin
  23. command.left_adjust
  24. command.right_adjust
  25. inspect
  26. command
  27. when "q", "Q", "quit" then
  28. stop := True
  29. when "a", "A" then
  30. value := parking.arrival
  31. if value > 0 then
  32. io.put_string("Arrival of car #" + value.to_string + ".%N")
  33. else
  34. io.put_string("No more places to park (no vacancy).%N")
  35. end
  36. when "?", "h", "help" then
  37. io.put_string(help_text)
  38. when "T" then
  39. parking.clock.display_on(io)
  40. when "c" then
  41. io.put_string("Number of cars in the parking: " + parking.occupied_slot_count.to_string + "%N")
  42. when "H" then
  43. io.put_string("Parking hour price: $" + parking.hour_price.to_string_format(3) + "%N")
  44. else
  45. split := command.split
  46. inspect
  47. split.first
  48. when "l", "L" then
  49. i := integer_from(split.last)
  50. if error_flag then
  51. error_flag := False
  52. elseif i < parking.lower_level then
  53. io.put_string("Level number too small.%N")
  54. elseif i > parking.upper_level then
  55. io.put_string("Level number too big.%N")
  56. else
  57. -- No error:
  58. io.put_string("Number of cars in level " + i.to_string + ": " + parking.level_occupied_slot_count(i).to_string + "%N")
  59. end
  60. when "t" then
  61. i := integer_from(split.last)
  62. if error_flag then
  63. error_flag := False
  64. elseif i <= 0 then
  65. io.put_string("It is not a valid time (negative).%N")
  66. else
  67. -- No error:
  68. parking.add_time(i)
  69. end
  70. when "H" then
  71. price := real_from(split.last)
  72. if error_flag then
  73. error_flag := False
  74. elseif price <= 0 then
  75. io.put_string("It is not a valid price.%N")
  76. else
  77. -- No error:
  78. parking.set_hour_price(price)
  79. end
  80. when "d" then
  81. i := integer_from(split.last)
  82. if error_flag then
  83. error_flag := False
  84. elseif i <= 0 then
  85. io.put_string("Car number too small.%N")
  86. else
  87. -- No error:
  88. price := parking.departure(i)
  89. if price < 0 then
  90. io.put_string("This car is probably outside of the parking.%N")
  91. else
  92. io.put_string("The price paid by this car is $" + price.to_string_format(2) + "%N")
  93. end
  94. end
  95. else
  96. io.put_string("Unknown Command: %"" + command + "%"%N")
  97. end
  98. end
  99. end
  100. io.put_string("Bye.%N")
  101. end
  102. error_flag: BOOLEAN -- Used to signal an input error.
  103. integer_from (string: STRING): INTEGER
  104. require
  105. string /= Void
  106. do
  107. if string.is_integer then
  108. Result := string.to_integer
  109. else
  110. error_flag := True
  111. io.put_string("%"" + string + "%" is not an integer value.%N")
  112. end
  113. end
  114. real_from (string: STRING): REAL
  115. require
  116. string /= Void
  117. do
  118. if string.is_real then
  119. Result := string.to_real
  120. else
  121. error_flag := True
  122. io.put_string("%"" + string + "%" is not an real value.%N")
  123. end
  124. end
  125. help_text: STRING "{
  126. Commands:
  127. ------------------------------------
  128. ? display this help
  129. q Quit this simulator
  130. T display current Time
  131. t <i> add <i> minutes to the current time
  132. a Arrival of a new car into the parking
  133. d <i> Departure of the car number <i>
  134. l <i> number of cars at Level <i>
  135. H [<x>] display / change Hour parking price with <x>
  136. c total Count of cars in the whole parking
  137. ------------------------------------
  138. }"
  139. end -- class PARKING_SIMULATOR