一、核心代码及其功能
1、委托类
class Delegates //委托类
{
public delegate void MeDelegate(StorageCard card2);
public event MeDelegate Remind;
public event MeDelegate Pay;
public void RemindNotify(StorageCard card2)
{
if (Remind != null)
{
Console.WriteLine("已经到还款日啦!\n请您保证储值卡余额充足哦,如有遗忘,我们会在次日7点整进行扣款哦!\n");
Remind(card2);
}
else
Console.WriteLine("临近还款日啦!\n记得按时还款哦!");
}
public void PayNotify(StorageCard card2)
{
if (Pay != null)
{
Console.WriteLine("已经到还款日啦!\n请保证储值卡余额充足哦,如有遗忘,我们会在次日7点整进行扣款哦!\n");
Pay(card2);
}
else
Console.WriteLine("临近还款日啦!\n记得按时还款哦!");
}
}
2、银行卡类定义
class StorageCard
{
public int countnum; //账户号码
public int balance; //账户余额
public int debt; //应还欠款
public int date; //还款日期
public StorageCard()
{
}
public StorageCard(int countnum, int balance, int debt, int date)
{
this.countnum = countnum;
this.balance = balance;
this.debt = debt;
this.date = date;
}
}
class CreditCard
{
static public void remind(StorageCard card2)
{
Console.WriteLine("还款日要到了!账户为{0}的借款为{1}!", card2.countnum, card2.balance);
}
static public void PayMoney(StorageCard card2)
{
if (card2.debt > card2.balance)
Console.WriteLine("账户为{0}的借款为{1}元整,您的储蓄卡余额不足!为{2}元\n", card2.countnum, card2.debt, card2.balance);
else
Console.WriteLine("账户为{0}的借款为{1}元整,您的储蓄卡余额为{2}元整,还款后还剩余{3}元整\n", card2.countnum, card2.debt, card2.balance, card2.balance-card2.debt);
}
}
3、主函数
static void Main(string[] args)
{
Delegates obj1 = new Delegates();
Delegates obj2 = new Delegates();
CreditCard card1 = new CreditCard();
StorageCard card2 = new StorageCard(11111,2000,1569,8);
StorageCard card3 = new StorageCard(22222,3000,669,3);
obj1.Remind += new Delegates.MeDelegate(CreditCard.remind);
obj2.Pay += new Delegates.MeDelegate(CreditCard.PayMoney);
for(int i = 1; i<=30; i++)
{
if (i == card2.date)
obj2.PayNotify(card2);
if (i == card3.date)
obj2.PayNotify(card3);
}
}
4、运行结果
到还款日期,银行卡余额充足:
到还款日期银行卡余额不足:
…
对用户欠款以及银行卡余额参数进行修改,多次调试,保证程序的运行结果正确!
二、学习总结
- 在本次的学习中,对委托类有了更深的学习以及了解,通过代码实践,也凸显出一些基本语法的熟悉度不足,对委托事件的用法以及调用不熟练,导致频繁出错,在接下来的学习中,课堂上掌握理论知识后会及时对函数以及结构等知识进行代码实践,让自己对C#语言更为熟练!