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

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

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

信用卡类

        我们需要设置信用卡总额度,月消费总额,以及还款日期,根据需求设置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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值