一,题目
信用卡还款是银行系统的重要业务,业务流程说明如下:
1,用户有信用卡和储蓄卡,储蓄卡有查询余额和取款功能,信用卡能够查看账单金额、查看还款日和查看余额三个功能;
2,请使用委托与事件实现下列功能:用户可自由设置信用卡还款关联的储蓄卡,当还款日到期时进行账单金额的自动划扣;
3,请注意事件主要是设计什么时候启动委托,委托的主要目的是设置还款的储蓄卡
4,为避免设计出现偏差,给出了部分功能代码,请在此基础上进行开发。
二,核心代码
1,CreditCardDelegate类定义委托和事件
代码如下
class CreditCardDelegate
{
public int billamount;
public int repaymentday;
//请在此处添加自己的代码
//定义委托
public delegate void chucuncard(int billamout, int repaymentday);
//定义事件
public event chucuncard Mychucuncard;
public void shiyon()
{
Console.WriteLine("今天是本月的{0}号。", DateTime.Today.Day);
if (repaymentday == DateTime.Today.Day)
{
Mychucuncard(billamount, repaymentday);
Console.WriteLine("已完成还款");
}
else
{
Console.WriteLine("无要求");
}
}
}
2,在主函数中使用委托与事件
foreach (CreditCard card in Cards)
{
depositCard.amount = 10000;
depositCard.Display();
Console.WriteLine("信用卡开始执行委托还款。。。。。。");
//请在此处添加自己的代码
CreditCardDelegate rng = new CreditCardDelegate();
rng.repaymentday = card.getrepaymentday();
rng.billamount = card.getbillamount();
rng.Mychucuncard += new CreditCardDelegate.chucuncard(depositCard.Account);
rng.shiyon();
depositCard.Display();
Console.WriteLine("\n");
}
3,整体代码
using System;
using System.Collections.Generic;
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 chucuncard(int billamout, int repaymentday);
//定义事件
public event chucuncard Mychucuncard;
public void shiyon()
{
Console.WriteLine("今天是本月的{0}号。", DateTime.Today.Day);
if (repaymentday == DateTime.Today.Day)
{
Mychucuncard(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, 5);
CreditCard creditCard2 = new CreditCard(-3000, 13);
CreditCard creditCard3 = new CreditCard(-5000, 29);
Console.WriteLine("");
List<CreditCard> Cards = new List<CreditCard>();
Cards.Add(creditCard1);
Cards.Add(creditCard2);
Cards.Add(creditCard3);
foreach (CreditCard card in Cards)
{
depositCard.amount = 10000;
depositCard.Display();
Console.WriteLine("信用卡开始执行委托还款。。。。。。");
//请在此处添加自己的代码
CreditCardDelegate rng = new CreditCardDelegate();
rng.repaymentday = card.getrepaymentday();
rng.billamount = card.getbillamount();
rng.Mychucuncard += new CreditCardDelegate.chucuncard(depositCard.Account);
rng.shiyon();
depositCard.Display();
Console.WriteLine("\n");
}
Console.ReadLine();
}
}
}
4,结果截图
![](https://i-blog.csdnimg.cn/blog_migrate/7fbee883f3aa77149d5133f7dc481d45.png)
三,总结
通过这次实验,大致了解了委托与事件的大概使用方法,也了解了c#的基本用法,既巩固了以往的知识,也了解了新的内容。
四,源码地址