PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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