Home > Back-end >  How do I keep the values of a child function in Java?
How do I keep the values of a child function in Java?

Time:11-27

I am trying to solve a problem about inheritance. It is about making a base account and then make a debit card that inherits from the base account.

The problem is that I do not know how to keep the value of the method in the child class.

Here is my code:

public class BaseAccount {

private double opening;

private double currentAmount = 0.0;

private double amount;


public BaseAccount(double opening, double currentAmount, double amount) {

this.opening = opening;

this.currentAmount = currentAmount;

this.amount = amount;

}


public double getOpening() {

return opening;

}


public void setOpening(double opening) {

this.opening = opening;

}


public double getCurrentAmount() {

return currentAmount;

}


public void setCurrentAmount(double currentAmount) {

this.currentAmount = currentAmount;

}


public double getAmount() {

return amount;

}


public void setAmount(double amount) {

this.amount = amount;

}

public String opening(double opening) {

this.opening = opening;

this.currentAmount = currentAmount   opening;

return "This account has been openend with "   this.opening;

}

public String deposit(double amount) {

this.currentAmount  = amount;

return "Depositing "   amount;

}

public String balance() {

return "Balance: "   currentAmount;

}

}



public class DebitCard extends BaseAccount{


public DebitCard(double opening, double currentAmount, double amount) {

super(opening, currentAmount, amount);

}

public String withdraw(double amount) {

double currentAmount = getCurrentAmount() - amount;

return amount   " have been retired. \nBalance: "   currentAmount;

}

}



public class Inheritance {


public static void main(String[] args) {

BaseAccount base1 = new BaseAccount(0,0,0);

System.out.println(base1.opening(500));

System.out.println(base1.deposit(22.22));

System.out.println(base1.balance());

DebitCard debit1 = new DebitCard(0,0,0);

System.out.println(debit1.opening(400));

System.out.println(debit1.deposit(33.33));

System.out.println(debit1.balance());

System.out.println(debit1.withdraw(33.33));

System.out.println(debit1.balance());

}

}

Run:

This account has been opened with 500.0

Depositing 22.22

Balance: 522.22

This account has been opened with 400.0

Depositing 33.33

Balance: 433.33

33.33 have been retired.

Balance: 400.0

Balance: 433.33

I do not understand why the balance at the ends ignores the change I made in the retire function of the child class. I know I have to do something with the getters and setters but I do not know what exactly.

CodePudding user response:

double currentAmount = getCurrentAmount() - amount;

This statement does not change the value of the currentAmount field: it creates a new currnetAmount local variable. If you want to be able to modify the currentAmount field then it cannot be private.

Try adding setCurrentAmount(currentAmount); to the withdraw method.

Also, the amount field is unused.

Edit:

public String withdraw(double amount) {

   double currentAmount = getCurrentAmount() - amount;

   setCurrentAmount(currentAmount);

   return amount   " have been retired. \nBalance: "   currentAmount;

}
  • Related