功能说明:
用户有一张信用卡,信用卡有一个总额度;每个月会有信用卡账单显示月消费总额,月消费总额是小于信用卡总额度的;用户有若干储蓄卡,可选择某张储蓄卡进行还款;还款是指从储蓄卡中划走信用卡的月消费总额到信用卡;如果储蓄卡余额不足则还款动作不成功。
设计流程:
创建一个信用卡类和储蓄卡类;
设定还款事件,查询今天是否是还款日。
当今天是还款日,则所有委托了这个事件的用户会向银行进行还款操作;
关键源码展示:
namespace ConsoleApplication3delegate
{
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);
}
}
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);}
}
class CreditCardDelegate
{
public int billamount;
public int repaymentday;
public delegate void DelegatePay(int bill, int day);
public event DelegatePay EventPay;
public void Notify()
{
Console.WriteLine("今天是本月{0}号", DateTime.Today.Day);
if (DateTime.Today.Day == repaymentday)
{
EventPay(billamount, repaymentday);
Console.WriteLine("还款成功");
}
else Console.WriteLine("未到还款日");
}
}
class Program
{
static void Main(string[] args)
{
DepositCard depositCard = new DepositCard();
depositCard.amount = 10000;
CreditCard creditCard1 = new CreditCard(-2000,9);
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.EventPay += new CreditCardDelegate.DelegatePay(depositCard.Account);
delegate1.Notify();
depositCard.Display();
Console.WriteLine("");
}
Console.ReadLine();
}
}
}