From fdb1f6ba8ffae8b22bf369fbaab90a180dfcd9f0 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sat, 21 Sep 2024 19:26:04 +0700 Subject: [PATCH] Create dg_voting.rs --- blockchain-module/dg/dg_voting.rs | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 blockchain-module/dg/dg_voting.rs diff --git a/blockchain-module/dg/dg_voting.rs b/blockchain-module/dg/dg_voting.rs new file mode 100644 index 000000000..70643d1f3 --- /dev/null +++ b/blockchain-module/dg/dg_voting.rs @@ -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>, +} + +// 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 { + 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;