-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Import necessary libraries | ||
use std::collections::HashMap; | ||
use std::hash::{Hash, Hasher}; | ||
use std::io::{Read, Write}; | ||
use std::ops::{Add, Mul, Sub}; | ||
use std::vec::Vec; | ||
|
||
// Define the VotingSystem struct | ||
pub struct VotingSystem { | ||
// Voting data | ||
votes: HashMap<u32, Vec<Vote>>, | ||
} | ||
|
||
// Implement the VotingSystem struct | ||
impl VotingSystem { | ||
// Create a new VotingSystem | ||
pub fn new() -> Self { | ||
VotingSystem { | ||
votes: HashMap::new(), | ||
} | ||
} | ||
|
||
// Add a vote to the voting system | ||
pub fn add_vote(&mut self, proposal_id: u32, vote: Vote) { | ||
// Get the vote vector for the proposal | ||
let vote_vector = self.votes.entry(proposal_id).or_insert(Vec::new()); | ||
|
||
// Add the vote to the vector | ||
vote_vector.push(vote); | ||
} | ||
|
||
// Get the voting results for a proposal | ||
pub fn get_voting_results(&self, proposal_id: u32) -> Vec<Vote> { | ||
self.votes.get(&proposal_id).unwrap().clone() | ||
} | ||
} | ||
|
||
// Define the Vote enum | ||
pub enum Vote { | ||
Yes, | ||
No, | ||
Abstain, | ||
} | ||
|
||
// Export the VotingSystem and Vote | ||
pub use VotingSystem; | ||
pub use Vote; |