功能说明: 本程序实现了信用卡自动还款的功能。用户可以设置信用卡关联的储蓄卡,并在还款日到期时,自动从储蓄卡中扣款,完成信用卡的还款。
设计流程:
- 定义DepositCard类,实现储蓄卡的查询余额和取款功能。
- 定义CreditCard类,实现信用卡的查看账单金额、查看还款日和查看余额三个功能。在构造函数中,设置账单金额和还款日。
- 定义CreditCardDelegate类,实现设置还款的储蓄卡的功能,并定义还款事件。
- 在Main函数中,初始化储蓄卡和信用卡的对象,并将信用卡对象存入列表Cards中。对于每个信用卡,通过委托将还款事件与信用卡关联起来,设置还款的储蓄卡。
using System; using System.Collections.Generic; 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; //请在此处添加自己的代码 public delegate void Mydelegate(int bill, int repday); public event Mydelegate Myevent; public void Wornevent() { Console.WriteLine("今天是{0}年{1}月{2}号", DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day); // 如果事件不为 null if (DateTime.Today.Day == repaymentday) { Myevent(billamount, repaymentday); // 触发事件 Console.WriteLine("还款成功!"); } else Console.WriteLine("未到还款日,无需还款!"); } } class test_3_4 { static void Main(string[] args) { DepositCard depositCard = new DepositCard(); depositCard.amount = 10000; CreditCard creditCard1 = new CreditCard(-2000, 6); CreditCard creditCard2 = new CreditCard(-3000, 15); CreditCard creditCard3 = new CreditCard(-5000,24); 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("信用卡开始执行委托还款。。。。。。"); //请在此处添加自己的代码 CreditCardDelegate CD = new CreditCardDelegate(); CD.billamount = card.getbillamount(); CD.repaymentday = card.getrepaymentday(); CD.Myevent += new CreditCardDelegate.Mydelegate(depositCard.Account); CD.Wornevent(); depositCard.Display(); Console.WriteLine(""); } } } }