Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
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?
acc2
acc1
I want to ensure that in the setNickName method there is a mechanism to prevent two object instances from being the same.
setNickName
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:
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.
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)
Set
Account
nickname
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.
AccountManager
nickName
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.