Search code examples
javaclassmethodsreturnreturn-value

How to make calculation inside class field in Java?


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.


Solution

  • 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;
    }