We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Although, I read tutorials and implemented the code, I dont know how it works. I mean, the family by default doesn't have senior or junior user. Which means, the familymember class don't know which method to invoke, when two parameters are passed.
I dont see any line that sets the userRole in the class as SENIOR or JUNIOR. I see the below line.
String userRole = family.userRole();
int budgetLimit = family.budgetLimit();
This is infact, setting userRole from family. the default in family is guest, so it should set userRole as Guest which will never be equal to the inputs passed.
when you look to sample input and sample output and the differnence between budget spent and left u will figure out that sunior must have 100 as budget limit and junior must have 50.
The code loops through all of the methods in the FamilyMember class (seniorMember(...) and juniorUser(...)) for each test. For each method, it gets the annotations and checks to see if it's a valid method to call for the current role; if it is, it calls it with the method.invoke(...) line.
In other words, this is very obviously tutorial code, not something you would (I hope!) write in the real world. It's not going to be very performant; it would perform weirdly if there were multiple methods for the same role; and if the methods have different footprints, it throws an "IllegalArgumentException" at run (not compile) time.
This test shows you how annotations can be used, but not how they should be.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Java Annotations
You are viewing a single comment's thread. Return to all comments →
Although, I read tutorials and implemented the code, I dont know how it works. I mean, the family by default doesn't have senior or junior user. Which means, the familymember class don't know which method to invoke, when two parameters are passed.
I dont see any line that sets the userRole in the class as SENIOR or JUNIOR. I see the below line.
This is infact, setting userRole from family. the default in family is guest, so it should set userRole as Guest which will never be equal to the inputs passed.
Am I understanding something different?
basically you need to pass value inside annotation. e.g @FamilyBudget(userRole = "SENIOR", budgetLimit = 100)
How can we figure out that budget limit is 50&100. Nowhere it is mentioned.
You just had to guess from the test case they provided. The challenge was poorly written.
when you look to sample input and sample output and the differnence between budget spent and left u will figure out that sunior must have 100 as budget limit and junior must have 50.
Yes this was just a little trick. We had to calculate from their Sample Output.
Spend: 75 Budget Left: 25 => budgetLimit 100 Spend: 45 Budget Left: 5 => budgetLimit 50
The code loops through all of the methods in the FamilyMember class (seniorMember(...) and juniorUser(...)) for each test. For each method, it gets the annotations and checks to see if it's a valid method to call for the current role; if it is, it calls it with the method.invoke(...) line.
In other words, this is very obviously tutorial code, not something you would (I hope!) write in the real world. It's not going to be very performant; it would perform weirdly if there were multiple methods for the same role; and if the methods have different footprints, it throws an "IllegalArgumentException" at run (not compile) time.
This test shows you how annotations can be used, but not how they should be.