Jump to content

Adding User input to bank balance

Hey guys, I'm practicing some java stuff and I've made a blueprint along with my account class and i've managed to make my own methods for printing and adding to the balance, but I'm trying to add to the balance using user input with the Scanner utility. All i'm able to print out after inputting a double value is the original unchanged balance. any help would be appreciated. 

//theres my account class blueprint

import java.util.Scanner;
public class ChequingAccount {

    private String first;
    private String last;
    public double balance;

    public ChequingAccount(String first, String last, double balance)
    {
        this.first = first;
        this.last = last;
        this.balance = balance;
    }

    public void addToBalance(double userNumber)
    {
        this.balance=balance+userNumber;
    }

    public void getBalance()
    {
        System.out.println(balance);
    }



   // public void userInput(double usernumber)
    //{
      //  this.balance=balance+usernumber;
    //}

}
//and theres my Instance I'm trying to add to with the user input

import java.util.Scanner;
public class TestChequingAccount {

    public static void main(String[]args)
    {
        ChequingAccount makaylasAccount=new ChequingAccount("Makayla", "Musgrove", 320.50);

        System.out.print("Your Balance is ");

        makaylasAccount.getBalance();

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter your deposit ammount");
        double userNumber = reader.nextDouble();

        //makaylasAccount.addToBalance();


        makaylasAccount.getBalance();
    }




}

 

Link to comment
Share on other sites

Link to post
Share on other sites

why is 

makaylasAccount.addToBalance();

commented out?

Link to comment
Share on other sites

Link to post
Share on other sites

I would suggest trying to break down your desired state in to logical steps as you would've identified the issue quite easily, you're not adding the input to the balance.

 

While we're at it I'll let you know where else you can improve :)

  1. The variables in the ChequingAccount class are not descriptive enough, firstName/lastName > first/last.
  2. The double variable in ChequingAccount should be private.
  3. You can use += to add within the addToBalanceMethod in ChequingAccount.
  4. Getters should only return data, so getBalance in ChequingAccount should only return the value of balance.
  5. If you are using Java 10 you can use var when a type can be inferred by the assignment (this is more personal preference but results in cleaner code)
    1. Example: var makaylasAccount = new ChequingAccount("Makayla", "Musgrove", 320.50);
Link to comment
Share on other sites

Link to post
Share on other sites

import java.util.Scanner;
public class ChequingAccount {

    private String firstName;
    private String lastName;
    private double balance;

    public ChequingAccount(String firstName, String lastName, double balance)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.balance = balance;
    }

    public void addToBalance(double increaseBalanceBy)
    {
        this.balance += increaseBalanceBy;
    }

    public void getBalance()
    {
        return this.balance;
    }
}


import java.util.Scanner;
public class TestChequingAccount {

    public static void main(String[]args)
    {
        var makaylasAccount = new ChequingAccount("Makayla", "Musgrove", 320.50);

        System.out.println("Your Balance is: " + makaylasAccount.getBalance());

        var reader = new Scanner(System.in);
		
        System.out.println("\nEnter your deposit ammount: ");
        var depositAmount = reader.nextDouble();

        makaylasAccount.addToBalance(depositAmount);

        System.out.println("\nYour Balance is now: " + makaylasAccount.getBalance());
    }
}

I don't have JDK installed and haven't used Java in about 10 years so I can't test it but that should work ^. If you don't have Java 10 you'll need to replace "var makaylasAccount" with "ChequingAccount makaylasAccount" and "var reader" with "Scanner reader".

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks so much, I think i get it now. I wasn't too far off. sorry about the late reply, i decided to sleep on it

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×