<\/span><\/h2>\n\n\n\nJava annotation can be used to define the metadata of a Java class or class element. We can use Java annotation at the compile time to instruct the compiler about the build process. Annotation is also used at runtime to get insight into the properties of class elements.<\/p>\n\n\n\n
Java annotation can be added to an element in the following way:<\/p>\n\n\n\n
@Entity\n Class DemoClass{\n\n }<\/code><\/pre>\n\n\n\nWe can also set a value to the annotation member. For example:<\/p>\n\n\n\n
@Entity(EntityName=\"DemoClass\")\n Class DemoClass{\n\n }<\/code><\/pre>\n\n\n\nIn Java, there are several built-in annotations. You can also define your own annotations in the following way:<\/p>\n\n\n\n
@Target(ElementType.METHOD)\n @Retention(RetentionPolicy.RUNTIME)\n @interface FamilyBudget {\n String userRole() default \"GUEST\";\n }<\/code><\/pre>\n\n\n\nHere, we define an annotation FamilyBudget<\/strong>, where userRole <\/strong>is the only member in that custom annotation. The userRole<\/strong> takes only String <\/strong>type values, and the default is “GUEST”<\/strong>. If we do not define the value for this annotation member, then it takes the default. By using @Target<\/strong>, we can specify where our annotation can be used. For example, the FamilyBudget<\/strong> annotation can only be used with the method in a class. @Retention<\/strong> defines whether the annotation is available at runtime. To learn more about Java annotation.<\/p>\n\n\n\nTake a look at the following code segment:<\/p>\n\n\n\n
@Target(ElementType.METHOD)\n@Retention(RetentionPolicy.RUNTIME)\n@interface FamilyBudget {\n String userRole() default \"GUEST\";\n}\n\nclass FamilyMember {\n\n public void seniorMember(int budget, int moneySpend) {\n System.out.println(\"Senior Member\");\n System.out.println(\"Spend: \" + moneySpend);\n System.out.println(\"Budget Left: \" + (budget - moneySpend));\n }\n\n public void juniorUser(int budget, int moneySpend) {\n System.out.println(\"Junior Member\");\n System.out.println(\"Spend: \" + moneySpend);\n System.out.println(\"Budget Left: \" + (budget - moneySpend));\n }\n}\n\npublic class Solution {\n public static void main(String[] args) {\n Scanner in = new Scanner(System.in);\n int testCases = Integer.parseInt(in.nextLine());\n while (testCases > 0) {\n String role = in.next();\n int spend = in.nextInt();\n try {\n Class annotatedClass = FamilyMember.class;\n Method[] methods = annotatedClass.getMethods();\n for (Method method : methods) {\n if (method.isAnnotationPresent(FamilyBudget.class)) {\n FamilyBudget family = method\n .getAnnotation(FamilyBudget.class);\n String userRole = family.userRole();\n int budgetLimit = family.budgetLimit();\n if (userRole.equals(role)) {\n if(spend<=budgetLimit){\n method.invoke(FamilyMember.class.newInstance(),\n budgetLimit, spend);\n }else{\n System.out.println(\"Budget Limit Over\");\n }\n }\n }\n }\n } catch (Exception e) {\n e.printStackTrace();\n }\n testCases--;\n }\n }\n}<\/code><\/pre>\n\n\n\nHere, we partially define an annotation, FamilyBudget<\/strong> and a class, FamilyMember<\/strong>. In this problem, we give the user role and the amount of money that a user spends as inputs. Based on the user role, you have to call the appropriate method in the FamilyMember<\/strong> class. If the amount of money spent is over the budget limit for that user role, it prints Budget Limit Over.
Your task is to complete the FamilyBudget<\/strong> annotation and the FamilyMember<\/strong> class so that the Solution <\/strong>class works perfectly with the defined constraints.<\/p>\n\n\n\nNote : <\/strong>You must complete the 5<\/strong> incomplete lines in the editor. You are not allowed to change, delete or modify any other lines. To restore the original code, click on the top-left button on the editor and create a new buffer.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line of input contains an integer N <\/em><\/strong>representing the total number of test cases. Each test case contains a string and an integer separated by a space on a single line in the following format:<\/p>\n\n\n\n UserRole MoneySpend<\/code><\/pre>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h4>\n\n\n\n- 2 <= N <= 10<\/li>
- 0 <= MoneySpend <= <\/em>200 <\/li>
- |UserRole<\/em>| = 6<\/li>
- Name contains only lowercase English letters.<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nBased on the user role and budget outputs, output the contents of the certain method. If the amount of money spent is over the budget limit, then output Budget Limit Over<\/code>.<\/p>\n\n\n\n<\/span>Sample Input<\/strong><\/span><\/h4>\n\n\n\n 3\n SENIOR 75\n JUNIOR 45\n SENIOR 40<\/code><\/pre>\n\n\n\n<\/span>Sample Output<\/strong><\/span><\/h4>\n\n\n\n Senior Member\n Spend: 75\n Budget Left: 25\n Junior Member\n Spend: 45\n Budget Left: 5\n Senior Member\n Spend: 40\n Budget Left: 60<\/code><\/pre>\n\n\n\n<\/span>Solution – Java Annotations<\/strong><\/span><\/h2>\n\n\n\nimport java.lang.annotation.*;\nimport java.lang.reflect.*;\nimport java.util.*;\n\n@Target(ElementType.METHOD)\n@Retention(RetentionPolicy.RUNTIME)\n@interface FamilyBudget {\n String userRole() default \"GUEST\";\n\n int budgetLimit() default 0;\n}\n\nclass FamilyMember {\n @FamilyBudget(userRole = \"SENIOR\", budgetLimit = 100)\n public void seniorMember(int budget, int moneySpend) {\n System.out.println(\"Senior Member\");\n System.out.println(\"Spend: \" + moneySpend);\n System.out.println(\"Budget Left: \" + (budget - moneySpend));\n }\n\n @FamilyBudget(userRole = \"JUNIOR\", budgetLimit = 50)\n public void juniorUser(int budget, int moneySpend) {\n System.out.println(\"Junior Member\");\n System.out.println(\"Spend: \" + moneySpend);\n System.out.println(\"Budget Left: \" + (budget - moneySpend));\n }\n}\n\npublic class Solution {\n public static void main(String[] args) {\n Scanner in = new Scanner(System.in);\n int testCases = Integer.parseInt(in.nextLine());\n while (testCases > 0) {\n String role = in.next();\n int spend = in.nextInt();\n try {\n Class annotatedClass = FamilyMember.class;\n Method[] methods = annotatedClass.getMethods();\n for (Method method : methods) {\n if (method.isAnnotationPresent(FamilyBudget.class)) {\n FamilyBudget family = method\n .getAnnotation(FamilyBudget.class);\n String userRole = family.userRole();\n int budgetLimit = family.budgetLimit();\n if (userRole.equals(role)) {\n if(spend<=budgetLimit){\n method.invoke(FamilyMember.class.newInstance(),\n budgetLimit, spend);\n }else{\n System.out.println(\"Budget Limit Over\");\n }\n }\n }\n }\n } catch (Exception e) {\n e.printStackTrace();\n }\n testCases--;\n }\n }\n}<\/pre>\n\n\n\n