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

功能说明:以委托和事件的方法来解决用户信用卡定时还款问题

                 设置信用卡类,扣款类,还款类来进行程序运行,设置某一特定还款日期,到相应时间后,就检查信用卡是否欠款,若欠款,则开始执行还款操作;若未欠款,则退出。

核心代码: 

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

namespace studentwork
{
    class Savings
    {
        int RemainingSum = 20000;
        Credit x;   //信用
        public Savings(Credit x)
        {
            this.x = x;
        }
        public void DeductMoney()   //扣款
        {
            if (x.CheckPayment() == 1)
            {
                RemainingSum = RemainingSum - x.GetAmountPending();
                Console.WriteLine("还款成功,余额为:{0}", RemainingSum);
            }
            else
            {
                Console.WriteLine("尚不需要还款,余额为:{0}", RemainingSum);
            }
        }
    }

    class Credit
    {
        int DueDate = 29;
        int AmountPending;
        int DateOfRepayment;
        public Credit(int RepaymentAmount, int RepaymentDate)
        {
            this. AmountPending= RepaymentAmount;
            this.DateOfRepayment = RepaymentDate;
        }
        public int GetAmountPending()
        {
            return AmountPending;
        }
        public int CheckPayment()
        {
            if (DueDate == DateOfRepayment)
                return 1;
            else
                return 0;
        }
    }
    class Delegate
    {
        public delegate void pay();
        public event pay payment;
        public void Notify()
        {
            if (payment != null)
            {
                Console.WriteLine("触发事件:");
                payment();
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Delegate objDelegate = new Delegate();
            Credit a1 = new Credit(1000, 27);
            Credit a2 = new Credit(1000, 29);
            Savings b1 = new Savings(a1);
            Savings b2 = new Savings(a2);
            objDelegate.payment +=
                new Delegate.pay(b1.DeductMoney);
            objDelegate.payment +=
               new Delegate.pay(b2.DeductMoney);
            objDelegate.Notify();
            Console.ReadLine();
        }
    }
}

实验结果: 

实验小结:(1)熟练地掌控事件和委托地使用方法可以避免多余的代码

                   (2)实验中对函数的调用依然不够熟悉,对语法和相关参数的使用依然存在问题,

                            需要大量练习

                    (3)对于委托和事件的理解不够到位,浪费了时间 ,但合理使用后,的确程序写起来更加方便

https://gitee.com/zhan-chaofan/fan.warehouse.git

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值