Annotations HackerRank Solution

The code is a Java program that uses annotations to manage a family budget. It defines a custom annotation 'FamilyBudget' that can be applied to methods. The annotation has two elements: 'userRole' and 'moneySpend'. The 'userRole' can be either 'SENIOR' or 'JUNIOR', and 'moneySpend' is the amount of money that the user can spend. The 'FamilyMember' class has two methods, each annotated with 'FamilyBudget', representing the spending behavior of a senior and junior family member.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Annotations HackerRank Solution HackerRank solution using CodeRankGPT:

The solution uses Java Reflection API to access the custom annotation 'FamilyBudget' applied to the methods of the 'FamilyMember' class. It reads the user role and the amount of money to spend from the input. Then, it iterates over the methods of the 'FamilyMember' class and checks if the 'FamilyBudget' annotation is present. If the annotation is present, it retrieves the user role and budget limit from the annotation. If the input role matches the role in the annotation and the input spend is less than or equal to the budget limit, it invokes the method with the budget limit and spend as arguments. If the spend exceeds the budget limit, it prints 'Budget Limit Over'.


/**
 * 
 */
package com.javaaid.hackerrank.solutions.languages.java.advanced;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.Scanner;

/**
 *
 */

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

	int moneySpend() default 100;
}

class FamilyMember {
	@FamilyBudget(userRole = "SENIOR")
	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", moneySpend = 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 JavaAnnotations {
	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.moneySpend();
						if (userRole.equals(role)) {
							if (budgetLimit >= spend) {
								method.invoke(FamilyMember.class.newInstance(), budgetLimit, spend);
							} else {
								System.out.println("Budget Limit Over");
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			testCases--;
		}
		in.close();
	}
}

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform