Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having Trouble with Changing the Value of a Field in Java

Tags:

So, as a disclaimer, I am extremely new to programming and am probably missing something obvious. Now, I am trying to create two methods named withdraw() and deposit() that will allow me to change the value of the field currentBalance, however, every time I try to change the value of currentBalance with my two methods, I check the value of the field with my method getBalance() and it is always unchanged.

class TradingService{

   public class Trader {

      //This field stores the trader's name
      private String traderName;

      //This field stores the trader's current balance
      private double currentBalance;

      //A constructor to create a Trader
      public Trader(String traderName) {
        this.traderName = traderName;
      }

      //This method gets returns the trader's name
      public String getName() {
        return traderName;
      }

      //This method set's the trader's name
      public void setName(String traderName) {
        this.traderName = traderName;
      }

      //This method decreases the trader's balance
      public void withdraw(double withdrawAmount) {
        this.currentBalance = (currentBalance - withdrawAmount);
      }

      //This method increases the trader's balance
      public void deposit(double depositAmount) {
        this.currentBalance = (currentBalance + depositAmount);
      }

      //This method returns the trader's current balance
      public double getBalance() {
        return currentBalance;
      }

   }

}

I am using DrJava and I am testing my code in the interactions panel. Here is the result of my tests.

> Trader t1
> t1 = new Trader("Bill")
Trader@22e1cbe4
> t1.deposit(10.0)
10.0
> t1.getBalance()
0.0

I have done everything that I can imagine to fix the code, but I am out of ideas and I don't think that typing random things into my code for another 3 hours will do much.

Thank you for taking the time to read my question.

like image 408
user6933987 Avatar asked Oct 06 '16 19:10

user6933987


1 Answers

The class "Trader" looking fine and work for me. But you have a "Trader" what is a public class inside "TradingService" class, I'm thinks maybe the class is not compiling and you are executing an old file, you can try this with "Trader" class like a inner class.

class Trader {

  //This field stores the trader's name
  private String traderName;

  //This field stores the trader's current balance
  private double currentBalance;

  //A constructor to create a Trader
  public Trader(String traderName) {
    this.traderName = traderName;
  }

  //This method gets returns the trader's name
  public String getName() {
    return traderName;
  }

  //This method set's the trader's name
  public void setName(String traderName) {
    this.traderName = traderName;
  }

  //This method decreases the trader's balance
  public void withdraw(double withdrawAmount) {
    this.currentBalance = (currentBalance - withdrawAmount);
  }

  //This method increases the trader's balance
  public void deposit(double depositAmount) {
    this.currentBalance = (currentBalance + depositAmount);
  }

  //This method returns the trader's current balance
  public double getBalance() {
    return currentBalance;
  }
}

public class TradingService{

    public static void main(String[] args)
    {

      Trader t = new Trader("test");
      t.deposit(10);
      System.out.print(t.getBalance());

    }
}
like image 143
Dante Faña Badia Avatar answered Sep 24 '22 17:09

Dante Faña Badia