本文主要是介绍Windows课程作业C#实现银行卡还款,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
为了熟悉C#的委托与事件,用委托与事件实现简单的银行卡与储蓄卡的还款功能
前言
信用卡还款是银行系统的重要业务,业务流程说明如下:
- 用户有信用卡和储蓄卡,储蓄卡有查询余额和取款功能,信用卡能够查看账单金额、查看还款日和查看余额三个功能;
- 请使用委托与事件实现下列功能:用户可自由设置信用卡还款关联的储蓄卡,当还款日到期时进行账单金额的自动划扣;
- 请注意事件主要是设计什么时候启动委托,委托的主要目的是设置还款的储蓄卡
- 为避免设计出现偏差,给出了部分功能代码,请在此基础上进行开发。
一、基本框架
namespace ConsoleApplication3delegate{class DepositCard{public int amount;public void Display(){Console.WriteLine("储蓄卡余额为:{0}", amount);}public void Account(int balance,int payday){amount += balance;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;//请在此处添加自己的代码}class Program{static void Main(string[] args){DepositCard depositCard = new DepositCard();depositCard.amount = 10000;CreditCard creditCard1 = new CreditCard(-2000,9);CreditCard creditCard2 = new CreditCard(-3000, 13);CreditCard creditCard3 = new CreditCard(-5000, 29);depositCard.Display(); Console.WriteLine("");List<CreditCard> Cards = new List<CreditCard>();Cards.Add(creditCard1); Cards.Add(creditCard2); Cards.Add(creditCard3);foreach (CreditCard card in Cards){Console.WriteLine("信用卡开始执行委托还款。。。。。。");//请在此处添加自己的代码Console.WriteLine("");}Console.ReadLine();}}}
二、具体部分实现
1.实现委托与事件
由于题目要求,可知这个委托主要应该是对储蓄卡的操作,获取储蓄卡相关信息的方法为account它的形参为(int,int)所以将委托也设置为(int,int)便于添加。
代码如下:
public delegate void Carddelegate(int c, int d1);
public class CreditCardDelegate{public int billamount;public int repaymentday;public event Carddelegate Orderhandle;public void action(int balance,int payday){if (Orderhandle != null){Orderhandle(balance,payday);}}//请在此处添加自己的代码}
2.功能实现
代码如下:
Timer t = new Timer();
int date = DateTime.Today.Day;
int date1 = DateTime.Today.Month;
int[] monthday = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//设置时间
//其实感觉有点多余对于这个例子,可以只取这个月
CreditCardDelegate Corder = new CreditCardDelegate();//创建委托Corder.Orderhandle += depositCard.Account;//将储蓄卡的方法添加进去 foreach (CreditCard card in Cards)//遍历每一张卡{for (int i = 1; i <= monthday[date1]; i++){//循环是遍历每个月的最大天数到期还款if (date==card.getrepaymentday() ){//当满足需要还款的时间时执行还款Console.WriteLine("信用卡开始执行委托还款。。。。。。");Corder.action(card.getbillamount(), date);date++;//执行完过一天break;}else{t.Interval = 1000;date++;//不到还款日}//请在此处添加自己的代码}Console.WriteLine("");}Console.ReadLine();
3.完整代码和结果
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Timers;
namespace ConsoleApplication3delegate
{public class DepositCard{public int amount;public void Display(){Console.WriteLine("储蓄卡余额为:{0}", amount);}public void Account(int balance, int payday){amount += balance;Console.WriteLine("今天是本月的第{0}天,取款{1},储蓄卡余额为:{2}。", payday, balance, amount);}}public 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); }}public delegate void Carddelegate(int c, int d1);public class CreditCardDelegate{public int billamount;public int repaymentday;public event Carddelegate Orderhandle;public void action(int balance,int payday){if (Orderhandle != null){Orderhandle(balance,payday);}}//请在此处添加自己的代码}class Program{static void Main(string[] args){DepositCard depositCard = new DepositCard();depositCard.amount = 10000;CreditCard creditCard1 = new CreditCard(-2000, 9);CreditCard creditCard2 = new CreditCard(-3000, 13);CreditCard creditCard3 = new CreditCard(-5000, 29);depositCard.Display(); Console.WriteLine("");List<CreditCard> Cards = new List<CreditCard>();Cards.Add(creditCard1); Cards.Add(creditCard2); Cards.Add(creditCard3);Timer t = new Timer();int date = DateTime.Today.Day;int date1 = DateTime.Today.Month;int[] monthday = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };CreditCardDelegate Corder = new CreditCardDelegate();Corder.Orderhandle += depositCard.Account;foreach (CreditCard card in Cards){for (int i = 1; i <= monthday[date1]; i++){if (date==card.getrepaymentday() ){Console.WriteLine("信用卡开始执行委托还款。。。。。。");Corder.action(card.getbillamount(), date);date++;break;}else{t.Interval = 1000;date++;}//请在此处添加自己的代码}Console.WriteLine("");}Console.ReadLine();}}
}
总结
这次主要是了解了基本的委托与事件的写法。刚开始写的时候莫名其妙没用委托与事件就写完了,后来才发现一点没用委托与事件,之后进行了学习才明白。
关于委托与事件引用别人的例子:首领A要搞一场鸿门宴,吩咐部下B和C各自带队埋伏在屏风两侧,约定以杯为令:若左手举杯,则B带队杀出;若右手举杯,则C带队杀出;若直接摔杯,则B和C同时杀出。B和C袭击的具体方法,首领A并不关心。
其中就可以设置一个委托,事件(攻击),将B,C攻击方法加入事件,便可以简单实现功能,事件与委托还有许多其他应用,可以减少代码冗余,增加代码利用率,在今后的设计中也会有很好的应用。
ps:第一次写博客可能有错误,望大家见谅指正。
这篇关于Windows课程作业C#实现银行卡还款的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!