一、功能说明
信用卡还款是银行系统的重要业务,用户有信用卡和储蓄卡,储蓄卡有查询余额和取款功能,信用卡能够查看账单金额、查看还款日和查看余额三个功能;用户可自由设置信用卡还款关联的储蓄卡,当还款日到期时进行账单金额的自动划扣。
二、设计流程
创建信用卡和储蓄卡。
信用卡:查余额和取款 储蓄卡:查余额,还款日期和余额
创建信用卡委托还款事件,然后查看当天日期每天进行查询是否是还款日,如果是还款日则会进行还款,银行会自动从用户的储蓄卡中取款向信用卡进行还款,否则还款日则无需进行还款。
三、关键源码展示
(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 Delegatepayfor(int billamount, int repaymentday);
public event Delegatepayfor Pay;
public void notify()
{
if (DateTime.Today.Day == repaymentday)
{
Pay(billamount, repaymentday);
Console.WriteLine("还款成功。");
}
else
Console.WriteLine("还未到还款日");
}
}
(4)主函数
static void Main(string[] args)
{
DepositCard depositCard = new DepositCard();
depositCard.amount = 10000;
CreditCard creditCard1 = new CreditCard(-2000, 6);
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.Pay += new CreditCardDelegate.Delegatepayfor(depositCard.Account);
delegate1.notify();
depositCard.Display();
Console.WriteLine("");
}
Console.ReadLine();
}
(5)结果显示
四、总结
通过实际的代码对委托和事件可以更好地理解。我知道了委托的定义和类差不多,但是由于它定义了方法,所以可以使方法当作另外一个参数进行传递。但是对于委托更深层次的理解还需要多多练习。
五、源码开源地