【C#使用委托实现信用卡用户定时还款功能】

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档



一、题目:

程序设计题:请使用委托实现信用卡用户定时还款功能
本题的应用场景解释:
用户有一张信用卡,信用卡有一个总额度;每个月会有信用卡账单显示月消费总额,月消费总额是小于信用卡总额度的;用户有若干储蓄卡,可选择某张储蓄卡进行还款;还款是指从储蓄卡中划走信用卡的月消费总额到信用卡;如果储蓄卡余额不足则还款动作不成功。

二、代码

代码如下(示例):

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值