简体   繁体   中英

Having Trouble with Changing the Value of a Field in Java

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.

Below code is working fine. I am not sure what Dr. Java is but the code is working as expected.

public class TradingService {

    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 static void main(String[] args) {
        TradingService service = new TradingService();
        TradingService.Trader trader = service.new Trader("trader");
        System.out.println("Balance before deposit:\n" + trader.getBalance());
        trader.deposit(10.00);
        System.out.println("Balance after deposit:\n" + trader.getBalance());
    }
}

Output:

Balance before deposit:
0.0
Balance after deposit:
10.0

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());

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM