今日无事,写一道面试题目来练练手。
题目要求
- 某停车场,分3层,每层100个车位
- 每个车位都能监控到车辆的驶入和离开
- 车辆进入前,显示每层的空余车位数量
- 车辆进入时,摄像头可识别车牌号和时间
- 车辆出来时,出口显示器显示车牌号和停车时长
编码
// 停车场
class Park {
constructor() {
this.parkNumList = []; // 每层空余车位
}
getPrakNumList() {
for (let i of this.parkNumList) {
console.log(`第${i.floor}层停车场,剩余停车位${i.parkNum}`);
}
}
registerFloor(floor) {
this.parkNumList.push(floor);
}
}
// 层数
class Floor {
constructor(floor, num) {
this.floor = floor; // 层数
this.parkNum = num; // 该层剩余车空位
}
carEnter() {
this.parkNum = this.parkNum - 1;
}
carLeave() {
this.parkNum = this.parkNum + 1;
}
}
// 停车位
class ParkSpace {
constructor(num, belongFloor) {
this.parkSpaceNum = num; //停车位编号
this.belongFloor = belongFloor; //停车位属于哪一层
this.isAir = false; //停车位是否空闲
}
monitorCarEnter() {
this.isAir = false;
this.belongFloor.carEnter();
console.log('-------停车位已监控到车辆进入------');
}
monitorCarLeave() {
this.isAir = true;
this.belongFloor.carLeave();
console.log('-----停车位已监控到车辆离开-----');
}
}
// 车
class Car {
constructor(carNum) {
this.carNum = carNum; // 车牌号
this.carEnterTime = ''; // 车进入停车位的时间
this.carLeaveTime = ''; // 车离开停车位的时间
}
beforeEnter(park) {
park.getPrakNumList();
}
enter(parkSpace, camera, enterTime) {
this.carEnterTime = enterTime;
parkSpace.monitorCarEnter();
camera.shot(this);
}
leave(parkSpace, monitor, leaveTime) {
this.carLeaveTime = leaveTime;
parkSpace.monitorCarLeave();
monitor.show(this);
}
}
// 摄像头
class Camera {
shot(car) {
console.log(`摄像头已识别车牌号:${car.carNum}`);
console.log(`摄像头已识别车辆进入时间:${car.carEnterTime}`);
}
}
// 出口显示器
class Monitor {
show(car) {
console.log(`出口显示器显示车牌号:${car.carNum}`);
console.log(`出口显示器显示车辆停车时长:${car.carEnterTime} - ${car.carLeaveTime}`);
}
}
// 舒店长要开停车场营业啦~
let park = new Park(); //初始化停车场
let firstFloor = new Floor(1, 100); //初始化一楼停车场
park.registerFloor(firstFloor);
let secondFloor = new Floor(2, 100); //初始化二楼停车场
park.registerFloor(secondFloor);
let thirdFloor = new Floor(3, 100); //初始化三楼停车场
park.registerFloor(thirdFloor);
let camera = new Camera(); //初始化摄像头
let monitor = new Monitor(); //初始化出口显示屏
// 欢迎第一辆车停车~
let car1 = new Car('桂G8888');
car1.beforeEnter(park);
let parkSpace1 = new ParkSpace(88, secondFloor); //这个车想停在二楼停车场的88号位置
car1.enter(parkSpace1, camera, '12:45:30');
// 然后又来了第二辆车~
let car2 = new Car('赣B8888');
car2.beforeEnter(park);
let parkSpace2 = new ParkSpace(88, thirdFloor); //这个车想停在三楼停车场的88号位置
car2.enter(parkSpace2, camera, '23:01:30');
// 然后又来了第三辆车~
let car3 = new Car('粤A6666');
car3.beforeEnter(park);
let parkSpace3 = new ParkSpace(88, thirdFloor); //这个车想停在一楼停车场的88号位置
car3.enter(parkSpace3, camera, '19:01:30');
// 然后又来了第四辆车~但是没有停车~唉
let car4 = new Car('粤A6666');
car4.beforeEnter(park);
运行结果
result.png小结
尽管完成了题目的要求,嗯嗯嗯嗯嗯...后续还需要优化一下。
网友评论