功能说明:
信用卡还款是银行系统的重要业务,业务流程说明如下:
用户有信用卡和储蓄卡,储蓄卡有查询余额和取款功能,信用卡能够查看账单金额、查看还款日和查看余额三个功能;
请使用委托与事件实现下列功能:用户可自由设置信用卡还款关联的储蓄卡,当还款日到期时进行账单金额的自动划扣;
请注意事件主要是设计什么时候启动委托,委托的主要目的是设置还款的储蓄卡
为避免设计出现偏差,给出了部分功能代码,请在此基础上进行开发。
设计流程:
先判断是否到达还款日期,再选择相应储蓄卡进行还款,同时还要判断储蓄卡余额是否足以还款,若不够还款则需要报错提示用户换卡进行还款。
关键源码:
储蓄卡类:
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//信用卡
{
public int billamount;//账单
public int repaymentday;//还款日期
public int day = 1;
public CreditCard(int billamount, int repaymentday)
{
this.billamount = billamount;
this.repaymentday = repaymentday;
}
public int getbillamount() //检查欠款金额
{
return billamount;
}
public int getrepaymentday() //检查还款日期
{
return repaymentday;
}
public int Repay()//检查是否还款
{
if (day == repaymentday)
return 1;
else
return 0;
}
public void Display()
{
Console.WriteLine("信用卡余额为:{0}", billamount);
}
public void havePayed()
{
billamount = 0;
}
}
委托类:
class CreditCardDelegate
{
public int billamount;//账单
public int repaymentday;//还款日期
public CreditCardDelegate(int billamount,int repaymentday)
{
this.billamount = billamount;
this.repaymentday = repaymentday;
}
public delegate void cardDelegate(int billmount, int repaymentday);
public event cardDelegate notifyRepayment = delegate { };
public void Notify()
{
if(notifyRepayment != null)
{
notifyRepayment(billamount, repaymentday);
}
}
}
主函数:
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);
if(DateTime.Today.Day == CreditCard.repaymentday)
{
if(CreditCard.Repay() == 1)
{
amount = amount - CreditCard.getbillamount();
if (amount< 0)
Console.WriteLine("还款失败,余额为:{0}", amount);
else
Console.WriteLine("还款成功,余额为{0}", amount);
}
else
{
Console.WriteLine("不需要还款,余额为{0}", amount);
}
}
foreach (CreditCard card in Cards)
{
CreditCardDelegate creditCardDelegate = new CreditCardDelegate(card.getbillamount(), card.getrepaymentday());
Console.WriteLine("信用卡开始执行委托还款。。。。。。");
Console.WriteLine("");
if(DateTime.Today.Day == card.getrepaymentday())
{
Console.WriteLine("请按‘1’或‘2’或‘3’在三张信用卡中选择一张进行还款", card.getbillamount());
try
{
int number = Convert.ToInt32(Console.ReadLine());
if(number == 1)
{
if(depositCard.amount + card.getbillamout() >= 0)
{
creditCardDelegate.notifyRepayment += new CreditCardDelegate.cardDelegate(depositCard.Account);
creditCardDelegate.Notify();
card.havePayed();
Console.WriteLine("还款成功");
}
else
{
Console.WriteLine("余额不足");
}
}
}
}
else
{
Console.WriteLine("不需要还款");
card.Display();
}
}
Console.ReadLine();
}
总结:
本次实验还有部分功能没有完善,没有熟练掌握委托和事件的关系,不能理清代码的逻辑实现具体细节。