【问题标题】:how could I display the full information to string in the program.cs?如何在 program.cs 中将完整信息显示为字符串?
【发布时间】:2018-05-03 02:16:16
【问题描述】:

我有以下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透支。System.Collections.ArrayList 交易记录。

请你帮我输出正确的信息。

【问题讨论】:

  • 请阅读How to Ask并正确格式化您的代码。

标签: java c# arraylist transactions bank


【解决方案1】:

我不知道为什么会发生这种情况,因为您有字符串方法覆盖。我唯一想到的是你需要重写银行类中的 tostring 方法,因为你是从银行继承的。试试看,告诉我这是否有效。

【讨论】:

    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    相关资源
    最近更新 更多