类图示例

示例

第一题

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

题目:

  • 画出UML类图
  • 用ES6语法写出该示例

解答

  • UML类图
classDiagram class Car{ +carno:String +carName:String } class Trip{ +car:car +carStart():Car +carEnd():Fee } class SpeedCar{ price:Number } class SpecialCar{ price:Number } SpeedCar --|> Car SpecialCar --|> Car Trip --> Car
  • 代码
/**
 * > 第一题
+ 打车时,可以打专车或者快车。任何车都有车牌号和名称。
+ 不同车价格不同,快车每公里1元,专车每公里2元。
+ 行程开始时,显示车辆信息
+ 行程结束时,显示打车金额(假定行程就5公里)

题目:
+ 画出UML类图
+ 用ES6语法写出该示例
 */

 //车 父类
class Car{
    constructor(number,name){
        this.number = number;
        this.name = name;
    }
}

//快车
class Kuaiche extends Car{
    constructor(number,name,price){
        super(number,name);
        this.price = 1;
    }
}

//专车
class Zhuanche extends Car{
    constructor(number,name,price){
        super(number,name);
        this.price = 2;
    }
}

//行程
class Trip{
    constructor(car){
        this.car = car;
    }
    startCar(){
        console.log(`行程开始,名称${this.car.name},车牌号${this.car.number}`);
    }

    endCar(){
        console.log(`行程结束,金额:${this.car.price * 5}`);
    }
}

//测试
let car = new Zhuanche(100,'帕萨特');
let trip = new Trip(car);
trip.startCar();
trip.endCar();

第二题

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

问题

  • 画出UML类图
  • 用ES6语法写出该示例

解答

  • UML
classDiagram class Camera{ +shot(car):Object } class Park{ +floors:Array<Floor> +camera:Camera +screen:Screen +carList:Object +emptyNum() Number +in(car) +out(car) } class Floor{ +index +place:Array<Place> +emptyPlaceNum() Number } class Place{ + empty:Boolean +in() +out() } class Car{ +number:Number } class Screen{ +show(car,inTime) String } Park --> Camera Park --> Floor Park --> Screen Park --> Car Floor --> Place
  • 示例
/**
 * > 第二题

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

问题

+ 画出UML类图
+ 用ES6语法写出该示例
 */

class Park {
    constructor(floors) {
        this.floors = floors || [];
        this.carmera = new Camera();
        this.screen = new Screen();
        this.carList = {} //存储摄像头拍摄返回的车辆信息
    }

    in(car) {
        //通过摄像头获取信息
        const info = this.carmera.shot(car);
        //停在停车位
        const i = parseInt(Math.random() * 100 % 100);
        const place = this.floors[0].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];
    }

    emptyNum() {
        return this.floors.map(floor => {
            return `${floor.index} 层还有 ${floor.emptyPlaceNum()} 个空余车位`
        }).join('\n');
    }
}

class Floor {
    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);
    }
}

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);

//初始化车辆
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('第二辆车离开');
park.out(car2);

console.log('第三辆车进入');
console.log(park.emptyNum());
park.in(car3);
console.log('第三辆车离开');
park.out(car3);
posted @ 2020-10-30 15:05  mrtransition  阅读(909)  评论(0)    收藏  举报