栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

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

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

根据本题所需要的功能,这里需要实现信用卡类,储蓄卡类,委托类以及测试类。

信用卡类

        我们需要设置信用卡总额度,月消费总额,以及还款日期,根据需求设置get,set方法。

public class CreditCard           //信用卡类
    {
        private int totalAmount = 10000;  //总额度
        private int totalMonthlyAmount;   //月消费总额
        private string repayDate;            //还款日期

        public string year = DateTime.Now.ToString("yyyy-MM-dd").Split('-')[0];  //当前年月日
        public string month = DateTime.Now.ToString("yyyy-MM-dd").Split('-')[1];
        public string day = "15";
        
        public CreditCard()
        {
            Console.WriteLine("还款日期:" + month + "月" + day +"日");
        }
        

        public int gettotalMonthlyAmount()
        {
            return this.totalMonthlyAmount;
        }

        public void settotalMonthlyAmount()
        {
            Console.Write("本月消费:");
            int totalMonthlyAmount = Convert.ToInt32(Console.ReadLine());
            if (totalMonthlyAmount >= 0 && totalMonthlyAmount < totalAmount)
                this.totalMonthlyAmount = totalMonthlyAmount;
            else
                Console.WriteLine("已超过信用卡总额度!");
        }

        public void setrepayDate()
        {
            Console.Write("当前日期:");
            string repayDate = Console.ReadLine();     //注意此处输入格式为:例如四月十二则输入0412
            this.repayDate = repayDate;
            if (String.Equals(this.repayDate,(month+day)))
            {
                Console.WriteLine("当日可还款!");
            }
            else
            {
                Console.WriteLine("未到还款日期!");
                System.Environment.Exit(0);
            }
        }
    }

储蓄卡类

        需要注意,其中的charge()方法与下面的委托payBackMoney()相关联,其参数列表与返回值类型需一致。

public class AccountCard           //储蓄卡类
    {
        private int Balance;          //储蓄卡余额

        public int getBalance()
        {
            return this.Balance;
        }

        public void setBalance()
        {
            Console.Write("储蓄卡余额:");
            int Balance = Convert.ToInt32(Console.ReadLine());
            this.Balance = Balance;
        }

        public void reCharge(AccountCard accountcart)
        {
            int money = Convert.ToInt32(Console.ReadLine());
            this.Balance += money;
            Console.WriteLine("充值成功!当前余额:"+ accountcart.getBalance());
        }

        public void charge(AccountCard accountcart, CreditCard creditcard)
        {
            if (accountcart.getBalance() > creditcard.gettotalMonthlyAmount())
            {
                this.Balance = accountcart.getBalance() - creditcard.gettotalMonthlyAmount();
                Console.WriteLine("还款成功,余额 " + (this.Balance) +" 元");
            }
            else
            {
                Console.Write("余额不足,还款失败,请充值 :");
                reCharge(accountcart);
                charge(accountcart, creditcard);
            }
        }
    }

委托类
public class Delegate    //委托事件类
    {
        public delegate void payBackMoney(AccountCard accountcart, CreditCard creditcard);
        public event payBackMoney payBackMoneyEvent;

        public void isPayTrue(AccountCard accountcart, CreditCard creditcard)
        {
            if (payBackMoneyEvent != null)
                payBackMoneyEvent(accountcart, creditcard);
            //payBackMoneyEvent?.Invoke(accountcart, creditcard);  //简化

        }
    }

结合以上三个类,可以设计出一个功能较为简单的信用卡定期还款的功能。在还款日期上,还未能实现动态设置,仍需改进。

完整代码如下:

using System;
namespace Csharpwork1 {

    public class Delegate    //委托事件类
    {
        public delegate void payBackMoney(AccountCard accountcart, CreditCard creditcard);
        public event payBackMoney payBackMoneyEvent;

        public void isPayTrue(AccountCard accountcart, CreditCard creditcard)
        {
            if (payBackMoneyEvent != null)
                payBackMoneyEvent(accountcart, creditcard);
            //payBackMoneyEvent?.Invoke(accountcart, creditcard);  //简化

        }
    }

    public class CreditCard           //信用卡类
    {
        private int totalAmount = 10000;  //总额度
        private int totalMonthlyAmount;   //月消费总额
        private string repayDate;            //还款日期

        public string year = DateTime.Now.ToString("yyyy-MM-dd").Split('-')[0];  //当前年月日
        public string month = DateTime.Now.ToString("yyyy-MM-dd").Split('-')[1];
        public string day = "15";
        
        public CreditCard()
        {
            Console.WriteLine("还款日期:" + month + "月" + day +"日");
        }
        

        public int gettotalMonthlyAmount()
        {
            return this.totalMonthlyAmount;
        }

        public void settotalMonthlyAmount()
        {
            Console.Write("本月消费:");
            int totalMonthlyAmount = Convert.ToInt32(Console.ReadLine());
            if (totalMonthlyAmount >= 0 && totalMonthlyAmount < totalAmount)
                this.totalMonthlyAmount = totalMonthlyAmount;
            else
                Console.WriteLine("已超过信用卡总额度!");
        }

        public void setrepayDate()
        {
            Console.Write("当前日期:");
            string repayDate = Console.ReadLine();     //注意此处输入格式为:例如四月十二则输入0412
            this.repayDate = repayDate;
            if (String.Equals(this.repayDate,(month+day)))
            {
                Console.WriteLine("当日可还款!");
            }
            else
            {
                Console.WriteLine("未到还款日期!");
                System.Environment.Exit(0);
            }
        }
    }

    public class AccountCard           //储蓄卡类
    {
        private int Balance;          //储蓄卡余额

        public int getBalance()
        {
            return this.Balance;
        }

        public void setBalance()
        {
            Console.Write("储蓄卡余额:");
            int Balance = Convert.ToInt32(Console.ReadLine());
            this.Balance = Balance;
        }

        public void reCharge(AccountCard accountcart)
        {
            int money = Convert.ToInt32(Console.ReadLine());
            this.Balance += money;
            Console.WriteLine("充值成功!当前余额:"+ accountcart.getBalance());
        }

        public void charge(AccountCard accountcart, CreditCard creditcard)
        {
            if (accountcart.getBalance() > creditcard.gettotalMonthlyAmount())
            {
                this.Balance = accountcart.getBalance() - creditcard.gettotalMonthlyAmount();
                Console.WriteLine("还款成功,余额 " + (this.Balance) +" 元");
            }
            else
            {
                Console.Write("余额不足,还款失败,请充值 :");
                reCharge(accountcart);
                charge(accountcart, creditcard);
            }
        }
    }


    public class Test         //测试类
    {
        static void Main(string[] args)
        {
            Delegate dlg = new Delegate();
            CreditCard creditcard = new CreditCard();
            AccountCard accountcart = new AccountCard();

            creditcard.setrepayDate();
            accountcart.setBalance();
            creditcard.settotalMonthlyAmount();

            dlg.payBackMoneyEvent += new Delegate.payBackMoney(accountcart.charge);
            dlg.isPayTrue(accountcart, creditcard);
            Console.ReadKey();
        }    
    }
}

 运行测试结果:

 

GitHub - LaterCat/Csharpwork1Contribute to LaterCat/Csharpwork1 development by creating an account on GitHub.https://github.com/LaterCat/Csharpwork1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/954705.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号