《几道面试题,大家看看》——题1解
题目
需求如下:
1, 购物平台有几个非常重要的概念。商品,卖家,买家。
2, 卖家可以修改自己某个商品的价格,这个价格变动简称为一条商品消息。
3, 买家有订阅商品消息的功能,一旦这个商品的价格发生变动,会立刻通知给买家。
4, 当然,买家也有取消某个商品消息订阅的功能。这样以后商品价格变动了,就不会通知到买家了。
要求:
请c#代码实现上面的需求:
1, 仅仅实现上面提到的逻辑就好。忽略其他逻辑;相关类的方法和字段也可以尽量简化。比如商品就可以仅仅有一个商品名和价格字段。
2, 本题目考察面向对象设计思想,请注意设计好类。可以在代码之前附带上自己的设计思想。
3, 本程序要可以运行。界面上不做要求,可以是一个简单的console,能说明问题就好。
4, 请注意良好的代码风格和命名规则。
实现
Commodity 类为商品类,代码清单1-1
Buyer 类为订阅者类 ,代码清单1-2
使用方法,代码清单1-3
代码清单1-1 Commodity.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Shop
{
public delegate void ChangePriceEventHandler(string Name,long Price);
public class Commodity
{
private long _price;
private string _name;
public string Name
{
get { return _name; }
set { _name = value;}
}
public long Price
{
get { return _price; }
set
{
_price = value;
OnChangePrice();
}
}
public event ChangePriceEventHandler ChangePrice;
public Commodity(string name,long price)
{
_name = name;
_price = price;
}
private void OnChangePrice()
{
ChangePriceEventHandler temp = ChangePrice;
if (temp != null)
{
temp(_name,_price);
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Shop
{
public delegate void ChangePriceEventHandler(string Name,long Price);
public class Commodity
{
private long _price;
private string _name;
public string Name
{
get { return _name; }
set { _name = value;}
}
public long Price
{
get { return _price; }
set
{
_price = value;
OnChangePrice();
}
}
public event ChangePriceEventHandler ChangePrice;
public Commodity(string name,long price)
{
_name = name;
_price = price;
}
private void OnChangePrice()
{
ChangePriceEventHandler temp = ChangePrice;
if (temp != null)
{
temp(_name,_price);
}
}
}
}
代码清单1-2 Buyer.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Shop
{
public class Buyer
{
private int _id;
public int ID
{
get { return _id; }
set { _id = value; }
}
public Buyer(int Id)
{
_id = Id;
}
/// <summary>
/// 订阅商品
/// </summary>
/// <param name="aCommodity">商品</param>
/// <param name="aChangePrice">商品价格改变的回调方法</param>
public void SubscribeCommodity(Commodity aCommodity,ChangePriceEventHandler aChangePrice)
{
aCommodity.ChangePrice += aChangePrice;
}
/// <summary>
/// 取消订阅商品
/// </summary>
/// <param name="aCommodity">商品</param>
/// <param name="aChangePrice">商品价格改变的回调方法</param>
public void CancelSubscribeCommodity(Commodity aCommodity, ChangePriceEventHandler aChangePrice)
{
aCommodity.ChangePrice -= aChangePrice;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Shop
{
public class Buyer
{
private int _id;
public int ID
{
get { return _id; }
set { _id = value; }
}
public Buyer(int Id)
{
_id = Id;
}
/// <summary>
/// 订阅商品
/// </summary>
/// <param name="aCommodity">商品</param>
/// <param name="aChangePrice">商品价格改变的回调方法</param>
public void SubscribeCommodity(Commodity aCommodity,ChangePriceEventHandler aChangePrice)
{
aCommodity.ChangePrice += aChangePrice;
}
/// <summary>
/// 取消订阅商品
/// </summary>
/// <param name="aCommodity">商品</param>
/// <param name="aChangePrice">商品价格改变的回调方法</param>
public void CancelSubscribeCommodity(Commodity aCommodity, ChangePriceEventHandler aChangePrice)
{
aCommodity.ChangePrice -= aChangePrice;
}
}
}
代码清单1-3 Program.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Shop
{
class Program
{
static void Main(string[] args)
{
Commodity aCommodity1 = new Commodity("中华牙膏", 12);
Commodity aCommodity2 = new Commodity("李宁运动服",300);
Console.WriteLine(string.Format("当前中华牙膏的价格:{0}",aCommodity1.Price.ToString()));
Console.WriteLine(string.Format("当前李宁运动服的价格:{0}", aCommodity2.Price.ToString()));
Console.WriteLine();
Buyer aBuyer1 = new Buyer(1);
Buyer aBuyer2 = new Buyer(2);
aBuyer1.SubscribeCommodity(aCommodity1, new ChangePriceEventHandler(DealChangePrice1));
Console.WriteLine("aBuyer1 订阅了 中华牙膏 ");
aBuyer1.SubscribeCommodity(aCommodity2, new ChangePriceEventHandler(DealChangePrice1));
Console.WriteLine("aBuyer1 订阅了 李宁运动服 ");
aBuyer2.SubscribeCommodity(aCommodity1, new ChangePriceEventHandler(DealChangePrice2));
Console.WriteLine("aBuyer2 订阅了 中华牙膏 ");
Console.WriteLine();
aCommodity1.Price = 13;
aCommodity2.Price = 400;
aBuyer1.CancelSubscribeCommodity(aCommodity1, new ChangePriceEventHandler(DealChangePrice1));
Console.WriteLine();
Console.WriteLine("aBuyer1 取消订阅了 中华牙膏 ");
Console.WriteLine();
aCommodity1.Price = 15;
Console.WriteLine();
Console.WriteLine("Press Enter to Exit");
Console.ReadLine();
}
static void DealChangePrice1(string Name,long Price)
{
Console.WriteLine(string.Format("aBuyer1 获取 {0} 的最新价格为:{1}",Name,Price.ToString()));
}
static void DealChangePrice2(string Name, long Price)
{
Console.WriteLine(string.Format("aBuyer2 获取 {0} 的最新价格为:{1}", Name,Price.ToString()));
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Shop
{
class Program
{
static void Main(string[] args)
{
Commodity aCommodity1 = new Commodity("中华牙膏", 12);
Commodity aCommodity2 = new Commodity("李宁运动服",300);
Console.WriteLine(string.Format("当前中华牙膏的价格:{0}",aCommodity1.Price.ToString()));
Console.WriteLine(string.Format("当前李宁运动服的价格:{0}", aCommodity2.Price.ToString()));
Console.WriteLine();
Buyer aBuyer1 = new Buyer(1);
Buyer aBuyer2 = new Buyer(2);
aBuyer1.SubscribeCommodity(aCommodity1, new ChangePriceEventHandler(DealChangePrice1));
Console.WriteLine("aBuyer1 订阅了 中华牙膏 ");
aBuyer1.SubscribeCommodity(aCommodity2, new ChangePriceEventHandler(DealChangePrice1));
Console.WriteLine("aBuyer1 订阅了 李宁运动服 ");
aBuyer2.SubscribeCommodity(aCommodity1, new ChangePriceEventHandler(DealChangePrice2));
Console.WriteLine("aBuyer2 订阅了 中华牙膏 ");
Console.WriteLine();
aCommodity1.Price = 13;
aCommodity2.Price = 400;
aBuyer1.CancelSubscribeCommodity(aCommodity1, new ChangePriceEventHandler(DealChangePrice1));
Console.WriteLine();
Console.WriteLine("aBuyer1 取消订阅了 中华牙膏 ");
Console.WriteLine();
aCommodity1.Price = 15;
Console.WriteLine();
Console.WriteLine("Press Enter to Exit");
Console.ReadLine();
}
static void DealChangePrice1(string Name,long Price)
{
Console.WriteLine(string.Format("aBuyer1 获取 {0} 的最新价格为:{1}",Name,Price.ToString()));
}
static void DealChangePrice2(string Name, long Price)
{
Console.WriteLine(string.Format("aBuyer2 获取 {0} 的最新价格为:{1}", Name,Price.ToString()));
}
}
}
执行
具体的执行效果见下图1-1
图1-1 代码的执行的效果
End.