我有以下3个类但由于某种原因我无法显示交易类型和金额,我也无法输出交易历史
CurrentAccount c1 = new CurrentAccount(" 234555",1000,234, TransactionType.Deposit); //不显示粗体部分
见下面的输出: 234545帐号100透支。 System.Collections.ArrayList交易记录。
如何更正我的课程以正确显示交易历史记录?
请参阅以下完整课程
abstract class BankAccount
{
protected string AccountNumber { get; } // read property
protected double Balance { get; set; } //read and write property
public BankAccount(string _accountNumber)
{
this.AccountNumber = _accountNumber;
this.Balance = 0;
}
public virtual void MakeDeposit(double amount)
{
Balance = Balance + amount;
}
public virtual void MakeWithdraw(double amount)
{
Balance = Balance - amount;
}
}
}
class CurrentAccount : BankAccount
{
private double OverdraftLimit { get; } // read only property
public ArrayList TransactionHistory = new ArrayList();
public CurrentAccount(string AccountNumber, double OverdraftLimit, double amount, TransactionType type) : base(AccountNumber)
{
this.OverdraftLimit = OverdraftLimit;
TransactionHistory.Add(new AccountTransaction(type, amount));
}
public override void MakeDeposit(double amount) // override method
{
Balance += amount;
TransactionHistory.Add(new AccountTransaction(TransactionType.Deposit, amount));
}
public override void MakeWithdraw(double amount)
{
if (Balance + OverdraftLimit > 0)
{
Balance -= amount;
TransactionHistory.Add(new AccountTransaction(TransactionType.Withdrawal, amount));
}
else
{
throw new Exception("Insufficient Funds");
}
}
public override string ToString()
{
// print the transaction history too
return AccountNumber + " account number " + OverdraftLimit + " overdraft." + TransactionHistory + " Transaction history.";
}
}
}
{
enum TransactionType
{
Deposit, Withdrawal
}
class AccountTransaction
{
public TransactionType type { get; private set; } // deposit/withdrawal
private double Amount { get; set; }
public AccountTransaction (TransactionType type, double _amount)
{
this.type = type;
this.Amount = _amount;
}
public override string ToString()
{
return "type" + type + "amount" + Amount;
}
}
}
class Program
{
static void Main(string[] args)
{
CurrentAccount c1 = new CurrentAccount("234555",1000, 234, **TransactionType.Deposit)**; // this part is not displayed
CurrentAccount c2 = new CurrentAccount("234534", 12000, 345, **TransactionType.Withdrawal)**; // this part is not displayed
CurrentAccount c3 = new CurrentAccount("234545", 100, 456, **TransactionType.Withdrawal)**; // this part is not displayed
Console.WriteLine(c1);
Console.WriteLine(c2);
Console.WriteLine(c3);
}
}
}
控制台的输出: 234555帐号1000 overdraft.System.Collections.ArrayList交易记录。 234534帐号12000 overdraft.System.Collections.ArrayList交易记录。 234545帐号100 overdraft.System.Collections.ArrayList交易记录。
请帮我输出正确的信息。
答案 0 :(得分:0)
我不知道为什么会发生这种情况,因为你有字符串方法覆盖。我唯一要做的就是你需要在银行类中覆盖tostring方法,因为你是从bank继承的。尝试一下,告诉我这是否有效。