Issues with changing a field value in Java

So, as a disclaimer, I'm extremely new to programming and probably don't see anything obvious. Now I am trying to create two methods with the name () and deposit () that will allow me to change the value of the currentBalance field, however every time I try to change the value of currentBalance using my two methods, I check the value of the field with my getBalance () method, and it always remains unchanged.

class TradingService{ public class Trader { //This field stores the trader name private String traderName; //This field stores the trader current balance private double currentBalance; //A constructor to create a Trader public Trader(String traderName) { this.traderName = traderName; } //This method gets returns the trader name public String getName() { return traderName; } //This method set the trader name public void setName(String traderName) { this.traderName = traderName; } //This method decreases the trader balance public void withdraw(double withdrawAmount) { this.currentBalance = (currentBalance - withdrawAmount); } //This method increases the trader balance public void deposit(double depositAmount) { this.currentBalance = (currentBalance + depositAmount); } //This method returns the trader current balance public double getBalance() { return currentBalance; } } } 

I am using DrJava and I am testing my code in the interaction 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 did everything I can imagine to fix the code, but I have no ideas, and I don’t think that writing random things in my code for 3 hours will do a lot.

Thanks for taking the time to read my question.

+5
source share
2 answers

The Trader class looks great and works for me. But you have a "Trader", what is an open class inside of the "TradingService" class, I think, maybe the class does not compile, and you are executing the old file, you can try it using the "Trader" class, as an inner class.

 class Trader { //This field stores the trader name private String traderName; //This field stores the trader current balance private double currentBalance; //A constructor to create a Trader public Trader(String traderName) { this.traderName = traderName; } //This method gets returns the trader name public String getName() { return traderName; } //This method set the trader name public void setName(String traderName) { this.traderName = traderName; } //This method decreases the trader balance public void withdraw(double withdrawAmount) { this.currentBalance = (currentBalance - withdrawAmount); } //This method increases the trader balance public void deposit(double depositAmount) { this.currentBalance = (currentBalance + depositAmount); } //This method returns the trader 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()); } } 
+2
source

Below code is working fine. I'm not sure what Dr. Java, but the code is working properly.

 public class TradingService { class Trader { //This field stores the trader name private String traderName; //This field stores the trader current balance private double currentBalance; //A constructor to create a Trader public Trader(String traderName) { this.traderName = traderName; } //This method gets returns the trader name public String getName() { return traderName; } //This method set the trader name public void setName(String traderName) { this.traderName = traderName; } //This method decreases the trader balance public void withdraw(double withdrawAmount) { this.currentBalance = (currentBalance - withdrawAmount); } //This method increases the trader balance public void deposit(double depositAmount) { this.currentBalance = (currentBalance + depositAmount); } //This method returns the trader 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 
+2
source

Source: https://habr.com/ru/post/1257821/


All Articles