Java Annotations HackerRank Solution

Java Annotations HackerRank Solution
Java Annotations HackerRank Solution

Java Annotations HackerRank Solution

Java 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.

Java annotation can be added to an element in the following way:

@Entity
Class DemoClass{

}

We can also set a value to the annotation member. For example:

@Entity(EntityName="DemoClass")
Class DemoClass{

}

In Java, there are several built-in annotations. You can also define your own annotations in the following way:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface FamilyBudget {
   String userRole() default "GUEST";
}

Code Solution:

//Java Annotations HackerRank Solution
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface FamilyBudget {
	String userRole() default "GUEST";
    int budgetLimit() default 0;
	
}
class FamilyMember {
	@FamilyBudget(userRole = "SENIOR", budgetLimit = 100)
	public void seniorMember(int budget, int moneySpend) {
		System.out.println("Senior Member");
		System.out.println("Spend: " + moneySpend);
		System.out.println("Budget Left: " + (budget - moneySpend));
	}
	@FamilyBudget(userRole = "JUNIOR", budgetLimit = 50)
	public void juniorUser(int budget, int moneySpend) {
		System.out.println("Junior Member");
		System.out.println("Spend: " + moneySpend);
		System.out.println("Budget Left: " + (budget - moneySpend));
	}
}
public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int testCases = Integer.parseInt(in.nextLine());
		while (testCases > 0) {
			String role = in.next();
			int spend = in.nextInt();
			try {
				Class annotatedClass = FamilyMember.class;
				Method[] methods = annotatedClass.getMethods();
				for (Method method : methods) {
					if (method.isAnnotationPresent(FamilyBudget.class)) {
						FamilyBudget family = method
								.getAnnotation(FamilyBudget.class);
						String userRole = family.userRole();
						int budgetLimit = family.budgetLimit(); ;
						if (userRole.equals(role)) {
							if(spend<=budgetLimit){
								method.invoke(FamilyMember.class.newInstance(),
										budgetLimit, spend);
							}else{
								System.out.println("Budget Limit Over");
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			testCases--;
		}
	}
}
//Java Annotations HackerRank Solution

This Java program is a solution to a HackerRank problem that involves using annotations to manage family budgets based on the roles of family members. Let’s break down the solution:

  1. Annotations:
    • @FamilyBudget: This is a custom annotation defined with @interface. It has two elements: userRole and budgetLimit, which represent the role of the family member and their budget limit, respectively.
    • @Target(ElementType.METHOD): Specifies that this annotation can be applied to methods.
    • @Retention(RetentionPolicy.RUNTIME): Specifies that this annotation should be available at runtime.
  2. FamilyMember Class:
    • seniorMember and juniorUser methods are annotated with @FamilyBudget, specifying the role and budget limit for each method.
  3. Main Class (Solution):
    • The main method reads input from the user regarding the family member’s role and spending.
    • For each test case, it iterates through the methods of the FamilyMember class.
    • It checks if a method is annotated with @FamilyBudget. If so, it retrieves the annotation details.
    • If the user’s role matches the role specified in the annotation, it checks if the spending is within the budget limit.
    • If the spending is within the limit, it invokes the annotated method with the budget and spending parameters. If not, it prints “Budget Limit Over”.
  4. Exception Handling:
    • The code includes exception handling to catch any errors that may occur during method invocation or class instantiation.

Overall, this solution demonstrates the use of annotations to manage family budgets based on roles, allowing for flexible and customizable budget constraints for different family members. It also showcases reflection in Java to dynamically inspect and invoke annotated methods at runtime.

Disclaimer: This problem is originally created and published by HackerRank, we only provide solutions to this problem. Hence, doesn’t guarantee the truthfulness of the problem. This is only for information purposes.

Leave a Comment