MikeMB35
MikeMB35

Reputation: 49

Can't change value of object from a class method

I made a class called BankAccount that has a method for depositing and withdrawing a certain balance from a given account. When I call the method in my main method it does not update the account with the new balance. The program is supposed to allow the user to select one of the three accounts and decide if they would like to deposit or withdraw from that bank account. Then it would ask them if they would like to make more transactions. If they wish to not make any more transactions they have the option of making transactions for a different day which would then present the updated data of each bank account, which mine currently doesn't do as the balance isn't being updated from either a deposit or withdraw. Any help would be greatly appreciated.

public class BankAccount
{

    public final int MIN_LENGTH = 2;
    public final int MAX_LENGTH = 40;
    public final String DEFAULT_NAME = "nobody";
    public final double MIN_BALANCE = 0.0;
    public final int DEFAULT_NUMBER = 0;
    public final int MIN_NUMBER = 0;
    public final int MAX_NUMBER = 999999;

    private double balance;
    private String owner;
    private int accountNumber;

    public BankAccount(double initBal, String name, int number)
    {
        balance = initBal;
        owner = name;
        accountNumber = number;
    }

    public BankAccount(double initBal, String name)
    {
        balance = initBal;
        owner = name;
        accountNumber = 45678;
    }

    public BankAccount(String name)
    {
        owner = name;
        balance = MIN_BALANCE;
        accountNumber = 34567;
    }

    public void deposit(double amount)
    {
        balance = balance + amount;
    }

    public void withdraw(double amount)
    {
        balance -= amount;
    }

    public void withdraw(double amount, double fee)
    {
        balance -= (amount + fee);
    }

    public double getBalance()
    {
        return balance;
    }

    public String getOwner()
    {
        return owner;
    }

    public int getAccountNumber()
    {
        return accountNumber;
    }

    public void setAccountNumber(int newAccountNumber)
    {
        accountNumber = newAccountNumber;
    }

    public void setOwner(String name)
    {
        owner = name;
    }

    public void display()
    {
        System.out.println("Account number: " + getAccountNumber());
        System.out.println("Account owner: " + getOwner());
        System.out.println("Account balance: " + getBalance() + "\n");
    }

    public void close()
    {
        owner = "CLOSED";
        balance = 0.0;
    }

    private static boolean isValidName(String name)
    {
        final int MIN_LENGTH = 2;
        final int MAX_LENGTH = 40;

        if(name.length() >= MIN_LENGTH && name.length() <= MAX_LENGTH && name != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private static boolean isValidBalance(double initBalance)
    {
        if(initBalance > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private static boolean isValidNumber(int acctNum)
    {
        final int MIN_NUMBER = 0;
        final int MAX_NUMBER = 999999;

        if(acctNum >= MIN_NUMBER && acctNum <= MAX_NUMBER)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}


import java.util.Scanner;

public class Transaction
{

    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        int accountChoice;
        double moneyAmount;
        String accountAction, transactionChoice = "nothing";
        BankAccount accountOne = new BankAccount(1000.0,"Matthew Johnson",12345);
        BankAccount accountTwo = new BankAccount(1000.0,"Sue Alexander",45678);
        BankAccount accountThree = new BankAccount(1000.0,"James William",34567);

        System.out.println("The following accounts are available:\n");
        do
        {
            accountOne.display();
            accountTwo.display();
            accountThree.display();

            System.out.print("Enter the number of the account you would like to access: ");
            accountChoice = keyboard.nextInt();

            System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
            accountAction = keyboard.nextLine();

            keyboard.nextLine();

            System.out.print("Enter the amount: ");
            moneyAmount = keyboard.nextDouble();

            if(accountChoice == 12345)
            {
                if(accountAction.equalsIgnoreCase("D"))
                {
                    accountOne.deposit(moneyAmount);
                }
                else if(accountAction.equalsIgnoreCase("W"))
                {
                    accountOne.withdraw(moneyAmount);
                }
            }
            else if(accountChoice == 45678)
            {
                if(accountAction.equalsIgnoreCase("D"))
                {
                    accountTwo.deposit(moneyAmount);
                }
                else if(accountAction.equalsIgnoreCase("W"))
                {
                    accountTwo.withdraw(moneyAmount);
                }
            }
            else if(accountChoice == 34567)
            {
                if(accountAction.equalsIgnoreCase("D"))
                {
                    accountThree.deposit(moneyAmount);
                }
                else if(accountAction.equalsIgnoreCase("W"))
                {
                    accountThree.withdraw(moneyAmount);
                }
            }

            keyboard.nextLine();
            System.out.println("More transactions? (y/n)");
            transactionChoice = keyboard.nextLine();

            if(transactionChoice.equalsIgnoreCase("Y"))
            {
                continue;
            }
            else if(transactionChoice.equalsIgnoreCase("N"))
            {
                System.out.println("Would you like to enter transactions for another day? (y/n)");
                transactionChoice = keyboard.nextLine();
                if(transactionChoice.equalsIgnoreCase("Y"))
                {
                    continue;
                }
                else if(transactionChoice.equalsIgnoreCase("N"))
                {
                    break;
                }
            }
            else
            {
                System.out.println("Invlid Input");
                break;
            }

        } while(!transactionChoice.equalsIgnoreCase("N"));
    }

}

Upvotes: 0

Views: 95

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

nextInt won't consume the newline which is left in the buffer, which is been captured by (your first) nextLine statement and is setting accountAction to an empty string. This is, generally, why I don't use this kind of work flow, and instead rely on nextLine and parsing the line separately, for example...

System.out.print("Enter the number of the account you would like to access: ");
accountChoice = new Scanner(keyboard.nextLine()).nextInt();

System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
accountAction = keyboard.nextLine();

System.out.print("Enter the amount: ");
moneyAmount = keyboard.nextDouble();

When you have these kinds of issues, make use of System.out.println to print out your variables, so you can see exactly what's going on, until you learn how to use the debugger ;)

You could also improve you code through the use of one or more loops, rather then relying on the if-else statement, which will reduce the amount of duplicate code, but that's beyond the scope of the question ;)

Upvotes: 1

Related Questions