Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

停车场 #31

Open
yizihan opened this issue Aug 6, 2018 · 0 comments
Open

停车场 #31

yizihan opened this issue Aug 6, 2018 · 0 comments

Comments

@yizihan
Copy link
Owner

yizihan commented Aug 6, 2018

题目

  • 某停车场,分3层,每层100车位
  • 每个车位都能监控到车辆的驶入和离开
  • 车辆进入前,显示每层的空余车位数量
  • 车辆进入时,摄像头可识别车牌号和时间
  • 车辆出来时,出口显示器显示车辆号和停车时长

UML

uml

解答

class Car {
  constructor (num) {
    this.num = num
  }
}

class Park {
  // 依赖 Camera、Screen 类
  constructor (floors) {
    this.floors = floors || []
    this.camera = new Camera()
    this.screen = new Screen()
    // 用于存放进场车辆信息
    this.carList = {}
  }
  emptyNum () {
    return this.floors.map(floor => {
      return `${floor.index} 层还有 ${floor.emptyPlaceNum()} 个空闲车位`
    }).join('\n')
  }
  in (car) {
    // 通过摄像头获取进停车场信息
    const info = this.camera.shot(car)
    // 0~100随机数
    const i = parseInt(Math.random() * 100 % 100)
    const place = this.floors[0].places[i]
    // 车入停车位
    place.in()
    // 停车位置
    info.place = place
    // 以车牌号为key,存储车辆车牌号、进场时间、停放位置
    this.carList[car.num] = info
  }
  out (car) {
    // 读取当前车辆的停车信息
    const info = this.carList[car.num]
    const place = info.place
    // 清空停车位
    place.out()
    // 显示车辆离开停车场时长
    this.screen.show(car, info.inTime)
    // 清空记录,避免内存泄漏 !!!
    delete this.carList[car.num]
  }
}

class Floor {
  // 依赖 Place 类
  constructor (index, places) {
    this.index = index
    this.places = places || []
  }
  // 每层空余车位数
  emptyPlaceNum () {
    let num = 0
    // 遍历本层所有车位,找出空余车位
    this.places.forEach(p => {
      if (p.empty) {
        num = num + 1
      }
    })
    return num
  }
}

class Place {
  constructor () {
    this.empty = true
  }
  in () {
    this.empty = false
  }
  out () {
    this.empty = true
  }
}

class Camera {
  shot (car) {
    return {
      num: car.num,
      inTime: Date.now()
    }
  }
}

class Screen {
  show (car, inTime) {
    console.log('车牌号', car.num)
    console.log('停车时间', Date.now() - inTime)
  }
}

实际测试

// 初始化停车场
const floors = []
for (let i = 0; i < 3; i++) {
  const places = []
  // 初始化100个停车位实例
  for (let j = 0; j < 100; j++) {
    places[j] = new Place()
  }
  // 初始化三个楼层实例,每层100个停车位
  floors[i] = new Floor(i + 1, places)
}
// 初始化停车场
const park = new Park(floors)

const car1 = new Car(100)
const car2 = new Car(200)
const car3 = new Car(300)

console.log('第一辆车进入')
console.log(park.emptyNum())
park.in(car1)
console.log('第二辆车进入')
console.log(park.emptyNum())
park.in(car2)
console.log('第一辆车离开')
park.out(car1)
console.log('d第二辆车离开')
park.out(car2)

console.log('第三辆车进入')
console.log(park.emptyNum())
park.in(car3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant