程序设计题:请使用委托实现信用卡用户定时还款功能

该代码示例展示了如何使用C#的委托和事件来模拟信用卡自动还款功能。在每月30号,系统会检查信用卡的月消费总额,并尝试从指定的储蓄卡中扣除相应金额进行还款。如果储蓄卡余额不足,则还款失败。程序通过委托触发还款事件,并注册多个信用卡对象进行检查。
摘要由CSDN通过智能技术生成

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

要求如下:①必须使用委托②事件的触发方式是每个月的到期还款日;

 

using System;

namespace windows_practice
{
    //信用卡类
    class CreditCard
    {
        private int CMoney;   //月消费额度
        public int TodayDate; //还款日期
        public DepositCard DC;
        public CreditCard(int Money, int Today, DepositCard DC)
        {
            this.CMoney = Money;
            this.TodayDate = Today;
            this.DC = DC;
        }
        public int GetCMoney()
        {
            return CMoney;
        }
        public void Check()
        {
            if (TodayDate == 30)
            {
                Console.WriteLine("已经是本月30号,本月需要还款金额为:{0}", CMoney);
                if (DC.GetDMoney() >= CMoney)
                {
                    Console.WriteLine("储蓄卡金额为:{0},已还款!", DC.GetDMoney());
                }
                else
                {
                    Console.WriteLine("储蓄卡金额为:{0},余额不足无法还款!", DC.GetDMoney());
                }
            }
            else
            {
                Console.WriteLine("还未到30号,请{0}天后还款!", 30 - TodayDate);
            }
        }
    }
    //储蓄卡类
    class DepositCard
    {
        private int DMpney;

        public DepositCard(int Money)
        {
            this.DMpney = Money;
        }
        public int GetDMoney()
        {
            return this.DMpney;
        }
    }

    class Delegate
    {
        public delegate void MyDelegate();
        public event MyDelegate Repay;

        public void Notify()
        {
            if (Repay != null)
            {
                Repay();
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Delegate objd = new Delegate();
            DepositCard d1 = new DepositCard(3000);
            DepositCard d2 = new DepositCard(3000);
            CreditCard c1 = new CreditCard(3000, 30, d1);
            CreditCard c2 = new CreditCard(3000, 22, d1);
            CreditCard c3 = new CreditCard(5000, 30, d2);

            objd.Repay += new Delegate.MyDelegate(c1.Check);
            objd.Repay += new Delegate.MyDelegate(c2.Check);
            objd.Repay += new Delegate.MyDelegate(c3.Check);
            objd.Notify();
        }
    }
}

运行结果: 

 

源代码: 

 

https://gitee.com/hesibao/glo.git

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值