6 面试题示例 – 前端设计模式系列教程

1 第一题

  • 打车时,可以选择专车或快车。任何车都有车牌号和名称
  • 不同车价格不同,快车每公里1元,专车2元
  • 行程开始时,显示车辆信息
  • 行程结束时,显示打车金额(假定行程5公里)

UML类图

代码:

class Car {
    constructor(number,name){
        this.number = number
        this.name = name
    }
}
class Fast extends Car{
    constructor(number,name){
        super(number,name)
        this.price = 15
    }
}
class Pro extends Car{
    constructor(number,name){
        super(number,name)
        this.price = 20
    }
}
class Trip{
    constructor(car){
        this.car = car
    }
    start(){
        console.log(`行程开始,名称:${this.car.name},车牌号:${this.car.number}`)
    }
    end(){
        console.log(`行程结束,金额:${this.car.price * 5}`)
    }
}

let car = new Fast(100220,'桑塔纳')
let trip = new Trip(car)
trip.start()
trip.end()

 

2 第二题

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

UML类图

代码:

// 摄像头
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)
    }
}
// 停车场类
class Park {
    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.emptyPlaceNumber()}`
        }).join('\n')
    }
    in(car) {
        const info = this.camera.shot(car)
        const i = parseInt(Math.random() * 100 % 100)
        const place = this.floors[parseInt(Math.random() * 3 % 100)].places[i]
        place.in()
        info.place = place
        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 {
    constructor(index, places) {
        this.index = index
        this.places = places || []
    }
    emptyPlaceNumber() {
        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 Car {
    constructor(num) {
        this.num = num
    }
}
// 创建楼层和停车位
const floors = []
for (let i = 0; i < 3; i++) {
    const places = []
    for (let j = 0; j < 100; j++) {
        places[j] = new Place()
    }
    floors[i] = new Floor(i + 1, places)
}

// 创建停车场
const park = new Park(floors)
// 创建3个车辆
const car1 = new Car(1001)
const car2 = new Car(1002)
const car3 = new Car(1003)
console.log('第一辆车进入')
console.log(park.emptyNum())
park.in(car1)
console.log('第二辆车进入')
console.log(park.emptyNum())
park.in(car2)
console.log('第二辆车离开')
console.log(park.emptyNum())
park.out(car2)
console.log('第三辆车进入')
console.log(park.emptyNum())
park.in(car3)
console.log('第三辆车离开')
console.log(park.emptyNum())
park.out(car3)

 

本站文章如未注明均为原创 | 文章作者:刘晓帆 | 转载请注明来自:前端印象

发表评论

邮箱地址不会被公开。 必填项已用*标注

浏览量:29 次浏览