使用委托实现信用卡用户定时还款功能
首先我要创造了一个委托类,然后继续创建了信用卡类,储蓄卡类和实现还款的还款类。最后填写main函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Creditcardrepayment
{
class Delegate
{
public delegate void mydelegate(); /*定义委托*/
public event mydelegate NotifyMoney;/*定义事件*/
public void Notify()
{
// 如果事件不为 null
if (NotifyMoney != null)
{
Console.WriteLine("触发还款事件:");
// 触发事件
NotifyMoney();
}
}
}
class CreditCard/*定义信用卡类*/
{
int repaymentdata;
private int availablet = 300000;/*信用卡可用总数*/
private string name;/*名字*/
int cost;/*信用卡花费*/
public CreditCard(string name, int repaymentdata, int cost)
{
this.name = name;
this.repaymentdata = repaymentdata;
this.cost = cost;
}
//属性
public string Name
{
get { return name; }
set { name = value; }
}
public int Repaymentdata
{
get { return repaymentdata; }
set { repaymentdata = value; }
}
public int Cost
{
get { return cost; }
set { cost = value; }
}
}
class CashCard/*定义储蓄卡类*/
{
string name;
int remain_money;/*储蓄卡余额*/
int today;/*时间*/
public CashCard(string name, int remain_money, int today)/*构造*/
{
this.name = name;
this.remain_money = remain_money;
this.today = today;
}
//属性
public string Name
{
get { return name; }
set { name = value; }
}
public int Remain_money
{
get { return remain_money; }
set { remain_money = value; }
}
public int Today
{
get { return today; }
set { today = value; }
}
}
//定义还款类
class PayBackMoney
{
CreditCard creditcard;
CashCard cashcard;
public PayBackMoney(CreditCard creditcard, CashCard cashcard)/*构造*/
{
this.creditcard= creditcard;
this.cashcard = cashcard;
}
public void PayMoney()
{
if (cashcard.Remain_money < creditcard.Cost)
{
creditcard.Cost = creditcard.Cost - cashcard.Remain_money;
Console.WriteLine(" {0}余额不足,还款未成功,请及时还款!当前欠费{1}元,储蓄卡余额为0",creditcard.Name, creditcard.Cost);
}
else
{
cashcard.Remain_money = cashcard.Remain_money - creditcard.Cost;
Console.WriteLine(" {0} 欠款已还清,目前储蓄卡余额为{1}元", cashcard.Name, cashcard.Remain_money);
}
}
}
class Program
{
[STAThread]
static void Main(string[] args)
{
CreditCard a1 = new CreditCard("张三", 30, 30000);
CashCard a = new CashCard("张三", 50000, 30);
CreditCard b1 = new CreditCard("张四", 30, 30000);
CashCard b = new CashCard("张四", 20000, 30);
PayBackMoney t1 = new PayBackMoney(a1,a);
PayBackMoney t2 = new PayBackMoney(b1, b);
Delegate objDelegate1 = new Delegate();
Delegate objDelegate2 = new Delegate();
objDelegate1.NotifyMoney += new Delegate.mydelegate(t1.PayMoney);/*事件订阅对象*/
objDelegate2.NotifyMoney += new Delegate.mydelegate(t2.PayMoney);
if (a1.Repaymentdata == a.Today)
objDelegate1.Notify();
objDelegate2.Notify();
Console.ReadLine();
}
}
}
>
里插入代码片
这在实验中我首先创建了一个委托类来定义委托,定义了委托名和对象名
然后定义了存储卡类和信用卡类,用来存放个人相关信息,来输入每个人不同的信用卡花费情况和存储卡存储情况。通过构造函数和一些属性可以很容易的输入相关数据。并方便后续调用。
之后运用一个还款类来实现还款的有关操作,在其中尤其注意要使用`
CreditCard creditcard;
CashCard cashcard;
> 来调用信用卡类和储蓄卡类的有关方法和内容。如果不使用这一方法不好实现和还款类的互通。
>
定义一个方法函数来实现有关还款的操作。
public void PayMoney()
{
if (cashcard.Remain_money < creditcard.Cost)
{
creditcard.Cost = creditcard.Cost - cashcard.Remain_money;
Console.WriteLine(" {0}余额不足,还款未成功,请及时还款!当前欠费{1}元,储蓄卡余额为0",creditcard.Name, creditcard.Cost);
}
else
{
cashcard.Remain_money = cashcard.Remain_money - creditcard.Cost;
Console.WriteLine(" {0} 欠款已还清,目前储蓄卡余额为{1}元", cashcard.Name, cashcard.Remain_money);
}
在main函数中使用
objDelegate1.NotifyMoney += new Delegate.mydelegate(t1.PayMoney);
来实现事件调用对象。
这是我生成的结果
因为第一次写可能还不是很完美欢迎各位大佬提出修改意见
gitee的地址
(https://gitee.com/zhang-tian_yu/study.git)