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

This is what I'm calling in another class. I want to be able to deposit 100$ into this account.

Account acct1;

acct1 = new Account(500, "Joe", 1112);

What would I need to do in order to deposit into this account? I've tried different variations of this (below), but I'm confused as to what to do.

initBal = new deposit(100);

Help?

Marc B
  • 356,200
  • 43
  • 426
  • 500
Matt Farstad
  • 47
  • 2
  • 3
  • probably `this->balance += amount` first of all, you're treating your `deposit` method as object itself, which is probably wrong. – Marc B Sep 11 '14 at 16:29
  • `new deposit(100)` is incorrect in this case because `deposit` is a method, not a class. – takendarkk Sep 11 '14 at 16:30
  • 1
    side note: it's actually pretty dangerous to use `float` or `double` for money in most languages, including Java. these data types are prone to floating-point double rounding errors during division, and the errors are worse with bigger numbers (more money == more erratic math). it's recommended to store the money as cents rather than dollars, in a `long int`. – Woodrow Barlow Sep 11 '14 at 16:52

2 Answers2

1

The syntax for what it appears you want to do is:

Account acct1;                           //Creating a reference of type Account
acct1 = new Account(500, "Joe", 1112);   //Instantiating a new Account object, 
                                         //giving a reference to that object to acct1
acct1.deposit(100);                      //Calling the deposit method in class Account
                                         //On the object referred to by acct1

More generally, to call a method on an object (of a type that has that method):

<object_reference>.<method_name>(<parameter 1>, <parameter 2>, ...);
Mshnik
  • 7,032
  • 1
  • 25
  • 38
0

Make sure your Account object stores your initial balance and that your deposit method increase it:

Example:

public class Account{

    private Double balance;

    public Account(Double initBalance, String name, int number){
        this.balance = initBalance;
    }

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

}

Then when you create an instance of Account acct1 = new Account(500, "Joe", 1112);

Then, to increase the balance of your account, you have to call the deposit method that is inside your instance of Account

 acct1.deposit(amount)
Paco Lagunas
  • 320
  • 1
  • 13