📄 bankaccount.cs
字号:
using System;
using System.Collections;
namespace Banking
{
public class BankAccount
{
private long accNo;
private decimal accBal;
private AccountType accType;
private Queue tranQueue = new Queue();
private static long nextNumber = 123;
// Constructors
internal BankAccount()
{
this.accNo = NextNumber();
this.accBal = 0;
this.accType = AccountType.Checking;
}
internal BankAccount(AccountType accType)
{
this.accNo = NextNumber();
this.accBal = 0;
this.accType = accType;
}
internal BankAccount(decimal balance)
{
this.accNo = NextNumber();
this.accBal = balance;
this.accType = AccountType.Checking;
}
internal BankAccount(AccountType accType, decimal balance)
{
this.accNo = NextNumber();
this.accBal = balance;
this.accType = accType;
}
// Finalizer and Dispose methods
~BankAccount()
{
foreach (BankTransaction tran in tranQueue) {
tran.Dispose();
}
}
internal void Dispose()
{
this.Finalize();
GC.SuppressFinalize(this);
}
// Operators
public static bool operator==(BankAccount acc1, BankAccount acc2)
{
if ((acc1.accNo == acc2.accNo) && (acc1.accType == acc2.accType) && (acc1.accBal == acc2.accBal)) {
return true;
} else {
return false;
}
}
public static bool operator!=(BankAccount acc1, BankAccount acc2)
{
return !(acc1 == acc2);
}
// Other Methods
public override bool Equals(Object acc1)
{
return this == acc1;
}
public override string ToString()
{
string retVal = "Number: " + this.accNo + "\tType: ";
retVal += (this.accType == AccountType.Checking) ? "Checking" : "Deposit";
retVal += "\tBalance: " + this.accBal;
return retVal;
}
public override int GetHashCode()
{
return (int)this.accNo;
}
public bool Withdraw(decimal amount)
{
bool sufficientFunds = accBal >= amount;
if (sufficientFunds) {
accBal -= amount;
BankTransaction theTran = new BankTransaction(-amount); // Negative amount, this a withdrawl
tranQueue.Enqueue(theTran);
}
return sufficientFunds;
}
public decimal Deposit(decimal amount)
{
accBal += amount;
BankTransaction theTran = new BankTransaction(amount);
tranQueue.Enqueue(theTran);
return accBal;
}
public long Number()
{
return accNo;
}
public decimal Balance()
{
return accBal;
}
public string Type()
{
return accType.Format();
}
public Queue Transactions()
{
return tranQueue;
}
private static long NextNumber()
{
return nextNumber++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -