我已经寻找解决方案,但我仍然不明白该怎么做。这个程序应该模仿一个自动取款机,用支票从余额中减去,用存款增加钱。
我有这个父类:
public class Transaction
{
private int transNumber;
private int transId;
private double transAmt;
public Transaction(int number, int id, double amount)
{
transNumber = number;
transId = id;
transAmt = amount;
}
public int getTransNumber()
{
return transNumber;
}
public int getTransId()
{
return transId;
}
public double getTransAmount()
{
return transAmt;
}
}
我有一个数组列表(transList),它为每个提款/存款存储交易对象。
子类是:
public class Check extends Transaction
{
private int checkNumber; // check number for each check transaction
public Check(int tId, double tAmt, int tCount, int checkNumber) {
super(tCount, tId, tAmt);
this.checkNumber = checkNumber;
}
public int getCheckNumber() {
return checkNumber;
}
public void setCheckNumber(int checkNumber) {
this.checkNumber = checkNumber;
}
}
如果用户要求,我需要做的是在 JOptionPane 中打印出支票号码。
我主要有:
checkNumStr = JOptionPane.showInputDialog("Enter check number:");
checkNum = Integer.parseInt(checkNumStr);
checkAmtStr = JOptionPane.showInputDialog("Eneter check amount:");
checkAmt = Double.parseDouble(checkAmtStr);
Check c = new Check(1, checkAmt, acctCheck.gettransCount(), checkNum);
acctCheck.addTrans(c);
我不确定我是否已经足够清楚了。我以前没有在这里问过问题。我需要知道的是如何从父类 Transaction 的对象数组列表中调用 Check.getCheckNumber()。
编辑:transList 位于另一个类中:支票账户:
public class CheckingAccount extends Account
{
private double totalServiceCharge;
private static ArrayList<Transaction> transList;
// keeps a list of Transaction objects for the account
private int transCount = 0;
// the count of Transaction objects and used as the ID for each transaction
NumberFormat fmt = NumberFormat.getCurrencyInstance();
public CheckingAccount()
{
transList = new ArrayList<Transaction>();
totalServiceCharge = 0;
}
public double getServiceCharge()
{
return totalServiceCharge;
}
public void setServiceCharge(double currentServiceCharge)
{
totalServiceCharge += currentServiceCharge;
}
// adds a transaction object to the transList
public void addTrans(Transaction newTrans)
{
transList.add (newTrans);
transCount++;
}
//returns the current value of transCount;
public int gettransCount()
{
return transCount;
}
}