Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. /*
  2.  
  3. A parking lot can have many slots which has different size.
  4. A slot can occupy at most two vehicles.
  5.  
  6. Checkin:
  7.  
  8. * Find the slot for the vehicle based on size
  9.  
  10. Checkout:
  11.  
  12. * Calculate total fare based on vehicle size and duration
  13.  
  14. Questions:
  15.  
  16. * Design bottom up or top down i.e. Vehicle, Slot or ParkingLot
  17. */
  18.  
  19. // Slot details
  20. capacity = [4, 2, 2];
  21. //filled = [true, false, false];
  22.  
  23. // Vehicle details
  24. sizes = [2, 1, 2, 2];
  25. parked = [1, 0, 1, 1];
  26. start = ['001', nil, '002', '003'];
  27. end = ['005', nil, '006', '008'];
  28.  
  29. // slot vehicle relation
  30. vehicleSlot = [0, nil, 0, 1];
  31.  
  32. function checkin (vehicle) {
  33. for slot, size in capacity:
  34. if capacity[slot] >= size[vehicle]:
  35. start[vehicle] = currentTimestamp;
  36. parked[vehicle] = 1;
  37. capacity[slot] -= size[vehicle];
  38. return true;
  39. return 'No slot empty'
  40. }
  41.  
  42. function checkout (vehicle) {
  43. if not parked[vehicle]:
  44. return 'Invalid checkout'
  45. parked[vehicle] = 0;
  46. slot = vehicleSlot[vehicle];
  47. capacity[slot] += size[vehicle];
  48. end[vehicle] = currentTimestamp;
  49. return pricingAlgo(vehicle);
  50. }
  51.  
  52. function pricingAlgo (vehicle) {
  53. // size * duration
  54. var duration = (end[vehicle] - start[vehicle]);
  55. return sizes[vehicle] * duration;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement