我正在尝试添加或减少余额。我在Main()类中创建了一个类级别的bank对象:
protected static Bank bank = new Bank();
我成功构建了帐户,客户和余额列表,并能够正确显示它们。但是,从余额中添加或减去似乎永远不会起作用。主要是因为它是一种尝试从静态上下文访问的非静态方法。
如果我将其更改为静态,我会遇到很大的不准确之处,因为它似乎不再容纳多个不同的实例,但我似乎无法找到一种方法来处理我需要的实例。
以下是Main中正在开展工作的课程:
public void processTransactionsInFile() throws FileNotFoundException
{
final int ACCOUNT_NUMBER_COUNT = 1;
final int TRANSACTION_COUNT = 0;
final int ACCOUNT_NUMBER = 1;
final int TRANSACTION = 0;
List<String> transactionList = new ArrayList<>();
List<String> accountNumberList = new ArrayList<>();
try (Scanner transactions = new Scanner(new File(INPUT_TRANSACTIONS_FILE)))
{
do {
String[] temp1 = transactions.next().split(",");
if (ACCOUNT_NUMBER_COUNT == ACCOUNT_NUMBER)
{
transactionList.add(temp1[ACCOUNT_NUMBER]);
}
if (TRANSACTION_COUNT == TRANSACTION)
{
accountNumberList.add(temp1[TRANSACTION]);
}
} while (transactions.hasNext());
}
for(int i = 0; i < transactionList.size(); i++)
{
String transactionString = transactionList.get(i);
double transactionDouble = Double.parseDouble(transactionString);
String accountNumberString = accountNumberList.get(i);
int accountNumberInt = Integer.parseInt(accountNumberString);
bank.getAccountWithNumber(accountNumberInt);
if (transactionDouble < 0)
{
// This doesn't work
Account.withdraw(transactionDouble);
}
else
{
Account.deposit(transactionDouble);
}
}
这就是账户
中的对应物public void withdraw(double amount)
{
currentBalance = currentBalance - amount;
}
public void deposit(double amount)
{
currentBalance = currentBalance + amount;
}
我也是这样尝试的:
public double withdraw(double amount)
{
setCurrentBalance(this.getCurrentBalance() - amount);
return currentBalance;
}
public double deposit(double amount)
{
this.setCurrentBalance(this.getCurrentBalance() + amount);
return currentBalance;
}
在银行类我有这个我试图用来加载我想要使用的实例:
public Account getAccountWithNumber(int accountNumber)
{
return accounts.get(accountNumber);
}
这个问题与this one非常相似,只是我有多个与我合作的不同对象,而且似乎没有任何解决方案适合我。
编辑:以下是要加载的文本文件的内容。第一个数字是帐号,第二个数字是交易:
10100,500.00
10101,-250.00
20100,450.00
20101,-100.00
10102,-300.00
20103,1000.00
答案 0 :(得分:0)
你试过了吗?
public static List<String> transactionList = new ArrayList<>();
public static List<String> accountNumberList = new ArrayList<>();
也;
如果'amount'大于'CurrentBalance',则withdraw方法应抛出异常。
答案 1 :(得分:0)
我找到了答案。我的代码有三个问题:
在Main.java中我应该使用:
Account account = bank.getAccountWithNumber(accountNumberInt);
而不是我正在使用的东西:
bank.getAccountWithNumber(accountNumberInt);
我还必须从Main.java类更改此代码段:
if (transactionDouble < 0)
{
account.withdraw(transactionDouble);
}
else
{
account.deposit(transactionDouble);
}
}
反映我加载帐号的方式的变化。仅此更改导致索引错误,但通过将Bank.java类更改为:
来解决此问题 public Account getAccountWithNumber(int accountNumber)
{
for(Account account : accounts) {
if(account.getAccountNumber() == accountNumber) {
return account;
}
}
return null;
}
或:
public Account getAccountWithNumber(int accountNumber)
{
return accounts.stream().filter(a -> a.getAccountNumber() == accountNumber).findFirst().get();
}
要么有效,要么不是我用的东西:
public Account getAccountWithNumber(int accountNumber)
{
return accounts.get(accountNumber);
}
完成所有这些更改后,一切都很完美。但是,清理我的代码比使用if逻辑更好:
try (Scanner transactions = new Scanner(new File(INPUT_TRANSACTIONS_FILE)))
{
do {
String[] temp1 = transactions.next().split(",");
transactionList.add(temp1[ACCOUNT_NUMBER]);
accountNumberList.add(temp1[TRANSACTION]);
} while (transactions.hasNext());
}
我还必须将N ==变量更改为N.equals(变量)