hanami_unami

Untitled

Mar 12th, 2022
1,322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'uuid' from uuid
  2.  
  3. class Car {
  4.   contructor (entryTime) {
  5.     assert(Date(entryTime).properFormat('YY MM DD HH MM SS'))
  6.     this.entryTime = entryTime
  7.     this.carId = new uuid()
  8.   }
  9.   getEntryTime {
  10.     return this.entryTime
  11.   }
  12.     getCarId {
  13.     return this.carId
  14.   }
  15. }
  16.  
  17.  
  18. class ParkingLot {
  19.   carsInLot = []
  20.   constructor (limit, fee) { // fee = 0.5, limi = 100
  21.     assert(limit > 1)
  22.     assert(fee >= 0)
  23.     this.limit = limit
  24.     this.fee = fee
  25.   }
  26.  
  27.     enter(entryTime) {
  28.     if (carsInLot.length < this.limit) {
  29.       const newCar = new Car(entryTime)
  30.         this.carsInLot.append(newCar)
  31.       return id
  32.     } else {
  33.       return "Parking Lot in full"
  34.     }
  35.   }
  36.  
  37.     exit(exitTime, id) {
  38.     const foundCarId = this.carsInLot.find(car => car.id)
  39.     this.carsInLot.remove(car => car.id)
  40.     const timeSpent = Hours(Date(exitTime) - Date(foundCar.getEntryTime())) // 1.5 hours
  41.     const cost = Math.ceil(timeSpent * this.fee) // $1.00
  42.     return cost
  43.   }
  44.  
  45. }
  46.  
  47. // if limit < 1
  48. const P1 = new ParkingLot(0, 0.5) // fail because we cant hava. lot with no spaces
  49. const P2 = new ParkingLot(5, -1) // fail beacuse negavtive fee
  50. const P3 = new ParkingLot(3, 1)
  51. P3.enter(1, '1:00pm') // id 1
  52. P3.enter(2, '1:00pm') // id 2
  53. P3.enter('2:00pm') // id 3
  54. p3.enter('3:00pm') // fail because the lot has max of 3 cars
  55. p3.exit('2:00pm', 1) // 1 hour * 1$ is $1 he gets charged $1
  56.  
Advertisement
Add Comment
Please, Sign In to add comment