- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJavaAnnotations.java
More file actions
Latest commit
63 lines (59 loc) · 2.38 KB
/
Copy pathJavaAnnotations.java
File metadata and controls
63 lines (59 loc) · 2.38 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
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;
importjava.lang.reflect.Method;
importjava.util.Scanner;
publicclassJavaAnnotations {
publicstaticvoidmain(String[] args) {
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();
intbudgetLimit = family.budgetLimit();
if (userRole.equals(role)) {
if(spend <= budgetLimit){
method.invoke(FamilyMember.class.newInstance(),
budgetLimit, spend);
}else{
System.out.println("Budget Limit Over");
}
}
}
}
} catch (Exceptione) {
e.printStackTrace();
}
testCases--;
}
}
privatestaticclassFamilyMember {
@FamilyBudget(userRole="SENIOR")
publicvoidseniorMember(intbudget, intmoneySpend) {
System.out.println("Senior Member");
System.out.println("Spend: " + moneySpend);
System.out.println("Budget Left: " + (budget - moneySpend));
}
@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));
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
private @interface FamilyBudget {
StringuserRole() default"GUEST";
intbudgetLimit() default100;
}
}