解题思路
设计时分成4个模块来设计,分别是储蓄卡类、信用卡类、还款委托类,以及最后导入、显示结果的主程序类。
还款委托类当作事件处理,委托类定义委托和事件以及自动还款方法
using System;
namespace LeeCodeText1
{
//信用卡类
class CreditCard
{
//还款金额
private int money;
//还款日
private string repaymentDate;
//还款金额的get,set方法
public void setMoney()
{
Console.Write("请输入本月需还款的金额:");
int money = Convert.ToInt32(Console.ReadLine());
this.money = money;
}
public int getMOney()
{
return money;
}
//还款日的get,set方法
public void setRepaymentDate()
{
Console.Write("还款日为:");
string repaymentDate = Console.ReadLine();
this.repaymentDate = repaymentDate;
}
public string getRepayment()
{
return repaymentDate;
}
//还款日还款方法
public void repayment(Bank bank, BankCard bankCard)
{
Console.WriteLine("今天是{0},还款日已到,此次需还款金额为 :{1}元,请还款。", this.repaymentDate, this.money);
int balance = bankCard.getBalance();
balance -= this.money;
bankCard.setBalance(balance);
while (balance < 0)
{
Console.WriteLine("对不起,当前余额为:{0},请充值", balance);
bankCard.setBalance(bank);
balance = bankCard.getBalance();
}
Console.WriteLine("恭喜您,还款成功,余额还剩:{0}", balance);
}
}
//银行卡类
class BankCard
{
//银行卡余额
private int balance;
//银行卡余额的get,set方法
public void setBalance(Bank bank)
{
int add = bank.add();
this.balance += add;
}
public void setBalance(int banlance)
{
this.balance = banlance;
}
public int getBalance()
{
return balance;
}
}
//银行类
class Bank
{
//充值余额
public int add()
{
Console.Write("请输入充值金额:");
int addMoney = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("恭喜您,成功缴纳人民币{0}元", addMoney);
return addMoney;
}
}
class AutomaticPayment
{
//定义委托
public delegate void delegateAutomaticPayment(Bank bank, BankCard bancard);
//定义事件
public event delegateAutomaticPayment eventAutomaticPayment;
//调用事件委托
public void auto(Bank bank, BankCard bankCard)
{
if (eventAutomaticPayment != null)
{
eventAutomaticPayment(bank, bankCard);
}
}
}
class Program
{
static void Main(string[] args)
{
//委托对象
AutomaticPayment objDelegate = new AutomaticPayment();
//银行实例对象
Bank bank = new Bank();
//银行卡实例对象
BankCard bankCard = new BankCard();
//信用卡实例对象
CreditCard creditCard = new CreditCard();
//设置银行卡本钱
//bankCard.setBalance(bank);
//设置还款金额
creditCard.setMoney();
//设置还款日期
creditCard.setRepaymentDate();
//订阅事件,还款方法
objDelegate.eventAutomaticPayment += new AutomaticPayment.delegateAutomaticPayment(creditCard.repayment);
objDelegate.auto(bank, bankCard);
}
}
}
实验结果:
实验心得:
① 本次实验将委托与事件应用到实践中,我更加熟悉了一些基本函数、语法的写法,及委托事件的用法。
② 在实验中,我遇到的困难主要是对委托事件基础语法结构不够熟悉,基本实现调用还有些生疏,有时会将该调用的函数或对象写混,因对于委托触发事件再执行操作的机制还不够熟练。
③ 还有就是对倒计时钟的实现相对困难,一开始写了部分代码但还是不能完全实现
仓库地址: