一.功能说明
信用卡还款是银行系统的重要业务,业务流程说明如下:
1、用户有信用卡和储蓄卡,储蓄卡有查询余额和取款功能,信用卡能够查看账单金额、查看还款日和查看余额三个功能;
2、请使用委托与事件实现下列功能:用户可自由设置信用卡还款关联的储蓄卡,当还款日到期时进行账单金额的自动划扣;
3、事件主要是设计什么时候启动委托,委托的主要目的是设置还款的储蓄卡。
二.设计流程+关键源码展示
1.储蓄卡功能实现
class DepositCard
{
public int amount;
public void Display()//查看余额
{
Console.WriteLine("储蓄卡余额为:{0}", amount);
}
public void Account(int balance, int payday)//取款
{
amount += balance;
Console.WriteLine("今天是本月的:{0}日,取款:{1},储蓄卡余额为:{2}。", DateTime.Today.Day, balance, amount);
}
}
2.信用卡功能的实现
class CreditCard
{
private int billamount;
private int repaymentday;
public CreditCard(int billamount, int repaymentday)
{
this.billamount = billamount;
this.repaymentday = repaymentday;
}
public int getbillamount() { return billamount; }//查看账单金额
public int getrepaymentday() { return repaymentday; }//查看还款日期
public void Display() { Console.WriteLine("信用卡余额为:{0}", billamount); }//查看余额
}
3.还款功能实现
class CreditCardDelegate
{
public int billamount;
public int repaymentday;
public delegate void MeDelegate(int bill,int day);//定义委托
public event MeDelegate NotifyEvent;//定义事件
public void Notify()
{
if (DateTime.Today.Day == repaymentday)
{
NotifyEvent(billamount, repaymentday);
Console.WriteLine("还款成功!");
}
else Console.WriteLine("未到还款日,无需还款!");
}
}
4.主函数
class Program
{
static void Main(string[] args)
{
DepositCard depositCard = new DepositCard();
depositCard.amount = 10000;
CreditCard creditCard1 = new CreditCard(-2000, 5);//账单金额和还款日期
CreditCard creditCard2 = new CreditCard(-3000, 13);
CreditCard creditCard3 = new CreditCard(-5000, 29);
depositCard.Display();
Console.WriteLine("");
List<CreditCard> Cards = new List<CreditCard>();
Cards.Add(creditCard1);
Cards.Add(creditCard2);
Cards.Add(creditCard3);
foreach (CreditCard card in Cards)
{
Console.WriteLine("信用卡开始执行委托还款:");
CreditCardDelegate delegate1 = new CreditCardDelegate();
delegate1.billamount = card.getbillamount();
delegate1.repaymentday = card.getrepaymentday();
delegate1.NotifyEvent += new CreditCardDelegate.MeDelegate(depositCard.Account);
delegate1.Notify();
depositCard.Display();
Console.WriteLine("");
}
Console.ReadLine();
}
}
三.结果测试
四.总结
此次作业是关于委托和事件的实操,通过代码实现了相关功能,对二者有了点简单的认识:委托可将方法作为参数,为事件而生,事件是对信息进行处理,委托的的函数需要相应的信息来触发,即委托是事件响应方法的提供者。