应用场景解释:
用户有一张信用卡,信用卡有一个总额度;每个月会有信用卡账单显示月消费总额,月消费总额是小于信用卡总额度的;用户有若干储蓄卡,可选择某张储蓄卡进行还款;还款是指从储蓄卡中划走信用卡的月消费总额到信用卡;如果储蓄卡余额不足则还款动作不成功。
具体要求如下:
①必须使用委托;
②事件的触发方式是每个月的到期还款日;
目录
实现步骤:
主要分为以下四步
1.定义委托与事件
2.定义People类
3.定义银行类
4.主函数部分
1.定义委托与事件
class Delegate
{
//定义委托
public delegate void MeDelegate(People people);
//定义事件
//还款日小于5天的时候定时通知还款日
public event MeDelegate Notification_event;
//到达还款日,直接扣款
public event MeDelegate Payback_event;
public void Notify(People people)
{
if (Notification_event != null)
{
Notification_event(people);
}
}
public void Payback(People people)
{
if(Payback_event != null)
{
//触发事件
Payback_event(people);
}
}
}
2.定义People类
用户具有姓名,欠款金额,账户余额和换款日期这四个属性,利用属性实现变量的set,get。
class People
{
//用户姓名
private string name;
// 所需还款金额
private int owe_money;
//所剩账户余额
private int remain_money;
//还款日期
private int payday;
public People(string name,int remain_money,int owe_money,int payday)
{
this.name = name;
this.remain_money = remain_money;
this.owe_money = owe_money;
this.payday = payday;
}
//利用属性实现get,set
public string Name
{
get { return name; } set { name = value; }
}
public int Remain_money
{
get { return remain_money; } set { remain_money = value; }
}
public int Owe_money
{
get { return owe_money; } set { owe_money = value; }
}
public int Payday
{
get { return payday; } set { payday = value; }
}
}
3.定义银行类
对用户进行欠款5天提醒与自动扣款
class Bank
{
public void Do_Notify(People people)
{
Console.WriteLine(" {0}您好,目前距还款日不足5天,请及时缴纳{1}元,谢谢!", people.Name, people.Owe_money);
}
public void Do_Payback(People people)
{
Console.WriteLine(" {0}您好,还款日已到,欠款将自动扣除,正在操作中...", people.Name);
if (people.Remain_money < people.Owe_money) { Console.WriteLine(" 余额不足,恶意欠款,还款动作不成功,您的账户已被拉黑!"); }
else{
people.Remain_money = people.Remain_money - people.Owe_money;
people.Owe_money = 0;
Console.WriteLine(" 欠款已还清,目前余额为{0}元,欠款情况为{1}元",people.Remain_money,people.Owe_money);
}
}
}
4.主函数部分
依据应用场景分为三种情况,故分别建立“储蓄卡1”、“储蓄卡2”、“储蓄卡3”三个储蓄情形,并观察其运行结果
class Program
{
static void Main(string[] args)
{
//控制输出的编码格式
Console.OutputEncoding = Encoding.GetEncoding("gb2312");
People p1 = new People("储蓄卡1", 1000, 200, 7);
People p2 = new People("储蓄卡2", 200, 20, 12);
People p3 = new People("储蓄卡3", 10, 8000, 15);
Bank bank1 = new Bank();
Delegate objDelegate1 = new Delegate();
Delegate objDelegate2 = new Delegate();
objDelegate1.Notification_event += bank1.Do_Notify;
objDelegate2.Payback_event += bank1.Do_Payback;
for (int i = 1; i <= 20; i++)
{
Console.WriteLine("****今天是第{0}天****", i);
Console.WriteLine("");
if (p1.Payday - i < 5 && p1.Payday - i > 0) objDelegate1.Notify(p1);
if (p2.Payday - i < 5 && p2.Payday - i > 0) objDelegate1.Notify(p2);
if (p3.Payday - i < 5 && p3.Payday - i > 0) objDelegate1.Notify(p3);
if (p1.Payday == i) objDelegate2.Payback(p1);
if (p2.Payday == i) objDelegate2.Payback(p2);
if (p3.Payday == i) objDelegate2.Payback(p3);
}
}
}
}
完整代码展示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homework_bank
{
class Delegate
{
//定义委托
public delegate void MeDelegate(People people);
//定义事件
//还款日小于5天的时候定时通知还款日
public event MeDelegate Notification_event;
//到达还款日,直接扣款
public event MeDelegate Payback_event;
public void Notify(People people)
{
if (Notification_event != null)
{
Notification_event(people);
}
}
public void Payback(People people)
{
if (Payback_event != null)
{
//触发事件
Payback_event(people);
}
}
}
class People
{
//用户姓名
private string name;
// 所需还款金额
private int owe_money;
//所剩账户余额
private int remain_money;
//还款日期
private int payday;
public People(string name, int remain_money, int owe_money, int payday)
{
this.name = name;
this.remain_money = remain_money;
this.owe_money = owe_money;
this.payday = payday;
}
//利用属性实现get,set
public string Name
{
get { return name; }
set { name = value; }
}
public int Remain_money
{
get { return remain_money; }
set { remain_money = value; }
}
public int Owe_money
{
get { return owe_money; }
set { owe_money = value; }
}
public int Payday
{
get { return payday; }
set { payday = value; }
}
}
class Bank
{
public void Do_Notify(People people)
{
Console.WriteLine(" {0}您好,目前距还款日不足5天,请及时缴纳{1}元,谢谢!", people.Name, people.Owe_money);
}
public void Do_Payback(People people)
{
Console.WriteLine(" {0}您好,还款日已到,欠款将自动扣除,正在操作中...", people.Name);
if (people.Remain_money < people.Owe_money) { Console.WriteLine(" 余额不足,属于恶意欠款,还款动作不成功,您的账户已被拉黑!"); }
else
{
people.Remain_money = people.Remain_money - people.Owe_money;
people.Owe_money = 0;
Console.WriteLine(" 欠款已还清,目前余额为{0}元,欠款情况为{1}元", people.Remain_money, people.Owe_money);
}
}
}
class Program
{
static void Main(string[] args)
{
//控制输出的编码格式
Console.OutputEncoding = Encoding.GetEncoding("gb2312");
People p1 = new People("储蓄卡1情况,尊敬的用户", 1000, 200, 7);
People p2 = new People("储蓄卡2情况,尊敬的用户", 200, 20, 12);
People p3 = new People("储蓄卡3情况,尊敬的用户", 10, 8000, 15);
Bank bank1 = new Bank();
Delegate objDelegate1 = new Delegate();
Delegate objDelegate2 = new Delegate();
objDelegate1.Notification_event += bank1.Do_Notify;
objDelegate2.Payback_event += bank1.Do_Payback;
for (int i = 1; i <= 20; i++)
{
Console.WriteLine("****今天是第{0}天****", i);
Console.WriteLine("");
if (p1.Payday - i < 5 && p1.Payday - i > 0) objDelegate1.Notify(p1);
if (p2.Payday - i < 5 && p2.Payday - i > 0) objDelegate1.Notify(p2);
if (p3.Payday - i < 5 && p3.Payday - i > 0) objDelegate1.Notify(p3);
if (p1.Payday == i) objDelegate2.Payback(p1);
if (p2.Payday == i) objDelegate2.Payback(p2);
if (p3.Payday == i) objDelegate2.Payback(p3);
}
}
}
}
运行结果:
练习小结:
本次练习的重点是利用委托与事件,一开始非常不熟练在参考了不少资料并了解相关用法之后,还是依葫芦画瓢实现了相关功能。