Skip to content

Commit

Permalink
Create dg_voting.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Sep 21, 2024
1 parent ef1cfe3 commit fdb1f6b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions blockchain-module/dg/dg_voting.rs
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;

0 comments on commit fdb1f6b

Please sign in to comment.