有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在程序中显示字符串的完整信息。反恐精英?

我有下面3个类,但由于某些原因,我无法显示交易类型和金额,也无法输出交易历史记录

CurrentAccount c1=新的CurrentAccount(“234555”,1000234,交易类型.存款);//粗体部分不显示

请参见下面的输出: 234545账号100透支系统。收藏。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透支。系统收藏。ArrayList事务历史记录。 234534账号12000透支。系统收藏。ArrayList事务历史记录。 234545账号100透支。系统收藏。ArrayList事务历史记录

你能帮我输出正确的信息吗


共 (1) 个答案

  1. # 1 楼答案

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