用面向对象模拟 顾客到餐厅就餐的过程. 顾客类 服务员 厨师
答案:2 悬赏:70
解决时间 2021-03-06 03:29
- 提问者网友:践踏俘获
- 2021-03-05 17:30
用c#编写
最佳答案
- 二级知识专家网友:嗷呜我不好爱
- 2021-03-05 18:15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
Customer xiaoZhang = new Customer("小张");
Waiter waiter = new Waiter(xiaoZhang);
Cook cook = new Cook(waiter);
xiaoZhang.点菜("红烧大排");
xiaoZhang.点菜("番茄炒蛋");
xiaoZhang.点菜("鱼香肉丝");
xiaoZhang.点完();
Console.ReadKey();
}
public class Customer
{
public string Name { get; set; }
private Waiter waiter;
public Customer(string name)
{
this.Name = name;
this.waiter = new Waiter(this);
}
public void 点菜(string dishName)
{
Console.WriteLine(this.Name + "点了:" + dishName);
this.waiter.记录(dishName);
}
public void 点完()
{
Console.WriteLine(this.Name + "点完菜了");
this.waiter.通知厨师();
}
public void 吃()
{
Console.WriteLine(this.Name + "开吃了!!!");
}
}
public class Waiter
{
public List Dishes = new List();
private Cook cook;
public Customer Customer { get; set; }
public Waiter(Customer customer)
{
this.Customer = customer;
this.cook = new Cook(this);
}
public void 记录(string dishName)
{
this.Dishes.Add(dishName);
Console.WriteLine("服务员记下了" + this.Customer.Name + "点的:" + dishName);
}
public void 通知厨师()
{
Console.WriteLine("服务员通知厨师开始为" + this.Customer.Name + "做菜。");
this.cook.做菜();
}
public void 上菜()
{
Console.WriteLine("服务员开始上菜");
this.Customer.吃();
}
}
public class Cook
{
private Waiter waiter;
public Cook(Waiter waiter)
{
this.waiter = waiter;
}
public void 做菜()
{
foreach (string dish in waiter.Dishes)
{
Console.WriteLine("厨师正在为" + this.waiter.Customer.Name + "做:" + dish);
}
this.做完了();
}
private void 做完了()
{
Console.WriteLine("厨师把" + this.waiter.Customer.Name + "的菜做好了。");
this.waiter.上菜();
}
}
}
} ---------------------------------------------------------------------
下面的是用命令模式+简单工厂做的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 命令模式
{
class Program
{
static void Main(string[] args)
{
Cook cook = new Cook();
Custom xiaoZhang = new Custom("小张");
Command 点炒菜 = xiaoZhang.点单("点炒菜");
Command 点饭 = xiaoZhang.点单("点饭");
Waiter waiter = new Waiter();
waiter.SetOrder(点炒菜);
waiter.SetOrder(点饭);
waiter.Notify();
Console.ReadKey();
}
}
public class Cook
{
public void 炒菜()
{
Console.WriteLine("开始炒菜");
}
public void 做饭()
{
Console.WriteLine("开始做饭");
}
}
public class Custom
{
public string Name { get; set; }
public Custom(string name)
{
this.Name = name;
}
public Command 点单(string commandString)
{
return CommandFactory.CreateCommand(commandString);
}
}
public class CommandFactory
{
public static Command CreateCommand(string commandString)
{
Cook cook = new Cook();
if (commandString == "点炒菜")
{
return new 炒菜Command(cook);
}
else if (commandString == "点饭")
{
return new 做饭Command(cook);
}
else
{
throw new Exception("没这个菜");
}
}
}
public abstract class Command
{
protected Cook cook;
public Command(Cook cook)
{
this.cook = cook;
}
public abstract void Execute();
}
public class 炒菜Command : Command
{
public 炒菜Command(Cook cook) : base(cook) { }
public override void Execute()
{
this.cook.炒菜();
}
}
public class 做饭Command : Command
{
public 做饭Command(Cook cook) : base(cook) { }
public override void Execute()
{
this.cook.做饭();
}
}
public class Waiter
{
private List commands = new List();
public void SetOrder(Command command)
{
this.commands.Add(command);
}
public void Notify()
{
foreach (var command in this.commands)
{
command.Execute();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
Customer xiaoZhang = new Customer("小张");
Waiter waiter = new Waiter(xiaoZhang);
Cook cook = new Cook(waiter);
xiaoZhang.点菜("红烧大排");
xiaoZhang.点菜("番茄炒蛋");
xiaoZhang.点菜("鱼香肉丝");
xiaoZhang.点完();
Console.ReadKey();
}
public class Customer
{
public string Name { get; set; }
private Waiter waiter;
public Customer(string name)
{
this.Name = name;
this.waiter = new Waiter(this);
}
public void 点菜(string dishName)
{
Console.WriteLine(this.Name + "点了:" + dishName);
this.waiter.记录(dishName);
}
public void 点完()
{
Console.WriteLine(this.Name + "点完菜了");
this.waiter.通知厨师();
}
public void 吃()
{
Console.WriteLine(this.Name + "开吃了!!!");
}
}
public class Waiter
{
public List
private Cook cook;
public Customer Customer { get; set; }
public Waiter(Customer customer)
{
this.Customer = customer;
this.cook = new Cook(this);
}
public void 记录(string dishName)
{
this.Dishes.Add(dishName);
Console.WriteLine("服务员记下了" + this.Customer.Name + "点的:" + dishName);
}
public void 通知厨师()
{
Console.WriteLine("服务员通知厨师开始为" + this.Customer.Name + "做菜。");
this.cook.做菜();
}
public void 上菜()
{
Console.WriteLine("服务员开始上菜");
this.Customer.吃();
}
}
public class Cook
{
private Waiter waiter;
public Cook(Waiter waiter)
{
this.waiter = waiter;
}
public void 做菜()
{
foreach (string dish in waiter.Dishes)
{
Console.WriteLine("厨师正在为" + this.waiter.Customer.Name + "做:" + dish);
}
this.做完了();
}
private void 做完了()
{
Console.WriteLine("厨师把" + this.waiter.Customer.Name + "的菜做好了。");
this.waiter.上菜();
}
}
}
} ---------------------------------------------------------------------
下面的是用命令模式+简单工厂做的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 命令模式
{
class Program
{
static void Main(string[] args)
{
Cook cook = new Cook();
Custom xiaoZhang = new Custom("小张");
Command 点炒菜 = xiaoZhang.点单("点炒菜");
Command 点饭 = xiaoZhang.点单("点饭");
Waiter waiter = new Waiter();
waiter.SetOrder(点炒菜);
waiter.SetOrder(点饭);
waiter.Notify();
Console.ReadKey();
}
}
public class Cook
{
public void 炒菜()
{
Console.WriteLine("开始炒菜");
}
public void 做饭()
{
Console.WriteLine("开始做饭");
}
}
public class Custom
{
public string Name { get; set; }
public Custom(string name)
{
this.Name = name;
}
public Command 点单(string commandString)
{
return CommandFactory.CreateCommand(commandString);
}
}
public class CommandFactory
{
public static Command CreateCommand(string commandString)
{
Cook cook = new Cook();
if (commandString == "点炒菜")
{
return new 炒菜Command(cook);
}
else if (commandString == "点饭")
{
return new 做饭Command(cook);
}
else
{
throw new Exception("没这个菜");
}
}
}
public abstract class Command
{
protected Cook cook;
public Command(Cook cook)
{
this.cook = cook;
}
public abstract void Execute();
}
public class 炒菜Command : Command
{
public 炒菜Command(Cook cook) : base(cook) { }
public override void Execute()
{
this.cook.炒菜();
}
}
public class 做饭Command : Command
{
public 做饭Command(Cook cook) : base(cook) { }
public override void Execute()
{
this.cook.做饭();
}
}
public class Waiter
{
private List
public void SetOrder(Command command)
{
this.commands.Add(command);
}
public void Notify()
{
foreach (var command in this.commands)
{
command.Execute();
}
}
}
}
全部回答
- 1楼网友:不傲怎称霸
- 2021-03-05 19:35
同问。。。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯