功能说明
账户信息总览展示储蓄卡和信用卡信息及还款信息(如订阅自动还款服务,在还款日触发自动还款);
储蓄卡和信用卡可完成信息展示、存取款;
其中信用卡还可完成设置还款日、贷款、开通/取消自动还款服务、手动还款功能。
源码展示
客户类
//客户类
class Client
{
// 字段:姓名、账户
internal string name;
internal Accounts accounts;
// 客户类构造函数
public Client(string nam, Accounts acco)
{
name = nam;
accounts = acco;
}
}
账户类
//账户类
class Accounts
{
// 字段:总金额、储蓄卡、信用卡
internal float assets = 0;
internal DepositCard depo;
internal CreditCard cred;
// 账户类构造函数
internal Accounts(DepositCard deposit, CreditCard credit)
{
depo = deposit;
cred = credit;
}
// 计算总金额
internal void CalAsserts()
{
assets = depo.balance + cred.balance;
}
// 账户信息展示
internal void Display(Client client, Delegate repayDelegate)
{
Console.WriteLine("It is {0}. ", DateTime.Now);
RepaymentStatus(client, repayDelegate);
depo.Display();
cred.Display();
Console.WriteLine("\n------SUMMARY------\n");
CalAsserts();
Console.WriteLine("Assets: " + assets);
Console.WriteLine("Debt: " + client.accounts.cred.debt);
}
// 还款状态
internal void RepaymentStatus(Client client, Delegate repayDelegate)
{
if (client.accounts.cred.debt != 0)
{
if (DateTime.Today.Day == cred.statement)
{
Console.WriteLine("You Need to Pay Your Debt.");
//可触发事件的处理方法
repayDelegate.Repay(client);
}
else
{
int duration;
duration = Math.Abs(DateTime.Today.Day - cred.statement);
if (DateTime.Today.Day < cred.statement)
{
Console.WriteLine("{0} Day(s) Before This Month's Due Day.\n", duration);
}
else
{
Console.WriteLine("You Have Over Due for {0} Day(s) !\n", duration);
}
}
}
else
{
Console.WriteLine("You Don't Have Debt This Month.");
}
}
// 自动还款,信用卡还款优先,储蓄卡其次
internal void AutoRepay(Client client)
{
if (cred.balance >= cred.debt)
{
cred.AutoRepay();
Console.WriteLine("-REPAYMENT COMPLETE!-");
}
else
{
float temp = cred.debt - cred.balance;
cred.debt = cred.balance;
cred.AutoRepay();
cred.debt = temp;
if (depo.balance >= cred.debt)
{
depo.AutoRepay(client);
Console.WriteLine("-REPAYMENT COMPLETE!-");
}
else
{
temp = cred.debt - depo.balance;
cred.debt = depo.balance;
depo.AutoRepay(client);
cred.debt = temp;
Console.WriteLine("-BOTH DEPOSIT CARD AND CREDIT CARD BALACE INSUFFICIENT!-\n");
Console.WriteLine("Credit Card Debt: {0}\nCredit Card Statement: {1}", cred.debt, cred.statement);
Console.WriteLine("\nPLEASE TOP-UP!");
}
}
}
}
存取款类
//存取款类
class TopWithdraw
{
// 存款
internal void TopUp(ref float balance)
{
Console.WriteLine("PLEASE ENTER THE AMOUNT YOU WANT TO TOP-UP: ");
int deposit = int.Parse(Console.ReadLine());
balance += deposit;
Console.WriteLine("Deposit: " + deposit);
}
// 取款
internal void Withdraw(ref float balance)
{
Console.WriteLine("PLEASE ENTER THE AMOUNT YOU WANT TO WITHDRAW: ");
int withdrawal = int.Parse(Console.ReadLine());
balance -= withdrawal;
Console.WriteLine("Withdrawal: " + withdrawal);
}
}
储蓄卡类
//储蓄卡类
class DepositCard : TopWithdraw
{
// 字段:余额
internal float balance;
// 储蓄卡类构造函数
internal DepositCard(float bala)
{
balance = bala;
}
// 储蓄卡信息展示
internal void Display()
{
Console.WriteLine("\n------Deposit Card------\n");
Console.WriteLine("Deposit Card Balance: " + balance);
}
// 储蓄卡自动还款
internal void AutoRepay(Client client)
{
Console.WriteLine("\nDEPOSIT CARD REPAYING...");
balance -= client.accounts.cred.debt;
Console.WriteLine("Today is Due Day({0}):\n Repayment: {1}\n Deposit Card Balance: {2}", DateTime.Today.Day, client.accounts.cred.debt, balance);
}
// 储蓄卡存款
internal void TopUp()
{
TopUp(ref balance);
Display();
}
// 储蓄卡取款
internal void Withdraw()
{
Withdraw(ref balance);
Display();
}
}
信用卡类
//信用卡类
class CreditCard : TopWithdraw
{
// 字段:还款日、负债、余额
internal int statement;
internal float debt;
internal float balance;
// 信用卡类构造函数
internal CreditCard(int stat, float dbt, float bala)
{
statement = stat;
debt = dbt;
balance = bala;
}
// 设置还款日
internal void SetStatement()
{
Console.WriteLine("PLEASE SET STATEMENT OF THE MONTH: ");
do
{
statement = int.Parse(Console.ReadLine());
} while (statement < 1 || statement > 31);
}
// 借贷
internal void Loan()
{
Console.WriteLine("PLEASE ENTER THE AMOUNT YOU WANT TO LOAN: ");
int loan = int.Parse(Console.ReadLine());
debt += loan;
Console.WriteLine("Loan: " + loan);
Display();
}
// 信用卡信息展示
internal void Display()
{
Console.WriteLine("\n------Credit Card------\n");
Console.WriteLine("Credit Card Balance: {0}\nCredit Card Debt: {1}\nCredit Card Statement: {2}", balance, debt, statement);
}
// 信用卡自动还款
internal void AutoRepay()
{
Console.WriteLine("\nCREDIT CARD REPAYING...");
balance -= debt;
debt = 0;
Console.WriteLine("Today is Due Day({0}):\n Repayment: {1}\n Credit Card Balance: {2}", DateTime.Today.Day, debt, balance);
}
// 信用卡存款
internal void TopUp()
{
TopUp(ref balance);
Display();
}
// 信用卡取款
internal void Withdraw()
{
Withdraw(ref balance);
Display();
}
// 开通/取消自动还款服务
internal void AutoRepayService(Client client, Delegate repayDelegate)
{
Console.WriteLine("Do You Want to Subscribe Auto Repay Service?\nY for Yes, N for No.\nPLEASE ENTER: ");
string choice = Console.ReadLine();
if (choice == "Y")
{
// 订阅事件AutoRepay,为委托方法注册方法accounts.AutoRepay,其签名需同委托方法RepaymentDelegate
repayDelegate.AutoRepay += new Delegate.RepaymentDelegate(client.accounts.AutoRepay);
Console.WriteLine("-SERVICE SUBSCRIBED!-");
}
else if(choice == "N")
{
// 取消订阅
repayDelegate.AutoRepay -= new Delegate.RepaymentDelegate(client.accounts.AutoRepay);
Console.WriteLine("-SERVICE UNSUBSCRIBED!-");
}
}
}
菜单类
//菜单类
class Menu
{
// 接受并判断用户输入的选项是否合法
internal int GetChoice(int lower, int upper)
{
int choice;
do
{
Console.WriteLine("PLEASE ENTER YOUR CHOICE: ");
choice = int.Parse(Console.ReadLine());
} while (choice < lower || choice > upper);
Console.WriteLine("-----------------------------");
return choice;
}
// 主菜单
internal void HomePage(Client client, Delegate repayDelegate)
{
int choice;
Console.WriteLine("Hello {0}, what can we help you with?\n", client.name);
Console.WriteLine(" (1) Accounts Status\n");// 账户信息及还款状态总览,如订阅自动还款服务,到还款日自动触发自动还款
Console.WriteLine(" (2) Deposit Card\n");// 储蓄卡
Console.WriteLine(" (3) Credit Card\n");// 信用卡
Console.WriteLine(" (0) EXIT\n");// 退出程序
choice = GetChoice(0, 3);
switch (choice)
{
case 1:
client.accounts.Display(client, repayDelegate);
break;
case 2:
DepoMenu(client, repayDelegate);
break;
case 3:
CretMenu(client, repayDelegate);
break;
case 0:
Application.Exit();
break;
}
Console.WriteLine("-----------------------------");
Console.WriteLine("PRESS ANY KEY TO CONTINUE... ");
Console.ReadKey(true);
HomePage(client, repayDelegate);
}
// 储蓄卡菜单
internal void DepoMenu(Client client, Delegate repayDelegate)
{
int choice;
Console.WriteLine("\n------Deposit Card------\n");
Console.WriteLine(" (1) Status\n");// 储蓄卡信息
Console.WriteLine(" (2) Top-up\n");// 储蓄卡存款
Console.WriteLine(" (3) Withdraw\n");// 储蓄卡取款
Console.WriteLine(" (0) BACK\n");// 回到上一页面
choice = GetChoice(0, 3);
switch (choice)
{
case 1:
client.accounts.depo.Display();
break;
case 2:
client.accounts.depo.TopUp();
break;
case 3:
client.accounts.depo.Withdraw();
break;
case 0:
HomePage(client, repayDelegate);
break;
}
Console.WriteLine("-----------------------------");
Console.WriteLine("PRESS ANY KEY TO CONTINUE... ");
Console.ReadKey(true);
Console.WriteLine("-----------------------------");
DepoMenu(client, repayDelegate);
}
// 信用卡菜单
internal void CretMenu(Client client, Delegate repayDelegate)
{
int choice;
Console.WriteLine("\n------Credit Card------\n");
Console.WriteLine(" (1) Status\n");// 信用卡信息
Console.WriteLine(" (2) Top-up\n");// 信用卡存款
Console.WriteLine(" (3) Withdraw\n");// 信用卡取款
Console.WriteLine(" (4) Set Statement\n");// 设置信用卡还款日
Console.WriteLine(" (5) Loan\n");// 信用卡贷款
Console.WriteLine(" (6) Auto Repay Service\n");// 开通/取消自动还款服务
Console.WriteLine(" (7) Manual Repay\n");// 手动还款
Console.WriteLine(" (0) BACK\n");// 回到上一页面
choice = GetChoice(0, 7);
switch (choice)
{
case 1:
client.accounts.cred.Display();
break;
case 2:
client.accounts.cred.TopUp();
break;
case 3:
client.accounts.cred.Withdraw();
break;
case 4:
client.accounts.cred.SetStatement();
break;
case 5:
client.accounts.cred.Loan();
break;
case 6:
client.accounts.cred.AutoRepayService(client,repayDelegate);
break;
case 7:
client.accounts.AutoRepay(client);
break;
case 0:
HomePage(client, repayDelegate);
break;
}
Console.WriteLine("-----------------------------");
Console.WriteLine("PRESS ANY KEY TO CONTINUE... ");
Console.ReadKey(true);
Console.WriteLine("-----------------------------");
CretMenu(client, repayDelegate);
}
}
委托类
//委托类
class Delegate
{
// 定义委托对象RepaymentDelegate方法,返回值类型为void,形参Client
internal delegate void RepaymentDelegate(Client client);
// 定义RepaymentDelegate委托方法下的事件AutoRepay
internal event RepaymentDelegate AutoRepay;
// 定义事件的处理方法,签名无需同RepaymentDelegate委托方法
internal bool Repay(Client client)
{
// 触发条件,事件AutoRepay非空,且当日为还款日
if (AutoRepay != null && DateTime.Today.Day == client.accounts.cred.statement)
{
// 触发内容
Console.WriteLine("AUTO REPAYMENT INITIATING ...");
// 与事件同名,签名同RepaymentDelegate委托方法
AutoRepay(client);
return true;
}
return false;
}
}
主程序
class Program
{
static void Main()
{
// 初始化用户信息
DepositCard depo = new DepositCard(10000);
CreditCard cred = new CreditCard(8, 5000, 100);
Accounts accounts = new Accounts(depo, cred);
Client client = new Client("Hazel", accounts);
// 委托类实例化
Delegate repayDelegate = new Delegate();
// 进入菜单
Menu menu = new Menu();
menu.HomePage(client, repayDelegate);
}
}
功能测试
测试用例
用户名:Hazel
储蓄卡初始金额:10000
信用卡初始金额:100
信用卡初始负债:5000
信用卡初始还款日:8
1 账户信息总览
1.1 未至还款日
1.2.1 还款日-未订阅自动还款服务
1.2.2 还款日-订阅自动还款服务
1.3 逾期
1.4 无欠款
2 银行卡各项功能-仅以 信用卡 为例
2.1 银行卡信息展示
2.2 银行卡存款
2.3 银行卡取款
2.4 设置信用卡还款日
2.5 信用卡贷款
2.6 信用卡开通/取消自动还款服务
触发后结果见 1.2.2
2.7 信用卡手动还款
心得体会
通过本次作业,厘清了Event和Dalegate的区别与联系:Event和Dalegate必须被声明为public;Dalegate是一种类型,相当于指向函数的指针,注册/注销的函数的签名(返回值及形参)须符合定义的委托方法;Event是一种对象,基于Dalegate产生的,不可以使用“=”赋值,也不可在类外部直接调用。
作为银行系统,在安全性上有所欠缺。