功能说明:以委托和事件的方法来解决用户信用卡定时还款问题
设置信用卡类,扣款类,还款类来进行程序运行,设置某一特定还款日期,到相应时间后,就检查信用卡是否欠款,若欠款,则开始执行还款操作;若未欠款,则退出。
核心代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace studentwork
{
class Savings
{
int RemainingSum = 20000;
Credit x; //信用
public Savings(Credit x)
{
this.x = x;
}
public void DeductMoney() //扣款
{
if (x.CheckPayment() == 1)
{
RemainingSum = RemainingSum - x.GetAmountPending();
Console.WriteLine("还款成功,余额为:{0}", RemainingSum);
}
else
{
Console.WriteLine("尚不需要还款,余额为:{0}", RemainingSum);
}
}
}
class Credit
{
int DueDate = 29;
int AmountPending;
int DateOfRepayment;
public Credit(int RepaymentAmount, int RepaymentDate)
{
this. AmountPending= RepaymentAmount;
this.DateOfRepayment = RepaymentDate;
}
public int GetAmountPending()
{
return AmountPending;
}
public int CheckPayment()
{
if (DueDate == DateOfRepayment)
return 1;
else
return 0;
}
}
class Delegate
{
public delegate void pay();
public event pay payment;
public void Notify()
{
if (payment != null)
{
Console.WriteLine("触发事件:");
payment();
}
}
}
class Program
{
static void Main(string[] args)
{
Delegate objDelegate = new Delegate();
Credit a1 = new Credit(1000, 27);
Credit a2 = new Credit(1000, 29);
Savings b1 = new Savings(a1);
Savings b2 = new Savings(a2);
objDelegate.payment +=
new Delegate.pay(b1.DeductMoney);
objDelegate.payment +=
new Delegate.pay(b2.DeductMoney);
objDelegate.Notify();
Console.ReadLine();
}
}
}
实验结果:
实验小结:(1)熟练地掌控事件和委托地使用方法可以避免多余的代码
(2)实验中对函数的调用依然不够熟悉,对语法和相关参数的使用依然存在问题,
需要大量练习
(3)对于委托和事件的理解不够到位,浪费了时间 ,但合理使用后,的确程序写起来更加方便