Edison Miranda
Edison Miranda

Reputation: 333

How to extract object from set by part of fields in its class

Assume I have some class

public class CashCard {
    private String lastName;
    private int pinCode;
    private int balance;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getPinCode() {
        return pinCode;
    }

    public void setPinCode(int pinCode) {
        this.pinCode = pinCode;
    }

    public int getBalance() {
        return balance;
    }

    public int takeMoney(int money) throws NotEnoughMoneyException {
        if (money > balance)
            throw new NotEnoughMoneyException();

        balance -= money;
        return money;
    }

    public int putMoney(int money) {
        balance += money;
        return money;
    }

    public CashCard(String lastName, int pinCode, int balance) {
        this.lastName = lastName;
        this.pinCode = pinCode;
        this.balance = balance;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || o.getClass() != this.getClass())
            return false;
        if (o == this)
            return true;
        CashCard that = (CashCard) o;
        return this.lastName.equals(that.lastName) && this.pinCode == that.pinCode;
    }

    @Override
    public int hashCode() {
        int result = 17;
        int prime = 31;

        result = result * prime + lastName.hashCode();
        result = result * prime + pinCode;

        return result;
    }
}

the main point in this class is that it has 3 fields but for 2 objects of this class if they have same(equivalent) two fields they are equal no matter what third field is for each of them

Next, for example I have some set of this objects

CashCard card1 = new CashCard("Goldman", 1111, 9000);
CashCard card2 = new CashCard("Miranda", 1234, 100);
CashCard card3 = new CashCard("Grey", 6969, 1000000);

Set<CashCard> cashCards = new HashSet<CashCard>();
cashCards.add(card1);
cashCards.add(card2);
cashCards.add(card3);

The last thing - I have last name and pincode

String lastName = "Goldman";
int pincode = 1111;

I need to figure out if set contains element with such name and pincode AND if it is so, change that element in set in some way.

Upvotes: 1

Views: 1295

Answers (1)

aioobe
aioobe

Reputation: 421290

You can't extract the object which matches some constraint directly. You can only check for containment. To actually find that particular object you're after, you'll have to iterate over the set using an iterator and compare each object with the one you've constructed. That is, do something as follows:

for (CashCard cc : cashCards)
    if (cc.equals(new CashCard("Goldman", 1111, 0L))
        cc.balance -= toDebit;

If you don't share the reference in other places, you could also remove the CashCard and insert a new CashCard with the updated property:

CashCard newCard = new CashCard("Goldman", 1111, newBalance);
cashCards.remove(newCard);
cashCards.add(newCard);

I would strongly advice you to rethink your design though. Excluding one field from equals/hashcode is a real code smell. Consider making CashCard contain name+pin and use a Map<CashCard, Long> balanceMap for balances instead.

Upvotes: 1

Related Questions