-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoting.sol
More file actions
82 lines (68 loc) · 2.76 KB
/
Voting.sol
File metadata and controls
82 lines (68 loc) · 2.76 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
78
79
80
81
82
pragma solidity >=0.4.0 <0.6.0;
// This line says the code will compile with version greater than 0.4 and less than 0.6
contract Voting {
// constructor to initialize candidates
// vote for candidates
// get count of votes for each candidates
struct voteRoom {
bytes32 roomName;
bytes32[] candidateList;
mapping (bytes32 => uint8) votesReceived;
bytes32 voteDate;
bool exists;
mapping (address => uint8) voted;
uint8 maxVotes;
uint8 end;
}
mapping (uint8 => voteRoom) voteRoomList;
uint8 public roomCount = 1;
function isVoteEnd(uint8 roomNumber, uint8 xx, uint8 yy, uint8 zz) view public returns (uint8) {
return voteRoomList[roomNumber].end;
}
function voteEnd(uint8 roomNumber, uint8 xx, uint8 yy, uint8 zz) public {
voteRoomList[roomNumber].end = 1;
}
function maxCheck(uint8 roomNumber, uint8 xx, uint8 yy, uint8 zz, uint8 aa) view public returns (uint8) {
return voteRoomList[roomNumber].maxVotes;
}
function votedCheck(uint8 roomNumber, uint8 xx, uint8 yy, uint8 zz) view public returns (bool) {
if(voteRoomList[roomNumber].voted[msg.sender] == 1){
return true;
}
else{
return false;
}
}
function addVoteRoom(bytes32 _roomName, bytes32[] memory _candidateList, bytes32 _voteDate, uint8 _max, uint8 xx) public {
voteRoomList[roomCount].roomName = _roomName;
voteRoomList[roomCount].candidateList = _candidateList;
for (uint i = 0; i < _candidateList.length; i++) {
voteRoomList[roomCount].votesReceived[_candidateList[i]] = 0;
}
voteRoomList[roomCount].voteDate = _voteDate;
voteRoomList[roomCount].exists = true;
voteRoomList[roomCount].maxVotes = _max;
roomCount = roomCount + 1;
}
function getRoomName(uint8 roomNumber, uint8 xx, uint8 yy) view public returns(bytes32) {
return voteRoomList[roomNumber].roomName;
}
function getCandidateList(uint8 roomNumber, uint8 xx, uint8 yy) view public returns(bytes32[] memory) {
return voteRoomList[roomNumber].candidateList;
}
function getVoteDate(uint8 roomNumber, uint8 xx, uint8 yy) view public returns(bytes32) {
return voteRoomList[roomNumber].voteDate;
}
function voteForCandidate(uint8 roomNumber, uint8 candiNum, uint8 xx) public {
//require(!votedCheck(roomNumber,0,0,0));
bytes32 candiName = voteRoomList[roomNumber].candidateList[candiNum];
voteRoomList[roomNumber].votesReceived[candiName] += 1;
voteRoomList[roomNumber].voted[msg.sender]=1;
}
function totalVotesFor(uint8 roomNumber, bytes32 candidate, uint8 xx) view public returns(uint8) {
return voteRoomList[roomNumber].votesReceived[candidate];
}
function howManyRooms(uint8 xx) view public returns(uint8) {
return roomCount;
}
}