-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelection_day.go
40 lines (32 loc) · 1.03 KB
/
election_day.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
package electionday
import (
"fmt"
)
// NewVoteCounter returns a new vote counter with
// a given number of initial votes.
func NewVoteCounter(initialVotes int) *int {
return &initialVotes
}
// VoteCount extracts the number of votes from a counter.
func VoteCount(counter *int) int {
if counter != nil {
return *counter
}
return 0
}
// IncrementVoteCount increments the value in a vote counter.
func IncrementVoteCount(counter *int, increment int) {
*counter = *counter + increment
}
// NewElectionResult creates a new election result.
func NewElectionResult(candidateName string, votes int) *ElectionResult {
return &ElectionResult{Name: candidateName, Votes: votes}
}
// DisplayResult creates a message with the result to be displayed.
func DisplayResult(result *ElectionResult) string {
return fmt.Sprintf("%s (%d)", result.Name, result.Votes)
}
// DecrementVotesOfCandidate decrements by one the vote count of a candidate in a map.
func DecrementVotesOfCandidate(results map[string]int, candidate string) {
results[candidate]--
}