asu
asu

Reputation: 91

How to update class field

I didn't know how to explain in the title, so here it is.

In class Passenger i have field balance, getters and setters ofc.

In another class I have some method, in that method I have some computation and it looks like this:

System.out.println("New balance of: " + selectedPassenger.getFirstName() + " is: " + (selectedPassenger.getBalance() - discountEconomyClass()));

As you can see I take the balance field, that is field from Passenger class, then I subtract that field from some method, it doesn't matter.

Then I display the list but in that list the value of the balance has not changed.

  @Override
public void showPassengerList(ArrayList<Passenger> passengersList) {
    System.out.println("---Passengers list--");
    for (Passenger tempPassenger : passengersList) {
        System.out.println(tempPassenger);
    }
}

Before executing that method where I subtract balance, balance is:

balance=300.0 after I run program and that method is executed balance is the same on the list. How to update balance?

I tried with this but don't know how exactly:

System.out.println("New balance is: " + selectedPassenger.setBalance());

Upvotes: 0

Views: 549

Answers (1)

tinstaafl
tinstaafl

Reputation: 6948

In your example you're only printing out the new balance. You aren't setting the balance to the new value. Try something like this:

selectedPassenger.setBalance(selectedPassenger.getBalance() - discountEconomyClass());

Upvotes: 3

Related Questions