本题的应用场景解释:用户有一张信用卡,信用卡有一个总额度;每个月会有信用卡账单显示月消费总额,月消费总额是小于信用卡总额度的;用户有若干储蓄卡,可选择某张储蓄卡进行还款;还款是指从储蓄卡中划走信用卡的月消费总额到信用卡;如果储蓄卡余额不足则还款动作不成功。
要求如下:①必须使用委托②事件的触发方式是每个月的到期还款日;
using System;
namespace windows_practice
{
//信用卡类
class CreditCard
{
private int CMoney; //月消费额度
public int TodayDate; //还款日期
public DepositCard DC;
public CreditCard(int Money, int Today, DepositCard DC)
{
this.CMoney = Money;
this.TodayDate = Today;
this.DC = DC;
}
public int GetCMoney()
{
return CMoney;
}
public void Check()
{
if (TodayDate == 30)
{
Console.WriteLine("已经是本月30号,本月需要还款金额为:{0}", CMoney);
if (DC.GetDMoney() >= CMoney)
{
Console.WriteLine("储蓄卡金额为:{0},已还款!", DC.GetDMoney());
}
else
{
Console.WriteLine("储蓄卡金额为:{0},余额不足无法还款!", DC.GetDMoney());
}
}
else
{
Console.WriteLine("还未到30号,请{0}天后还款!", 30 - TodayDate);
}
}
}
//储蓄卡类
class DepositCard
{
private int DMpney;
public DepositCard(int Money)
{
this.DMpney = Money;
}
public int GetDMoney()
{
return this.DMpney;
}
}
class Delegate
{
public delegate void MyDelegate();
public event MyDelegate Repay;
public void Notify()
{
if (Repay != null)
{
Repay();
}
}
}
class Program
{
static void Main(string[] args)
{
Delegate objd = new Delegate();
DepositCard d1 = new DepositCard(3000);
DepositCard d2 = new DepositCard(3000);
CreditCard c1 = new CreditCard(3000, 30, d1);
CreditCard c2 = new CreditCard(3000, 22, d1);
CreditCard c3 = new CreditCard(5000, 30, d2);
objd.Repay += new Delegate.MyDelegate(c1.Check);
objd.Repay += new Delegate.MyDelegate(c2.Check);
objd.Repay += new Delegate.MyDelegate(c3.Check);
objd.Notify();
}
}
}
运行结果:
源代码: