-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVotingMachine.java
More file actions
59 lines (58 loc) · 1.18 KB
/
Copy pathVotingMachine.java
File metadata and controls
59 lines (58 loc) · 1.18 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
class VotingMachine
{
String[] cname;
String name;
int[] count;
public void addCandidates(String[] name)
{
cname=new String[name.length];
for (int i=0;i<name.length;i++)
{
cname[i]=name[i];
}
count=new int[cname.length];
}
public void casteVote(int age,String voteToCandidates)
{
try
{
if(age>=18)
{
for (int i=0;i<cname.length;i++)
{
if (cname[i]==voteToCandidates)
{
name=voteToCandidates;
count[i]++;
}
}
}
else
throw new Exception("Invalid age");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void printResults()
{
//System.out.println("name of Candidates "+name);
for (int i=0; i<count.length;i++)
{
if(name.equals(cname[i])){
System.out.println("name of Candidates "+name);
System.out.println("casted Vote "+count[i]);
}
}
}
public static void main(String[] args)
{
String[] na={"jabbar","ali","jawad","inayat"};
VotingMachine vote=new VotingMachine();
vote.addCandidates(na);
vote.casteVote(20,"jabbar");
vote.casteVote(30,"ali");
vote.casteVote(30,"ali");
vote.printResults();
}
}