0

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
Community
  • 1
  • 1
Elliander
  • 503
  • 1
  • 9
  • 19

2 Answers2

0

Have you tried:

public static List<String> transactionList = new ArrayList<>();
public static List<String> accountNumberList = new ArrayList<>();

Also;

The withdraw method should throw an exception if 'amount' is greater than 'CurrentBalance'.

Liam Ferris
  • 1,786
  • 2
  • 18
  • 38
  • Declaring those ArrayLists at the class level doesn't make a difference. Why would I throw an exception if amount is greater? That's just to differentiate between a positive and negative transaction. If it's negative it subtracts and if it's positive it adds. – Elliander Oct 02 '15 at 10:43
0

I found my answer. My code has three problems:

In Main.java I should use:

Account account = bank.getAccountWithNumber(accountNumberInt);

Instead of what I was using:

bank.getAccountWithNumber(accountNumberInt);

I also had to change this snippet from the Main.java class:

                        if (transactionDouble < 0)
                            {
                                account.withdraw(transactionDouble);
                            }
                        else
                            {
                                account.deposit(transactionDouble);

                            }
                     }

To reflect the change in how I was loading the account numbers. This change alone caused an index error, but was fixed by changing the Bank.java class to either:

    public Account getAccountWithNumber(int accountNumber)
    {   
     for(Account account : accounts) {
      if(account.getAccountNumber() == accountNumber) {
       return account;
      }
     }
        return null;  
    }

or:

    public Account getAccountWithNumber(int accountNumber)
    {     
         return accounts.stream().filter(a -> a.getAccountNumber() == accountNumber).findFirst().get();
    }

Either works, but not what I was using:

public Account getAccountWithNumber(int accountNumber)
    {   
        return accounts.get(accountNumber);  
    }

After making all of these changes everything works perfectly. However, cleaning up my code this worked better than my if logic:

        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());
        }

I also had to change an N == variable to N.equals(variable)

Elliander
  • 503
  • 1
  • 9
  • 19