I am trying to add to or subtract from a balance. I created a class level bank object in the Main() class:
protected static Bank bank = new Bank();
and I successfully built the list of accounts, customers, and balances and was able to display them correctly. However, getting adding or subtracting from the balances never seems to work. Mostly because it's a non static method trying to access from a static context.
If I change it to static I get major inaccuracies because it doesn't seem to hold multiple different instances anymore, but I can't seem to find a way to work with just the instance I need.
Here's the class in Main that's doing the work:
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);
}
}
And here's it's counterpart in Account
public void withdraw(double amount)
{
currentBalance = currentBalance - amount;
}
public void deposit(double amount)
{
currentBalance = currentBalance + amount;
}
and I also tried it this way:
public double withdraw(double amount)
{
setCurrentBalance(this.getCurrentBalance() - amount);
return currentBalance;
}
public double deposit(double amount)
{
this.setCurrentBalance(this.getCurrentBalance() + amount);
return currentBalance;
}
In the bank class I have this which I am trying to use to load the instance I want to work with:
public Account getAccountWithNumber(int accountNumber)
{
return accounts.get(accountNumber);
}
This question is very similar to this one, except that I have multiple different objects I am working with and none of the solutions there seem to work for me.
EDIT: Here's the contents of the text file being loaded. The first number is the account number and the second number is the transaction:
10100,500.00
10101,-250.00
20100,450.00
20101,-100.00
10102,-300.00
20103,1000.00