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

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

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

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp8
{
    public class Card
    {
        private string name;//储蓄卡名
        private int amount;//金额
        public Card(string name, int amount)
        {
            this.name = name;
            this.amount = amount;
        }
        public int GetMoney()
        {
            return this.amount; 
        } 
        public void SetMoney(int r)
        {
            this.amount = r; 
        }
    }
    public class creditCard
    {
        public string name;        
        private int creditMoney;//信用卡余额        
        public int Day;//扣款日期
        private Card DC;//绑定储蓄卡对象
        public creditCard(string name, int creditmoney, int Day, Card DC)
        {
            this.name = name;
            this.creditMoney = creditmoney;
            this.Day = Day;
            this.DC = DC;
        }
        public void Repayment()  //还款
        {
            Console.WriteLine("用户{0}当前金额{1}", this.name, this.DC.GetMoney());
            DC.SetMoney(this.DC.GetMoney() + creditMoney);
            Console.WriteLine("用户已还款");
            Console.WriteLine("还款金额:"+ Math.Abs(this.creditMoney));
            Console.WriteLine("余下金额为"+ DC.GetMoney());
        }
        public void NoRepayment() 
        {
            Console.WriteLine("未到还款日期,用户无需还款");
            Console.WriteLine("余下金额为"+ DC.GetMoney());
        }
    }  
    class Delegate//还款委托类
    {       
        public delegate void repayMoney();//还款委托        
        public event repayMoney DoRepay;//还款事件        
        public void NotifyRepay()//事件执行
        {
            if (DoRepay != null)
            {
                Console.WriteLine("触发事件:");
                DoRepay();
            }
         }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Card D1 = new Card("wxy1", 10000);
            Card D2 = new Card("wxy2", 5000);
            creditCard C1 = new creditCard("wxy", -1000, 2, D1);
            ArrayList Cards = new ArrayList();
            Cards.Add(C1);

            //创建委托对象
            Delegate rD = new Delegate();
            foreach (creditCard C in Cards)
            {
                //判断是否到了该还款的日期
                if (C.Day == int.Parse(DateTime.Now.ToString("yyyy-MM-dd").Split('-')[2]))
                {
                    //事件添加
                    rD.DoRepay += new Delegate.repayMoney(C.Repayment);
                }
                else
                {
                    rD.DoRepay += new Delegate.repayMoney(C.NoRepayment);
                }
            }
            //事件执行

            rD.NotifyRepay();
            Console.ReadLine();
        }
    }
}

https://gitee.com/wu-xin-yi/wxygit

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值