题目:
信用卡还款是银行系统的重要业务,业务流程说明如下:
- 用户有信用卡和储蓄卡;储蓄卡有查询余额和取款功能,信用卡能够查看账单金额、查看还款日和查看余额三个功能;
- 请使用委托与事件实现下列功能:用户可自由设置信用卡还款关联的储蓄卡,当还款日到期时进行账单金额的自动划扣;
- 请注意事件主要是设计什么时候启动委托,委托的主要目的是设置还款的储蓄卡;
- 为避免设计出现偏差,给出了部分功能代码,请在此基础上进行开发。
设计流程:首先将该程序分为四类,分别为储蓄卡类,信用卡类,委托类,主程序。再分别在各类中设计函数实现其功能。
关键代码:
class DepositCard
{
private string name;
private int depositmoney;
public DepositCard(string name, int depositmoney)
{
this.name = name;
this.depositmoney = depositmoney;
}
public int getDepositMoney()
{
return this.depositmoney;
}
public void setDepositMoney(int money)
{
this.depositmoney = money;
}
}
功能说明:设计储蓄卡类,并设计储蓄卡的查询余额函数和设置余额函数。
class CreditCard
{
public string name;
public int creditmoney;
public int repaymentday;
public DepositCard s;
public CreditCard(string name, int creditmoney, int repaymentday, DepositCard s)
{
this.name = name;
this.creditmoney = creditmoney;
this.repaymentday = repaymentday;
this.s = s;
}
public void repayment()
{
Console.WriteLine("{0}的储蓄卡余额为:{1}", this.name, this.s.getDepositMoney());
s.setDepositMoney(this.s.getDepositMoney() + creditmoney);
Console.WriteLine("已还款");
Console.WriteLine("还款金额:{0}", this.creditmoney);
Console.WriteLine("账户余额:{0}", s.getDepositMoney());
}
public void not()
{
Console.WriteLine("{0}的储蓄余额为:{1}", this.name, this.s.getDepositMoney());
Console.WriteLine("未到还款日期");
}
}
功能说明:设计信用卡类,并设计读取余额、还款金额函数,考虑到有可能用户未到还款日期,再额外设定一个说明未到还款日期功能的函数。
class repaymentDelegate
{
public delegate void repayMent();
public event repayMent Repay;
public void notifRepay()
{
if (Repay != null)
{
Console.WriteLine("触发事件");
Repay();
}
}
}
功能说明:设计委托类
class Program
{
static void Main(string[] args)
{
DepositCard D1 = new DepositCard("x", 6000);
DepositCard D2 = new DepositCard("y", 1000);
DepositCard D3 = new DepositCard("z", 10000);
CreditCard C1 = new CreditCard("x", -1000, 0, D1);
CreditCard C2 = new CreditCard("y", -500, 1, D2);
CreditCard C3 = new CreditCard("z", -8000, 0, D3);
CreditCard[] CreditCards = new CreditCard[3];
CreditCards[0] = C1;
CreditCards[1] = C2;
CreditCards[2] = C3;
repaymentDelegate m = new repaymentDelegate();
foreach (CreditCard C in CreditCards)
{
if (C.repaymentday == 0)
m.Repay += new repaymentDelegate.repayMent(C.repayment);
else
m.Repay += new repaymentDelegate.repayMent(C.not);
}
m.notifRepay();
Console.ReadKey();
}
}
}
功能说明:设计主函数,并对用户进行初始化。
运行结果截图:
完整代码:
namespace ConsoleApplication3delegate
{
class DepositCard
{
private string name;
private int depositmoney;
public DepositCard(string name, int depositmoney)
{
this.name = name;
this.depositmoney = depositmoney;
}
public int getDepositMoney()
{
return this.depositmoney;
}
public void setDepositMoney(int money)
{
this.depositmoney = money;
}
}
class CreditCard
{
public string name;
public int creditmoney;
public int repaymentday;
public DepositCard s;
public CreditCard(string name, int creditmoney, int repaymentday, DepositCard s)
{
this.name = name;
this.creditmoney = creditmoney;
this.repaymentday = repaymentday;
this.s = s;
}
public void repayment()
{
Console.WriteLine("{0}的储蓄卡余额为:{1}", this.name, this.s.getDepositMoney());
s.setDepositMoney(this.s.getDepositMoney() + creditmoney);
Console.WriteLine("已还款");
Console.WriteLine("还款金额:{0}", this.creditmoney);
Console.WriteLine("账户余额:{0}", s.getDepositMoney());
}
public void not()
{
Console.WriteLine("{0}的储蓄余额为:{1}", this.name, this.s.getDepositMoney());
Console.WriteLine("未到还款日期");
}
}
class repaymentDelegate
{
public delegate void repayMent();
public event repayMent Repay;
public void notifRepay()
{
if (Repay != null)
{
Console.WriteLine("触发事件");
Repay();
}
}
}
class Program
{
static void Main(string[] args)
{
DepositCard D1 = new DepositCard("x", 6000);
DepositCard D2 = new DepositCard("y", 1000);
DepositCard D3 = new DepositCard("z", 10000);
CreditCard C1 = new CreditCard("x", -1000, 0, D1);
CreditCard C2 = new CreditCard("y", -500, 1, D2);
CreditCard C3 = new CreditCard("z", -8000, 0, D3);
CreditCard[] CreditCards = new CreditCard[3];
CreditCards[0] = C1;
CreditCards[1] = C2;
CreditCards[2] = C3;
repaymentDelegate m = new repaymentDelegate();
foreach (CreditCard C in CreditCards)
{
if (C.repaymentday == 0)
m.Repay += new repaymentDelegate.repayMent(C.repayment);
else
m.Repay += new repaymentDelegate.repayMent(C.not);
}
m.notifRepay();
Console.ReadKey();
}
}
}
总结:通过此次实验,体会到委托与事件在程序设计中的重要性,对委托与事件的认识有了深一步了解:委托属于一个定义,是和类、接口类似的,通常放在外部。借助本次作业,更好的将委托与事件应用于实践。