How to make calculation inside class field in Java?

Viewed 2927

So I created Saving class, created also setters and getters. Now I need u method, which will calculate the total amount of deposits.

public class Saving {

      private double deposits;

      private double totalAmountOfDeposits;

      public double getDeposits() 
             {                      
             return deposits;
             }

      public void setDeposits(double deposits) 
             { 
             this.deposits = deposits + deposits;
             }

      public double getTotalAmountOfDeposits()
             {
             double total = 0;
             return total = total + deposits;
             }
}

When I use this class in the program I got a wrong calculation. The program just add first value of deposit to the first value.

import java.util.Scanner;

public class SavingDemo {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        Saving save = new Saving();

        System.out.println("Deposit amount");
        double depositeAmount = input.nextDouble();
        save.setDeposits(depositeAmount);
        System.out.println("Deposit amount");
        double depositeAmount2 = input.nextDouble();
        save.setDeposits(depositeAmount);
        System.out.println("Deposit amount");
        double depositeAmount3 = input.nextDouble();
        save.setDeposits(depositeAmount);
        System.out.println("The total amount has been deposited is " + save.getTotalAmountOfDeposits());
    }
}

And here is the output:

Deposit amount
12
Deposit amount
34
Deposit amount
56
The total amount has been deposited is 24.0

As you can see its just added 12 to 12. Just want to mention that I'm totally new in programming. Les than a month.

3 Answers

I see two problems in your code. Take a look at the commented line. The reason you are seeing 12 + 12 is because that is exactly what you are instructing the JVM to do.

System.out.println("Deposit amount");
double depositeAmount = input.nextDouble();
save.setDeposits(depositeAmount);
System.out.println("Deposit amount");
double depositeAmount2 = input.nextDouble();
save.setDeposits(depositeAmount); // <= adds the wrong variable
System.out.println("Deposit amount");
double depositeAmount3 = input.nextDouble();
save.setDeposits(depositeAmount); // <= adds the wrong variable
System.out.println("The total amount has been deposited is " + save.getTotalAmountOfDeposits());

Secondly, it looks like you may have a design flaw in your implementation of the Saving class.

You'll want to brush up on variable scope

If you take a look at your implementation on your total:

public double getTotalAmountOfDeposits()
         {
         double total = 0;
         return total = total + deposits;
         }

You have the total starting at 0 every time this method getTotalAmountOfDeposits() is called. the total variable in this method is local to it's method. So what you currently have is a method variable

You'll want to do some research into class variable. This will maintain that the instance of the object will have this variable assigned through the life cycle of the instantiated object.

When you have variables of the same name, you can get the instance variable with this keyword.

So when dealing with your setter

 public void setSomething(double something) {
     this.something // class variable
     something // method variable
 }

If you want your object to maintain state, you can set it on your object itself, and have your set deposit modify that state. Some pseudo code to get you moving forward.

public class Saving {
    private double totalAmountOfDeposits; // you can modify this value with public methods

    public void setDeposit(_) {
        // Setter implementation
        // increment totalAmountOfDeposits;

    public double getTotalAmountOfDeposits(_)
        // return totalAmountOfDeposits;
}

You should write a method

public void addDeposits(double deposits) 
             { 
             this.deposits = this.deposits + deposits;
             }

and change setDeposits to

public void setDeposits(double deposits) 
             { 
             this.deposits = deposits;
             }

after this call addDeposits to add deposits

To eliminate confusion within the Saving Class change the argument name for the setDeposits() method to double newDeposit instead of double deposits which is also a class field name. Although the construct is legal it does make it a wee bit confusing. Inside the setDeposits() method use:

this.deposit+= newDeposit;

As a matter of fact, you can get rid of the deposits field altogether since you also have the field named totalAmountOfDeposits. Use that instead:

this.totalAmountOfDeposits+= newDeposit;

You might also want a clearDeposits() method in your Saving Class:

public void clearDeposits() {
    this.totalAmountOfDeposits = 0.0;
}

Your getTotalAmountOfDeposits() method within the Saving Class doesn't really make any sense either. Since you are always summing deposits anyways you can just return what is held within the totalAmountOfDeposits field:

public double getTotalAmountOfDeposits() {
    return totalAmountOfDeposits;
}

The above method is would now of course be very mush the same as the getDeposits() method which could be changed to getTotalDeposits(). You can then change the getTotalAmountOfDeposits() method name to getTotalNumberOfDeposits() and add a additional class field named numberOfDeposits:

private double totalAmountOfDeposits;
private int numberOfDeposits = 0;

public double getTotalDeposits() {                      
     return totalAmountOfDeposits;
}

public int getTotalNumberOfDeposits() {
    return numberOfDeposits;
}

and in your setDeposits() method add the code line:

numberOfDeposits++;

So that it would look something like:

public void setDeposits(double newDeposit) {
    totalAmountOfDeposits+= newDeposit;
    numberOfDeposits++;
}

If you do add a clearDeposits() method to your Saving Class then don't forget to add the code line: numberOfDeposits = 0; into that method as well. It might now look something like:

public void clearDeposits() {
    totalAmountOfDeposits = 0.0;
    numberOfDeposits = 0;
}

You also have some issues within your main() method of your SavingDemo Class. Take a real close look at each call you make to the setDeposits() method for each value the User supplies. Each User supplied value goes into a specific double type variable name. Is that what you are passing to the setDeposits() method? ;)

Once you've got all that taken care of you can display to console:

System.out.println("The total amount has been deposited is " + 
            save.getTotalDeposits() + " by making " + 
            save.getTotalNumberOfDeposits() + " deposits.");
Related