yandex
loader

please wait

  • Raman Tripathi Mar-14-2019 06:03:25 AM ( 1 week ago )

     

    Below is my code snippet

    Account acc1 = new Account ("123", "James", "Savings");
    Account acc2 = new Account ("234", "James", "Checking");
    acc1.setNickName("James Account");
    acc2.setNickName("James Account");
    
    
    //Nick Name Method
    private String nickName;
    
    public void setNickName (String name)
    {
        nickname = name;
    
    }

    My question is how can I prevent acc2 object from having the value of NickName same as that of acc1?

    I want to ensure that in the setNickName method there is a mechanism to prevent two object instances from being the same.

  • Rahul Chaudhary Mar-14-2019 06:05:06 AM ( 1 week ago )

    It's not as simple as just overriding equals or hashCode in your Account object. By specifying a constraint on two distinct instances of Account, this implies that both instances are aware of one another. In this context, they're not going to be; they're two independent instances which really shouldn't be aware of each other in reality.

    What you're going to have to do is have some kind of account manager service, which could then be responsible for setting the nickname on the accounts.

    In this context, you don't really have to override either equals or hashCode since that's not going to give you what you want anyway; you won't always have two otherwise identical accounts with the same nickname, and bastardizing the equals method to pay attention to a single field feels wrong.

    First things first - let's start with a simple account manager. This makes use of collections and streams, concepts which for brevity I'll leave as an exercise for the reader.

    The mechanics are straightforward:

    • If we have an account which has the nickname, we don't update it. If we don't, we update it by ID.
    • This service is now responsible for creating and manipulating all accounts.  

    publicclassAccountManager{

    final List accounts = new ArrayList<>();

    public Account createAccount(String id, String name, String type) {

    Account account = new Account(id, name, type);

    accounts.add(account);

    return account; }

    public boolean setNickName(String id, String nickName) {

    Optional existingAccount = accounts.stream()

    .firstMatch(a -> a.getNickName().equals(nickName));

    if(existingAccount.isPresent()) {

    return false; // don't allow modification to an account who has the same nickname

    } else {

    accounts.stream()

    .firstMatch(a -> a.getId().equals(id))

    .ifPresent(a -> a.setNickName(nickName));

    return true;

    }

    }

    }

    Now, with this class, you should get what you want - any account managed by the AccountManager won't be able to have the same nickname.

  • Priya Roy Mar-14-2019 06:10:02 AM ( 1 week ago )

    You can create a static Set object in your Account class and every time a nickname is added, you check if already one exists. If it doesn't, then set the value. If it doesn't exists, then you can have your own logic(I have thrown a IllegalArgumentException)

    class Account {
    
    private static Set<String> nickNameSet = new HashSet<>();
    
    public void setNickName(String nickName) {
        if(nickNameSet.add(nickName))
        {
        this.nickName = nickName;
        }
        else {
            throw new IllegalArgumentException("Nick Name already exists");
        }
      }
    }

    As pointed out by @Makoto, defining the Set object in the Account class will lead to all account object having access to nickname of other accounts. If data hiding is your concern, we should create a new class say AccountManager and have the logic of identifying duplicate nickName delegated to it.

Please login

Similar Discussion

Recommended For You