信用卡用户定时还款
一、委托类
代码如下:
class Delegates //委托类
{
public delegate void MeDelegate(StorageCard card2); //定义委托
public event MeDelegate Remind; //定义提醒事件
public event MeDelegate Pay; //定义扣款事件
public void RemindNotify(StorageCard card2)
{
if (Remind != null)
{
Console.WriteLine("已到还款日!");
Remind(card2);
}
else
Console.WriteLine("还款日未到。");
}
public void PayNotify(StorageCard card2)
{
if (Pay != null)
{
Console.WriteLine("已到还款日!");
Pay(card2);
}
else
Console.WriteLine("还款日未到。");
}
}
二、储蓄卡类
代码如下:
class StorageCard //储蓄卡
{
public int countnum; //账户号码
public int balance; //账户余额
public int debt; //应还欠款
public int date; //还款日期
public StorageCard()
{
}
public StorageCard(int countnum, int balance, int debt, int date)
{
this.countnum = countnum;
this.balance = balance;
this.debt = debt;
this.date = date;
}
}
三、信用卡类
代码如下:
class CreditCard //信用卡
{
static public void remind(StorageCard card2)
{
Console.WriteLine("还款日要到了!账户为{0}的借款为{1}!", card2.countnum, card2.balance);
}
static public void PayMoney(StorageCard card2)
{
if (card2.debt > card2.balance)
Console.WriteLine("账户为{0}的借款为{1},储蓄卡余额不足!为{2}", card2.countnum, card2.debt, card2.balance);
else
Console.WriteLine("账户为{0}的借款为{1},储蓄卡余额为{2},还款后剩余{3}", card2.countnum, card2.debt, card2.balance, card2.balance-card2.debt);
}
}
四、main函数
代码如下:
static void Main(string[] args)
{
Delegates obj1 = new Delegates();
Delegates obj2 = new Delegates();
CreditCard card1 = new CreditCard();
StorageCard card2 = new StorageCard(0001,10000,800,9);
StorageCard card3 = new StorageCard(0002,8000,1000,6);
obj1.Remind += new Delegates.MeDelegate(CreditCard.remind);
obj2.Pay += new Delegates.MeDelegate(CreditCard.PayMoney);
for(int i = 1; i<=30; i++)
{
if (i == card2.date)
obj2.PayNotify(card2);
if (i == card3.date)
obj2.PayNotify(card3);
}
}
结果截图
总结
在这次的委托实现信用卡定时还款的案例中,我较为熟练的掌握了委托的定义与使用,实现了事件的订阅与发布,在之后对委托的使用会更为熟练。