Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- A parking lot can have many slots which has different size.
- A slot can occupy at most two vehicles.
- Checkin:
- * Find the slot for the vehicle based on size
- Checkout:
- * Calculate total fare based on vehicle size and duration
- Questions:
- * Design bottom up or top down i.e. Vehicle, Slot or ParkingLot
- */
- // Slot details
- capacity = [4, 2, 2];
- //filled = [true, false, false];
- // Vehicle details
- sizes = [2, 1, 2, 2];
- parked = [1, 0, 1, 1];
- start = ['001', nil, '002', '003'];
- end = ['005', nil, '006', '008'];
- // slot vehicle relation
- vehicleSlot = [0, nil, 0, 1];
- function checkin (vehicle) {
- for slot, size in capacity:
- if capacity[slot] >= size[vehicle]:
- start[vehicle] = currentTimestamp;
- parked[vehicle] = 1;
- capacity[slot] -= size[vehicle];
- return true;
- return 'No slot empty'
- }
- function checkout (vehicle) {
- if not parked[vehicle]:
- return 'Invalid checkout'
- parked[vehicle] = 0;
- slot = vehicleSlot[vehicle];
- capacity[slot] += size[vehicle];
- end[vehicle] = currentTimestamp;
- return pricingAlgo(vehicle);
- }
- function pricingAlgo (vehicle) {
- // size * duration
- var duration = (end[vehicle] - start[vehicle]);
- return sizes[vehicle] * duration;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement