应用场景:
用户有一张信用卡,信用卡有一个总额度;
每个月会有信用卡账单显示月消费总额(月消费总额是小于信用卡总额度的);
用户有若干储蓄卡,可选择某张储蓄卡进行还款;
还款是指从储蓄卡中划走信用卡的月消费总额到信用卡;
如果储蓄卡余额不足则还款动作不成功。
要求:①必须使用委托②事件的触发方式是每个月的到期还款日;
代码:
信用卡类:
首先,信用卡类有一个总额度,因为在每个月会有信用卡账单显示月消费总额,于是我们还要写一个账单的成员函数。
class Xinyong
{
private float sum;
private float xiaofei;
public Xinyong(float a,float b)
{
sum = a;
xiaofei = b;
}
public void Zhangdan()
{
Console.WriteLine("--本月消费账单--");
Console.WriteLine("本月消费:");
Console.WriteLine(xiaofei);
Console.WriteLine("----------------");
}
}
储蓄卡类:
用户通过储蓄卡进行还款。
class Chuxu
{
private float sum;
private float xiaofei;
public Chuxu(float a,float b)
{
sum = a;
xiaofei=b;
}
public void Huankuan()
{
if(sum<xiaofei )
{
Console.WriteLine("还款失败,请使用其他储蓄卡");
}
else
{
sum -= xiaofei;
Console.WriteLine("还款成功!");
Console.WriteLine("储蓄卡余额:");
Console.WriteLine(sum);
}
}
}
委托类:
还款的触发条件是“已到本月还款日期”。
class Delegate
{
// 定义委托
public delegate void MeDelegate();
// 定义事件
public event MeDelegate myevent;
public void Notify()
{
// 如果事件不为 null
if (myevent!= null)
{
Console.WriteLine("#已到本月还款日期#");
// 触发事件
myevent();
}
}
}
主函数:
在主函数里面,我们创建多个储蓄卡类的对象,以实现选择想要的储蓄卡进行还款。同时把还款添加到事件中,使得在“已到本月还款日期”的条件下进行还款。
class Test
{
static void Main(string[] args)
{
float xiaofei;
string a,b;
// 委托的对象
Delegate obj1 = new Delegate();
Console.WriteLine("请输入消费:");
a=Console.ReadLine();
xiaofei=float.Parse(a);
Xinyong obj2 = new Xinyong(1000,xiaofei);
Chuxu obj3 = new Chuxu(1000, xiaofei);
Chuxu obj4 = new Chuxu(500, xiaofei);
obj1.myevent += new Delegate.MeDelegate(obj2.Zhangdan);
Console.WriteLine("请选择储蓄卡1还是储蓄卡2:");
b = Console.ReadLine();
if(b=="1")
{
obj1.myevent += new Delegate.MeDelegate(obj3.Huankuan);
}
else if(b=="2")
{
obj1.myevent += new Delegate.MeDelegate(obj4.Huankuan);
}
else
{
Console.WriteLine("储蓄卡输入错误!");
}
obj1.Notify();
}
}
全部代码:
using System;
namespace app
{
class Xinyong
{
private float sum;
private float xiaofei;
public Xinyong(float a,float b)
{
sum = a;
xiaofei = b;
}
public void Zhangdan()
{
Console.WriteLine("--本月消费账单--");
Console.WriteLine("本月消费:");
Console.WriteLine(xiaofei);
Console.WriteLine("----------------");
}
}
// 第二个类
class Chuxu
{
private float sum;
private float xiaofei;
public Chuxu(float a,float b)
{
sum = a;
xiaofei=b;
}
public void Huankuan()
{
if(sum<xiaofei )
{
Console.WriteLine("还款失败,请使用其他储蓄卡");
}
else
{
sum -= xiaofei;
Console.WriteLine("还款成功!");
Console.WriteLine("储蓄卡余额:");
Console.WriteLine(sum);
}
}
}
class Delegate
{
// 定义委托
public delegate void MeDelegate();
// 定义事件
public event MeDelegate myevent;
public void Notify()
{
// 如果事件不为 null
if (myevent!= null)
{
Console.WriteLine("#已到本月还款日期#");
// 触发事件
myevent();
}
}
}
class Test
{
static void Main(string[] args)
{
float xiaofei;
string a,b;
// 委托的对象
Delegate obj1 = new Delegate();
Console.WriteLine("请输入消费:");
a=Console.ReadLine();
xiaofei=float.Parse(a);
Xinyong obj2 = new Xinyong(1000,xiaofei);
Chuxu obj3 = new Chuxu(1000, xiaofei);
Chuxu obj4 = new Chuxu(500, xiaofei);
obj1.myevent += new Delegate.MeDelegate(obj2.Zhangdan);
Console.WriteLine("请选择储蓄卡1还是储蓄卡2:");
b = Console.ReadLine();
if(b=="1")
{
obj1.myevent += new Delegate.MeDelegate(obj3.Huankuan);
}
else if(b=="2")
{
obj1.myevent += new Delegate.MeDelegate(obj4.Huankuan);
}
else
{
Console.WriteLine("储蓄卡输入错误!");
}
obj1.Notify();
}
}
}
运行截图:
总结:
对于信用卡用户定时还款,最重要的就是通过使用委托、事件在“已到本月还款日期”的同时,进行储蓄卡的还款。