Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'uuid' from uuid
- class Car {
- contructor (entryTime) {
- assert(Date(entryTime).properFormat('YY MM DD HH MM SS'))
- this.entryTime = entryTime
- this.carId = new uuid()
- }
- getEntryTime {
- return this.entryTime
- }
- getCarId {
- return this.carId
- }
- }
- class ParkingLot {
- carsInLot = []
- constructor (limit, fee) { // fee = 0.5, limi = 100
- assert(limit > 1)
- assert(fee >= 0)
- this.limit = limit
- this.fee = fee
- }
- enter(entryTime) {
- if (carsInLot.length < this.limit) {
- const newCar = new Car(entryTime)
- this.carsInLot.append(newCar)
- return id
- } else {
- return "Parking Lot in full"
- }
- }
- exit(exitTime, id) {
- const foundCarId = this.carsInLot.find(car => car.id)
- this.carsInLot.remove(car => car.id)
- const timeSpent = Hours(Date(exitTime) - Date(foundCar.getEntryTime())) // 1.5 hours
- const cost = Math.ceil(timeSpent * this.fee) // $1.00
- return cost
- }
- }
- // if limit < 1
- const P1 = new ParkingLot(0, 0.5) // fail because we cant hava. lot with no spaces
- const P2 = new ParkingLot(5, -1) // fail beacuse negavtive fee
- const P3 = new ParkingLot(3, 1)
- P3.enter(1, '1:00pm') // id 1
- P3.enter(2, '1:00pm') // id 2
- P3.enter('2:00pm') // id 3
- p3.enter('3:00pm') // fail because the lot has max of 3 cars
- p3.exit('2:00pm', 1) // 1 hour * 1$ is $1 he gets charged $1
Advertisement
Add Comment
Please, Sign In to add comment