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.
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());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With