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