提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、题目:
程序设计题:请使用委托实现信用卡用户定时还款功能
本题的应用场景解释:
用户有一张信用卡,信用卡有一个总额度;每个月会有信用卡账单显示月消费总额,月消费总额是小于信用卡总额度的;用户有若干储蓄卡,可选择某张储蓄卡进行还款;还款是指从储蓄卡中划走信用卡的月消费总额到信用卡;如果储蓄卡余额不足则还款动作不成功。
二、代码
代码如下(示例):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CreditCard
{
class CashCard//储蓄卡类
{
int balance;//余额
CreditCard c;//信用卡
public CashCard() { }
public CashCard(int balance, CreditCard c)
{
this.balance = balance;
this.c = c;
}
public void SetCredit(CreditCard c)
{
this.c = c;
}
public void CheckBanlance()
{
if (c.checkDate())//判断是否到了还款日
{
balance = balance - c.getPayBanlance();
if (balance >= 0)
{
Console.WriteLine("还款成功,你的余额为:{0}", balance);
}
else
{
Console.WriteLine("还款失败,余额不足");
}
}
else if (!c.checkDate())
{
Console.WriteLine("不需要还款,你的余额为:{0}", balance);
}
}
}
class CreditCard//信用卡类
{
int PayDate = 30;
int PayBanlance;
int TodayDate;
public CreditCard() { }
public CreditCard(int PayBanlance, int TodayDate)
{
this.PayBanlance = PayBanlance;
this.TodayDate = TodayDate;
}
public int getPayBanlance()
{
return PayBanlance;
}
public bool checkDate()//判断还款日
{
if (TodayDate == PayDate)
{
return true;
}
else
{
return false;
}
}
}
class Delegate
{
public delegate void MeDelegate();//定义一个委托
public event MeDelegate NotifyEventOne;//定义事件
public void Notify()
{
if (NotifyEventOne != null)
{
Console.WriteLine("触发事件");
NotifyEventOne();
}
}
}
class Program
{
[STAThread]
static void Main(string[] args)
{
Delegate objDelegate = new Delegate();
CreditCard c1 = new CreditCard(4000, 29);
CreditCard c2 = new CreditCard(2000, 30);
CreditCard c3 = new CreditCard(6000, 30);
CashCard ca1 = new CashCard(5000, c1);
CashCard ca2 = new CashCard(10000, c2);
objDelegate.NotifyEventOne += new Delegate.MeDelegate(ca1.CheckBanlance);
objDelegate.NotifyEventOne += new Delegate.MeDelegate(ca2.CheckBanlance);
objDelegate.Notify();
}
}
}
总结
gitee仓库的clone地址:
链接: https://gitee.com/nine-hundred-and-twelve/cscouse.git.