/ 从设计到模式
  // 设计模式简介
  // 设计
  // 模式
  // 分开
  // 从设计到模式
    // 23种设计模式
      // 创建型
        // 工厂模式(工厂方法模式,抽象工厂模式,建造者模式)
        // 单例模式
        // 原型模式
      // 组合型

      // 行为型
        // 策略模式
        // 模板方法模式
        // 观察者模式
        // 迭代器模式
        // 职责连模式
        // 命令模式
        // 备忘录模式
        // 状态模式
        // 访问者模式
        // 中介者模式
        // 解释器模式
      //结构型
        // 适配器模式
        // 装饰器模式
        // 代理模式
        // 外观模式
        // 桥接模式
        // 组合模式
        // 享元模式

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

// 画出UML类型
// 用ES6语法写出该实例

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

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

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

// // 行程
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 Kuaiche(100,"北京现代")
// let trip = new Trip(car)
// trip.start()
// trip.end()

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

// 车
class Car{
  constructor(num){
    this.num = num
  }
}
// 停车场
class Park{
  constructor(floors){
    this.floors = floors || []
    this.camera = new Camera()
    this.screen = new Screen()
    this.cartList = {} //存储
  }
  in(car){
//
    const info = this.camera.shot(car)
    const i = parseInt(Math.random()*100%100)
    const place = this.floors[0].places[i]
    place.in()
    info.place = place
    this.cartList[car.num] = info
    console.log(this.cartList)
  }
  out(car){
    const info = this.cartList[car.num]
    // 讲停车位清空
    const place = info.place
    place.out()
    // 显示时间
    this.screen.show(car,info.inTime)
    // 清空
    delete this.cartList[car.num]
  }
  emtpyNum(){
    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)
  }
}

//初始化停车场
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)

park.in(car1)
console.log(park.emtpyNum())
park.in(car2)
console.log(park.emtpyNum())

park.out(car1)
console.log(park.emtpyNum())
park.out(car2)
console.log(park.emtpyNum())

park.in(car3)
console.log(park.emtpyNum())
park.out(car3)
console.log(park.emtpyNum())

// 1 层还有99个车位
// 2 层还有100个车位
// 3 层还有100个车位

// 1 层还有98个车位
// 2 层还有100个车位
// 3 层还有100个车位

// 车牌号 100
// 停车时间 17

// 1 层还有99个车位
// 2 层还有100个车位
// 3 层还有100个车位

// 车牌号 200
// 停车时间 3
// 1 层还有100个车位
// 2 层还有100个车位
// 3 层还有100个车位

// 1 层还有99个车位
// 2 层还有100个车位
// 3 层还有100个车位

// 车牌号 300
// 停车时间 5
// 1 层还有100个车位
// 2 层还有100个车位
// 3 层还有100个车位

最新文章

  1. 操作各个版本的excel的链接写法
  2. jQuery实现瀑布流
  3. 常用CSS优化总结——网络性能与语法性能建议
  4. atittit.表单验证的实现方式以及原理本质以及选型以及自定义兼容easyui dsl规则的表单验证
  5. MySQL批量SQL插入性能优化
  6. kmp 和boyer-moore
  7. 关于STM32库中 __IO 修饰符(volatile修饰符,反复无常的意思)
  8. 支持多种浏览器的纯css下拉菜单
  9. loadrunner_analysis技巧_filter和group by
  10. 三个QT咨询公司以及QT5.0的主要特点
  11. TCP/IP详解之:SNMP
  12. JQuery基础知识(1)
  13. 07 总结ProgressDialog 异步任务
  14. Spring 学习笔记---Bean的生命周期
  15. 教程:关于如何通过Maven仓库安装Spire系列的 Java产品
  16. android studio 虚拟机adb.exe已停止工作的处理
  17. C# 计算地图上某个坐标点的到多边形各边的距离
  18. js 30Dom应用
  19. HTML入门5
  20. Rest和Restful &amp; http

热门文章

  1. CF1095E Almost Regular Bracket Sequence
  2. 在Apache Struts中利用OGNL注入
  3. 基于FATFS的磁盘分布
  4. 基于tiny4412的Linux内核移植 -- 设备树的展开【转】
  5. vim块编辑删除、插入、替换【转】
  6. Webpack2 中的 NamedModulesPlugin 与 HashedModuleIdsPlugin
  7. 拿什么守护你的Node.JS进程: Node出错崩溃了怎么办?
  8. flask(1)
  9. C#实现邮件发送的功能
  10. 网站被k