-
Notifications
You must be signed in to change notification settings - Fork 0
/
Voting System.go
125 lines (104 loc) · 2.6 KB
/
Voting System.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
)
type Vote struct {
VoterID int
Candidate string
}
type Block struct {
PrevHash string
CurrentHash string
Votes []Vote
}
var Blockchain []Block
var Candidates map[string]int
var Voters map[int]bool
func RegisterVoter(voterID int) {
Voters[voterID] = false
fmt.Printf("Voter %d registered.\n", voterID)
}
func CastVote(voterID int, candidate string) {
if _, ok := Voters[voterID]; !ok {
fmt.Println("Invalid voter ID: ", voterID)
return
}
if Voters[voterID] {
fmt.Println("Voter", voterID, "has already cast a vote.")
return
}
if _, ok := Candidates[candidate]; !ok {
fmt.Println("Candidate does not exist.")
return
}
vote := Vote{VoterID: voterID, Candidate: candidate}
lastBlock := Blockchain[len(Blockchain)-1]
newBlock := Block{
PrevHash: lastBlock.CurrentHash,
Votes: append(lastBlock.Votes, vote),
}
newBlock.CurrentHash = calculateHash(newBlock)
Blockchain = append(Blockchain, newBlock)
Candidates[candidate]++
Voters[voterID] = true
fmt.Printf("Vote cast by Voter %d for %s is recorded.\n", voterID, candidate)
}
func calculateHash(block Block) string {
record := block.PrevHash
for _, vote := range block.Votes {
record += string(vote.VoterID) + vote.Candidate
}
h := sha256.New()
h.Write([]byte(record))
hashed := h.Sum(nil)
return hex.EncodeToString(hashed)
}
func CalculateElectionResults() {
fmt.Println("\nElection Results:")
var winner string
maxVotes := -1
for candidate, votes := range Candidates {
if votes > maxVotes {
winner = candidate
maxVotes = votes
} else if votes == maxVotes {
winner = "Tie"
}
}
if winner != "Tie" {
fmt.Printf("Winner: %s\n", winner)
} else {
fmt.Println("Election resulted in a tie.")
}
}
func main() {
genesisBlock := Block{PrevHash: "", CurrentHash: "", Votes: nil}
Blockchain = append(Blockchain, genesisBlock)
Candidates = make(map[string]int)
Candidates["Candidate A"] = 0
Candidates["Candidate B"] = 0
Voters = make(map[int]bool)
for i := 1; i <= 10; i++ {
RegisterVoter(i)
}
CastVote(1, "Candidate A")
CastVote(2, "Candidate B")
CastVote(3, "Candidate A")
CastVote(3, "Candidate B")
CastVote(4, "Candidate B")
CastVote(5, "Candidate A")
CastVote(5, "Candidate A")
CastVote(6, "Candidate B")
CastVote(7, "Candidate C")
CastVote(11, "Candidate B")
CalculateElectionResults()
fmt.Println("\nBlockchain:")
for i, block := range Blockchain {
fmt.Printf("Block %d\n", i)
fmt.Printf("PrevHash: %s\n", block.PrevHash)
fmt.Printf("CurrentHash: %s\n", block.CurrentHash)
fmt.Printf("Votes: %v\n\n", block.Votes)
}
}