-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoter.java
More file actions
45 lines (44 loc) · 1.53 KB
/
Voter.java
File metadata and controls
45 lines (44 loc) · 1.53 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
import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
class Voter{
public static int calculateAge(String dob){
LocalDate today=LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate parsedDate = LocalDate.parse(dob, formatter);
Period age = Period.between(parsedDate, today);
return age.getYears();
}
public static List<String> getEligibleVoter(HashMap<String, String> mapObj){
List<String> listObj=new ArrayList<String>();
Iterator<Map.Entry<String, String>> itr = mapObj.entrySet().iterator();
while(itr.hasNext()){
Map.Entry<String, String> entry = itr.next();
int ageCheck=calculateAge(entry.getKey());
if(ageCheck>=18){
listObj.add(entry.getValue());
}
}
return listObj;
}
public static void main(String[] args){
Scanner s=new Scanner(System.in);
HashMap<String, String> obj=new HashMap<String, String>();
System.out.println("\nEnter total number of Voters to find eligibility..");
int n=s.nextInt();
String[] voterDob=new String[n];
String[] voterName=new String[n];
System.out.println("\nEnter Name and DOB(DD-MM-YYYY) of Voters..");
for(int i=0;i<n;i++){
voterName[i]=s.next();
voterDob[i]=s.next();
obj.put(voterDob[i], voterName[i]);
}
List<String> voterList=new ArrayList<String>();
voterList=getEligibleVoter(obj);
System.out.println("\n-----------Eligible Voters List-----------\n");
for(String i: voterList){
System.out.println("\t\t"+i);
}
}
}