Digital Voting Ballot
Example heading with h2 size
Example heading with h3 size
Following is sample java code.
int i = 10;
if(i>0){
System.out.println('positive');
}
```<dependencies>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.9.4</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
</dependency>
</dependencies>
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.ClientTransactionManager;
import org.web3j.tx.gas.DefaultGasProvider;
import java.math.BigInteger;
import java.util.List;
import java.util.Scanner;
public class VotingDApp {
private static final String INFURA_URL = "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"; // Replace with your Infura project URL
private static final String CONTRACT_ADDRESS = "YOUR_CONTRACT_ADDRESS"; // Replace with your contract address
private static final String ADMIN_PRIVATE_KEY = "YOUR_PRIVATE_KEY"; // Replace with your private key
private static final String WALLET_ADDRESS = "YOUR_WALLET_ADDRESS"; // Replace with your wallet address
private static Web3j web3j;
private static VotingContract contract;
public static void main(String[] args) throws Exception {
// Connect to Ethereum client
web3j = Web3j.build(new HttpService(INFURA_URL));
System.out.println("Connected to Ethereum network.");
// Load contract
contract = VotingContract.load(
CONTRACT_ADDRESS,
web3j,
new ClientTransactionManager(web3j, WALLET_ADDRESS),
new DefaultGasProvider()
);
// Check if the contract is valid
if (!contract.isValid()) {
System.err.println("Invalid contract address or ABI.");
return;
}
// Simple CLI to interact with the smart contract
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Select an action: 1) Register Voter 2) Vote 3) Show Candidates 4) Exit");
int action = scanner.nextInt();
switch (action) {
case 1:
registerVoter();
break;
case 2:
vote();
break;
case 3:
showCandidates();
break;
case 4:
System.out.println("Exiting.");
return;
default:
System.out.println("Invalid action. Please try again.");
}
}
}
private static void registerVoter() throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter voter's address: ");
String voterAddress = scanner.nextLine();
TransactionReceipt receipt = contract.registerVoter(voterAddress).send();
System.out.println("Voter registered. Transaction Hash: " + receipt.getTransactionHash());
}
private static void vote() throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter candidate index to vote for: ");
int candidateIndex = scanner.nextInt();
TransactionReceipt receipt = contract.vote(BigInteger.valueOf(candidateIndex)).send();
System.out.println("Vote submitted. Transaction Hash: " + receipt.getTransactionHash());
}
private static void showCandidates() throws Exception {
List<VotingContract.Candidate> candidates = contract.getCandidates().send();
for (int i = 0; i < candidates.size(); i++) {
VotingContract.Candidate candidate = candidates.get(i);
System.out.println("Candidate " + i + ": " + candidate.name + " - Votes: " + candidate.voteCount);
}
}
}
curl -L get.web3j.io | sh
web3j generate solidity -a /path/to/your/contract.abi -b /path/to/your/contract.bin -o /path/to/your/src/main/java -p com.yourpackage