使用委托实现信用卡用户定时还款功能

应用场景:

用户有一张信用卡,信用卡有一个总额度;
每个月会有信用卡账单显示月消费总额(月消费总额是小于信用卡总额度的);
用户有若干储蓄卡,可选择某张储蓄卡进行还款;
还款是指从储蓄卡中划走信用卡的月消费总额到信用卡;
如果储蓄卡余额不足则还款动作不成功。
要求:①必须使用委托②事件的触发方式是每个月的到期还款日;

代码:

信用卡类:

首先,信用卡类有一个总额度,因为在每个月会有信用卡账单显示月消费总额,于是我们还要写一个账单的成员函数。

    class Xinyong
    {
        private float sum;
        private float xiaofei;
        public Xinyong(float a,float b)
        {
            sum = a;
            xiaofei = b;
        }
        public void Zhangdan()
        {
            Console.WriteLine("--本月消费账单--");
            Console.WriteLine("本月消费:");
            Console.WriteLine(xiaofei);
            Console.WriteLine("----------------");
        }
    }

储蓄卡类:

用户通过储蓄卡进行还款。

class Chuxu
    {
        private float sum;
        private float xiaofei;
        public Chuxu(float a,float b)
        {
            sum = a;
            xiaofei=b;
        }
        public void Huankuan()
        {
            if(sum<xiaofei )
            {
                Console.WriteLine("还款失败,请使用其他储蓄卡");
            }
            else
            {
                sum -= xiaofei;
                Console.WriteLine("还款成功!");
                Console.WriteLine("储蓄卡余额:");
                Console.WriteLine(sum);
            }
        }
    }

委托类:

还款的触发条件是“已到本月还款日期”。

class Delegate
    {
        // 定义委托
        public delegate void MeDelegate();
        // 定义事件
        public event MeDelegate myevent;

        public void Notify()
        {
            // 如果事件不为 null
            if (myevent!= null)
            {
                Console.WriteLine("#已到本月还款日期#");
                // 触发事件
                myevent();
            }
        }
    }

主函数:

在主函数里面,我们创建多个储蓄卡类的对象,以实现选择想要的储蓄卡进行还款。同时把还款添加到事件中,使得在“已到本月还款日期”的条件下进行还款。

    class Test
    {
        static void Main(string[] args)
        {
            float xiaofei;
            string a,b;
            // 委托的对象
            Delegate obj1 = new Delegate();
            Console.WriteLine("请输入消费:");
            a=Console.ReadLine();
            xiaofei=float.Parse(a);
            Xinyong obj2 = new Xinyong(1000,xiaofei);
            Chuxu obj3 = new Chuxu(1000, xiaofei);
            Chuxu obj4 = new Chuxu(500, xiaofei);
            obj1.myevent += new Delegate.MeDelegate(obj2.Zhangdan);
            Console.WriteLine("请选择储蓄卡1还是储蓄卡2:");
            b = Console.ReadLine();
            if(b=="1")
            {
                obj1.myevent += new Delegate.MeDelegate(obj3.Huankuan);
            }
            else if(b=="2")
            {
                obj1.myevent += new Delegate.MeDelegate(obj4.Huankuan);
            }
            else
            {
                Console.WriteLine("储蓄卡输入错误!");
            }
            obj1.Notify();
        }
    }

全部代码:

using System;
namespace app
{
    class Xinyong
    {
        private float sum;
        private float xiaofei;
        public Xinyong(float a,float b)
        {
            sum = a;
            xiaofei = b;
        }
        public void Zhangdan()
        {
            Console.WriteLine("--本月消费账单--");
            Console.WriteLine("本月消费:");
            Console.WriteLine(xiaofei);
            Console.WriteLine("----------------");
        }
    }
    // 第二个类
    class Chuxu
    {
        private float sum;
        private float xiaofei;
        public Chuxu(float a,float b)
        {
            sum = a;
            xiaofei=b;
        }
        public void Huankuan()
        {
            if(sum<xiaofei )
            {
                Console.WriteLine("还款失败,请使用其他储蓄卡");
            }
            else
            {
                sum -= xiaofei;
                Console.WriteLine("还款成功!");
                Console.WriteLine("储蓄卡余额:");
                Console.WriteLine(sum);
            }
        }
    }
    class Delegate
    {
        // 定义委托
        public delegate void MeDelegate();
        // 定义事件
        public event MeDelegate myevent;

        public void Notify()
        {
            // 如果事件不为 null
            if (myevent!= null)
            {
                Console.WriteLine("#已到本月还款日期#");
                // 触发事件
                myevent();
            }
        }
    }
    class Test
    {
        static void Main(string[] args)
        {
            float xiaofei;
            string a,b;
            // 委托的对象
            Delegate obj1 = new Delegate();
            Console.WriteLine("请输入消费:");
            a=Console.ReadLine();
            xiaofei=float.Parse(a);
            Xinyong obj2 = new Xinyong(1000,xiaofei);
            Chuxu obj3 = new Chuxu(1000, xiaofei);
            Chuxu obj4 = new Chuxu(500, xiaofei);
            obj1.myevent += new Delegate.MeDelegate(obj2.Zhangdan);
            Console.WriteLine("请选择储蓄卡1还是储蓄卡2:");
            b = Console.ReadLine();
            if(b=="1")
            {
                obj1.myevent += new Delegate.MeDelegate(obj3.Huankuan);
            }
            else if(b=="2")
            {
                obj1.myevent += new Delegate.MeDelegate(obj4.Huankuan);
            }
            else
            {
                Console.WriteLine("储蓄卡输入错误!");
            }
            obj1.Notify();
        }
    }
}

运行截图:

在这里插入图片描述
在这里插入图片描述

总结:

对于信用卡用户定时还款,最重要的就是通过使用委托、事件在“已到本月还款日期”的同时,进行储蓄卡的还款。

源码的clone地址:

https://github.com/pan20174/pan20174/tree/main

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值