<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>IFE ECMAScript</title>
</head>
<body>
<script>
var id = 0;
function generateID() {
return ++id;
}
function Restaurant(attr) {
this.cash = attr.cash;
this.seats = attr.seats;
this.staff = attr.staff;
}
Restaurant.prototype.hire = function (staff) {
this.staff.push(staff);
}
Restaurant.prototype.fire = function (staff) {
this.staff = this.staff.filter(function (el) {
return el !== staff;
});
}
function Staff(name, salary) {
this.id = generateID();
this.name = name;
this.salary = salary;
}
Staff.prototype.work = function () {
}
var singletonWaiter = (function () {
var waiter = null;
function Waiter(name, salary) {
Staff.call(this, name, salary);
}
Waiter.prototype = {
construstor: Waiter,
notifyCook: function (task) {
if (typeof task == "object") {
console.log("服务员:厨师,做一份" + task.name);
let _ck = singletonCook.getCook("李大厨", 10000);
_ck.cookDish(task.name);
_ck = null;
}
},
serveDish: function (dishes) {
console.log(`服务员:${dishes.dishname}来了!`);
},
}
return {
getWaiter: function (name, salary) {
if (waiter === null) {
waiter = new Waiter(name, salary);
}
return waiter;
}
}
})();
var singletonCook = (function () {
var cook = null;
function Cook(name, salary) {
Staff.call(this, name, salary);
}
Cook.prototype = {
construstor: Cook,
cookDish: function (dishes) {
console.log(`厨师:${dishes}做好了!`);
},
notifyWaiter: function (dishes) {
console.log("可以上菜了!");
let _wt = singletonWaiter.getWaiter("王小二", 3500);
_wt.serveDish(dishes);
_wt = null;
},
}
return {
getCook: function (name, salary) {
if (cook === null) {
cook = new Cook(name, salary);
}
return cook;
}
}
})();
function Customer(name) {
this.name = name;
this.order = function (dishes) {
console.log(name + "点的菜是" + dishes.dishname);
singletonWaiter.getWaiter("王小二", 3500).serveDish(dishes);
}
this.eat = function () {
console.log("顾客:开始吃菜,吃完离开!");
}
}
function Dish(dishname, cost, price) {
this.dishname = dishname;
this.cost = cost;
this.price = price;
}
function getMenu(dishes) {
let menu = new Array();
for (let i = 0; i < dishes.length; i++) {
let obj = {};
obj.dishname = dishes[i].dishname;
obj.price = dishes[i].price;
menu.push(obj);
}
return menu;
}
//test
var ifeRestaurant = new Restaurant({
cash: 1000000,
seats: 1,
staff: [],
});
var newCook = singletonCook.getCook("李大厨", 10000);
var newWaiter = singletonWaiter.getWaiter("王小二", 3500);
ifeRestaurant.hire(newCook);
ifeRestaurant.hire(newWaiter);
var customerQueue = ['赵先生', '钱女士', '孙大姐', '李大哥'];
var ifedishes = [
new Dish("麻辣小龙虾", 30, 88),
new Dish("酸辣土豆丝", 8, 18),
new Dish("肉末茄子", 12, 28),
new Dish("水煮肉片", 30, 58),
];
var menu = getMenu(ifedishes);
console.log(menu);
for (let i = 0, len = customerQueue.length; i < len; i++) {
let cus = new Customer(customerQueue.shift());
let j = Math.floor(Math.random() * menu.length);
cus.order(menu[j]);
cus.eat();
console.log("------------------------------");
if (!customerQueue.length) {
console.log("服务员:没有顾客了,可以休息下了!");
}
}
</script>
</body>
</html>