PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

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