windows程序设计实验二

一、题目

信用卡还款是银行系统的重要业务,业务流程说明如下:

  1. 用户有信用卡和储蓄卡,储蓄卡有查询余额和取款功能,信用卡能够查看账单金额、查看还款日和查看余额三个功能;

  1. 请使用委托与事件实现下列功能:用户可自由设置信用卡还款关联的储蓄卡,当还款日到期时进行账单金额的自动划扣;

  1. 请注意事件主要是设计什么时候启动委托,委托的主要目的是设置还款的储蓄卡

  1. 为避免设计出现偏差,给出了部分功能代码,请在此基础上进行开发。

二、功能说明

信用卡到还款日后自动扣除绑定储蓄卡金额

三、设计流程

四、关键源码

储蓄卡类

class DepositCard//储蓄卡
    {
        public int amount;

        public void Display()
        {
            Console.WriteLine("储蓄卡余额为:{0}", amount);
        }
        public void Account(int balance,int repayment)//还款
        {
            amount += balance;//amount余额
            Console.WriteLine("今天是本月的{0},取款{1},储蓄卡余额为:{2}。", DateTime.Today.Day, balance, amount);
        }
    }

信用卡类

class CreditCard//信用卡
    {
        private int billamount;//账单金额
        private int repaymentday;//还款日

        public CreditCard(int billamount, int repaymentday)//创建信用卡
        {
            this.billamount = billamount;
            this.repaymentday = repaymentday;
        }

        public int getbillamount() { return billamount; }//信用卡查账单金额

        public int getrepaymentday() { return repaymentday; }//信用卡查还款日

        public void Display() { Console.WriteLine("信用卡余额为:{0}", billamount); }//信用卡查余额

    }

委托类

class CreditCardDelegate//委托
    {
        public int billamount;//账单
        public int repaymentday;//还款日
        public delegate void DrdDelegate(int bill,int day);//委托声明
        public event DrdDelegate EvenDrd;
        public void Reply()
        {
            
            if (DateTime.Today.Day == repaymentday)
            {
                EvenDrd(billamount, repaymentday);
                Console.WriteLine("还款成功");
            }
            else Console.WriteLine("未到达还款日,不扣款");
        }
    }

主程序

class Program
    {
        static void Main(string[] args)
        {
            DepositCard depositCard = new DepositCard();//建立储蓄卡账户
            depositCard.amount = 10000;//初始余额10000
            CreditCard creditCard1 = new CreditCard(-2000, 6);//设置还款账单,还款日
            CreditCard creditCard2 = new CreditCard(-3000, 6);
            CreditCard creditCard3 = new CreditCard(-5000, 6);
            Console.WriteLine("");
            List<CreditCard> Cards = new List<CreditCard>();//信用卡列,方便循环
            Cards.Add(creditCard1); Cards.Add(creditCard2); Cards.Add(creditCard3);
            depositCard.Display();//储蓄卡余额
            Console.WriteLine("");
            CreditCardDelegate Six = new CreditCardDelegate();
            foreach (CreditCard card in Cards)
            {
                Console.WriteLine("信用卡开始执行委托还款。。。。。。");
                
                CreditCardDelegate delegate1= new CreditCardDelegate();
                delegate1.billamount=card.getbillamount();
                delegate1.repaymentday=card.getrepaymentday();
                delegate1.EvenDrd += new CreditCardDelegate.DrdDelegate(depositCard.Account);
                delegate1.Reply();

                Console.WriteLine("");
            }
            Console.ReadLine();
        }
    }
}

五、结果

六、总结

这项实验核心就是委托和事件,除去略微学会委托、事件的声明和使用,我还知道了1、静态函数不能够调用非静态函数,2、占位符的使用(在输出时若使用汉字大括号符号则无法正确输出占位符对应的数据,此时因为占位符在输出语句中双引号中,所以不会报错)3、若一个类想要调用另一个类的函数(如B想调用中的a(),可A test=new A();test.a();

七、源码

https://gitee.com/Elvin-Apocalys/win

using System;
using System.Security.Principal;

namespace ConsoleApplication3delegate
{
    class DepositCard//储蓄卡
    {
        public int amount;

        public void Display()
        {
            Console.WriteLine("储蓄卡余额为:{0}", amount);
        }
        public void Account(int balance,int repayment)//还款
        {
            amount += balance;//amount余额
            Console.WriteLine("今天是本月的{0},取款{1},储蓄卡余额为:{2}。", DateTime.Today.Day, balance, amount);
        }
    }

    class CreditCard//信用卡
    {
        private int billamount;//账单金额
        private int repaymentday;//还款日

        public CreditCard(int billamount, int repaymentday)//创建信用卡
        {
            this.billamount = billamount;
            this.repaymentday = repaymentday;
        }

        public int getbillamount() { return billamount; }//信用卡查账单金额

        public int getrepaymentday() { return repaymentday; }//信用卡查还款日

        public void Display() { Console.WriteLine("信用卡余额为:{0}", billamount); }//信用卡查余额

    }

    class CreditCardDelegate//委托
    {
        public int billamount;//账单
        public int repaymentday;//还款日
 
        public delegate void DrdDelegate(int bill,int day);//委托声明
        public event DrdDelegate EvenDrd;
        public void Reply()
        {
           
            if (DateTime.Today.Day == repaymentday)
            {
                EvenDrd(billamount, repaymentday);
                Console.WriteLine("还款成功");
            }
            else Console.WriteLine("未到达还款日,不扣款");
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            DepositCard depositCard = new DepositCard();//建立储蓄卡账户
            depositCard.amount = 10000;//初始余额10000
            CreditCard creditCard1 = new CreditCard(-2000, 6);//设置还款账单,还款日
            CreditCard creditCard2 = new CreditCard(-3000, 6);
            CreditCard creditCard3 = new CreditCard(-5000, 6);
            Console.WriteLine("");
            List<CreditCard> Cards = new List<CreditCard>();
            Cards.Add(creditCard1); Cards.Add(creditCard2); Cards.Add(creditCard3);
            depositCard.Display();//储蓄卡余额
            Console.WriteLine("");
            CreditCardDelegate Six = new CreditCardDelegate();
            foreach (CreditCard card in Cards)
            {
                Console.WriteLine("信用卡开始执行委托还款。。。。。。");
                //请在此处添加自己的代码
                CreditCardDelegate delegate1= new CreditCardDelegate();
                delegate1.billamount=card.getbillamount();
                delegate1.repaymentday=card.getrepaymentday();
                delegate1.EvenDrd += new CreditCardDelegate.DrdDelegate(depositCard.Account);
                delegate1.Reply();
               
               // creditCard1.Display();
                Console.WriteLine("");
            }
            Console.ReadLine();
        }
    }
}

八、(我是菜鸡)借鉴自

https://blog.csdn.net/peanutaasdzz/article/details/123900234?spm=1001.2014.3001.5506

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值