题目
信用卡还款是银行系统的重要业务,业务流程说明如下:
- 用户有信用卡和储蓄卡,储蓄卡有查询余额和取款功能,信用卡能够查看账单金额、查看还款日和查看余额三个功能;
- 请使用委托与事件实现下列功能:用户可自由设置信用卡还款关联的储蓄卡,当还款日到期时进行账单金额的自动划扣;
- 请注意事件主要是设计什么时候启动委托,委托的主要目的是设置还款的储蓄卡
- 为避免设计出现偏差,给出了部分功能代码,请在此基础上进行开发。
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; 请在此处添加自己的代码 } 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("信用卡开始执行委托还款。。。。。。"); 请在此处添加自己的代码 Console.WriteLine(""); } Console.ReadLine(); } } }
功能说明
创建一个储蓄卡、信用卡
定义CreditCardDelegate的委托类,在其中定义一个委托delegatepay,定义事件eventpay提醒还款,定义函数payday用于触发事件
关键源码展示
第一段:为了实现自动划扣的功能,可以在CreditCardDelegate类中定义一个事件(例如PaymentEvent)来触发自动还款的委托。
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); }
}
第二段:
在Main方法中,可以创建一个CreditCardDelegate对象,并将其关联DepositCard的委托,然后订阅PaymentEvent事件并在事件处理程序中调用MakePayment方法。
foreach (CreditCard card in Cards)
{
Console.WriteLine("信用卡开始执行委托还款");
ccDelegate.billamount = card.getbillamount();
ccDelegate.repaymentday = card.getrepaymentday();
ccDelegate.PaymentEvent += (sender, e) => ccDelegate.MakePayment();
ccDelegate.OnPaymentEvent();
Console.WriteLine("");
}
完整代码展示
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.ToString("dd"), 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 event EventHandler PaymentEvent;
public Action<int, int> PaymentDelegate;
public void SetPaymentDelegate(DepositCard depositCard)
{
PaymentDelegate = depositCard.Account;
}
public void MakePayment()
{
PaymentDelegate(billamount, repaymentday);
}
public void OnPaymentEvent()
{
if (DateTime.Today.Day == repaymentday)
{
PaymentEvent?.Invoke(this, EventArgs.Empty);
}
}
}
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);
CreditCardDelegate ccDelegate = new CreditCardDelegate();
ccDelegate.SetPaymentDelegate(depositCard);
foreach (CreditCard card in Cards)
{
Console.WriteLine("信用卡开始执行委托还款。。。。。。");
ccDelegate.billamount = card.getbillamount();
ccDelegate.repaymentday = card.getrepaymentday();
ccDelegate.PaymentEvent += (sender, e) => ccDelegate.MakePayment();
ccDelegate.OnPaymentEvent();
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
运行结果:
总结
本程序实现了一个简单的信用卡自动还款系统,使用了委托和事件来实现自动划扣的功能。使用委托和事件可以有效地降低程序的耦合度,增强程序的灵活性和可扩展性。