forked from Sonu64/Voting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoting.sol
65 lines (54 loc) · 1.49 KB
/
voting.sol
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
//SPDX-License-Identifier:MIT
pragma solidity ^0.8.0;
contract votes{
uint private Candidate_1;
uint private Candidate_2;
uint private Candidate_3;
uint private Candidate_4;
address[] voters;
modifier check(uint _s){
int correct=0;
if(_s==1||_s==2||_s==3||_s==4){
correct+=1;
}
require(correct!=0,"Invalid Option");
_;
}
modifier voter(address _add)
{
uint flag=0;
for(uint i=0;i<voters.length;i++)
{
if(_add==voters[i])
{
flag+=1;
break;
}
}
require(flag==0,"Voter has already voted");
_;
}
function vote(uint Candidate_Number) external check(Candidate_Number) voter(msg.sender){
voters.push(msg.sender);
uint _s=Candidate_Number;
if(_s==1)
Candidate_1+=1;
else if(_s==2)
Candidate_2+=1;
else if(_s==3)
Candidate_3+=1;
else
Candidate_4+=1;
}
function vote_count(uint Candidate_Number) external view check(Candidate_Number) returns(uint){
// return(Candidate_1,Candidate_2,Candidate_3,Candidate_4);
if(Candidate_Number==1)
return Candidate_1;
else if(Candidate_Number==2)
return Candidate_2;
else if(Candidate_Number==3)
return Candidate_3;
else
return Candidate_4;
}
}