Windows程序设计作业一
目录
- 一、作业题目
- 二、功能描述
- 三、核心代码
- 四、结果截图
- 五、仓库链接
一、作业题目
程序设计题:使用委托实现信用卡用户定时还款功能
应用场景解释:用户有一张信用卡,信用卡有一个总额度;每个月会有信用卡账单显示月消费总额,月消费总额是小于信用卡总额度的;用户有若干储蓄卡,可选择某张储蓄卡进行还款;还款是指从储蓄卡中划走信用卡的月消费总额到信用卡;如果储蓄卡余额不足则还款动作不成功。
要求如下:①必须使用委托②事件的触发方式是每个月的到期还款日
二、功能描述
1)定义储蓄卡类,类中有用户名、卡号、余额三个成员,以及相应的get()、set()方法。
2)定义信用卡类,类中有用户名、还款日期、还款金额、绑定储蓄卡对象四个成员。以及需要还款和无需还款的方法。
3)定义扣款委托类。
4) 主函数中定义储蓄卡对象数组,表示某用户拥有的所有储蓄卡。
5) 用户输入想要还款的储蓄卡编号,则信用卡和该张储蓄卡绑定。
6) 用System.DateTime获取当前日期。
7) 判断当前日期是否为还款日期,若是还款日期,使用委托为信用卡订阅需要还款事件;反之使用委托为信用卡订阅无需还款事件。
8)若到还款日期,用户选择的储蓄卡余额不足,提示更换储蓄卡;反之还款成功。
三、核心代码
using System;
namespace ConsoleApp4
{class Program{//储蓄卡类class depositCard{//储蓄卡号private long id;//储蓄名private string name;//储蓄金额private int depositMoney;//初始化public depositCard(string name, int money,long id){this.name = name;this.depositMoney = money;this.id = id;}//获取储蓄卡idpublic long getId(){return this.id;}//获取储蓄金额public int getDepositMoney(){return this.depositMoney;}//修改储蓄金额public void setDepositMoney(int r){this.depositMoney = r;}}//信用卡类class creditCard{//信用卡还款日期(因还款日期固定,设置为静态)static public int dueDay=2;//信用卡用户名public string name;//信用卡消费金额public int consumeMoney;//绑定的储蓄卡对象public depositCard dCard;//初始化public creditCard(string name, int consumeMoney, depositCard dCard){this.name = name;this.consumeMoney = consumeMoney;this.dCard = dCard;} //需要还款public void repayM(){System.DateTime currentTime = new System.DateTime();currentTime = System.DateTime.Now;Console.WriteLine("当前日期:{0}月{1}日", currentTime.Month, currentTime.Day);Console.WriteLine("用户名:{0} 卡号:{1} 卡内余额:{2}", this.name, this.dCard.getId(), this.dCard.getDepositMoney()) ;Console.WriteLine("**********************************************************");if (this.dCard.getDepositMoney() - consumeMoney >= 0){dCard.setDepositMoney(this.dCard.getDepositMoney() - consumeMoney);Console.WriteLine("用户已还款");Console.WriteLine("还款金额:{0}", this.consumeMoney);Console.WriteLine("卡内余额:{0}", dCard.getDepositMoney());Console.WriteLine();}else {Console.WriteLine("提示:您的余额不足!请更换储蓄卡!");}}//无需还款public void norepayM(){System.DateTime currentTime = new System.DateTime();currentTime = System.DateTime.Now;Console.WriteLine("当前日期:{0}月{1}日", currentTime.Month, currentTime.Day);Console.WriteLine("用户名:{0} 卡内余额:{1}", this.name, this.dCard.getDepositMoney());Console.WriteLine("**********************************************************");Console.WriteLine("下次还款日期为{0}月{1}号,未到还款日期,无需还款", (creditCard.dueDay> currentTime.Day? currentTime.Month: currentTime.Month+1), creditCard.dueDay);Console.WriteLine();}}//扣款委托类class repayDelegate{//扣款委托public delegate void repayMoney();//扣款事件public event repayMoney DoRepay;//事件执行public void NotifyRepay(){if (DoRepay != null){Console.WriteLine("触发事件:");// 触发事件DoRepay();}}}static void Main(string[] args){//创建储蓄卡对象数组depositCard[] D = new depositCard[3];D[0] = new depositCard("Jenny", 2000, 6217213407601880725);D[1] = new depositCard("Jenny", 5000, 6217211107051540774);D[2] = new depositCard("Jenny", 10000, 6717314207071809721);//创建委托对象repayDelegate rD = new repayDelegate();//判断是否为还款日期System.DateTime currentTime = new System.DateTime();currentTime = System.DateTime.Now;//用户选择还款储蓄卡Console.WriteLine("请选择还款的储蓄卡:");Console.WriteLine("1(卡1) 2(卡2) 3(卡3)");int i = int.Parse(Console.ReadLine());creditCard C = new creditCard("Jenny", 8000, D[i - 1]);//判断当前日期是否为还款日期if (creditCard.dueDay == currentTime.Day){//事件订阅rD.DoRepay += new repayDelegate.repayMoney(C.repayM);}else{//事件订阅rD.DoRepay += new repayDelegate.repayMoney(C.norepayM);}//事件委托rD.NotifyRepay();}}
}
四、结果截图
本次实验设置储蓄卡数量为3,卡一余额为2000,卡二余额为5000,卡三余额为10000,信用卡需还款金额为8000,不同情况运行结果如下。
1)设置还款日期(静态)为当前日期
选择卡一为还款储蓄卡:
选择卡二为还款储蓄卡:

设置卡三为还款储蓄卡:

(2)当前日期不是还款日期
设置每月2号为还款日期:

设置每月1号为还款日期:

五、仓库链接
Gitee仓库
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
