- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrankJava0042.java
More file actions
Latest commit
77 lines (67 loc) · 2.16 KB
/
Copy pathHackerrankJava0042.java
File metadata and controls
77 lines (67 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;
importjava.lang.reflect.Method;
importjava.util.Scanner;
publicclassHackerrankJava0042 {
publicstaticvoidmain(String[] args) {
// Java Annotations
// https://www.hackerrank.com/challenges/java-annotations/problem?isFullScreen=true
Scannerin = newScanner(System.in);
inttestCases = Integer.parseInt(in.nextLine());
while (testCases > 0) {
Stringrole = in.next();
intspend = in.nextInt();
try {
ClassannotatedClass = FamilyMember.class;
Method[] methods = annotatedClass.getMethods();
for (Methodmethod : methods) {
if (method.isAnnotationPresent(FamilyBudget.class)) {
FamilyBudgetfamily = method
.getAnnotation(FamilyBudget.class);
StringuserRole = family.userRole();
// --- You need to add this line ---
intbudgetLimit = family.budgetLimit();
if (userRole.equals(role)) {
// --- You need to add this line ---
if (budgetLimit >= spend) {
method.invoke(FamilyMember.class.newInstance(),
budgetLimit, spend);
} else {
System.out.println("Budget Limit Over");
}
}
}
}
} catch (Exceptione) {
e.printStackTrace();
}
testCases--;
}
in.close();
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface FamilyBudget {
StringuserRole() default"GUEST";
// --- You need to add this line ---
intbudgetLimit() default0;
}
classFamilyMember {
// --- You need to add this line ---
@FamilyBudget(userRole = "SENIOR", budgetLimit = 100)
publicvoidseniorMember(intbudget, intmoneySpend) {
System.out.println("Senior Member");
System.out.println("Spend: " + moneySpend);
System.out.println("Budget Left: " + (budget - moneySpend));
}
// --- You need to add this line ---
@FamilyBudget(userRole = "JUNIOR", budgetLimit = 50)
publicvoidjuniorUser(intbudget, intmoneySpend) {
System.out.println("Junior Member");
System.out.println("Spend: " + moneySpend);
System.out.println("Budget Left: " + (budget - moneySpend));
}
}