Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

SWE3006 – Advanced Software Testing

Automation Testing – Voting System

Reg No: 15MIS0087


Name: P.Ragavendhar
Slot: L43+L44

Code

Module : Home

package vote;
import java.sql.*;
import java.util.Calendar;
import java.util.Scanner;
class Home {
public static void main(String[] d){
System.out.println("\n*-*-*-Voting System-*-*-*");
Scanner scan = new Scanner(System.in);
System.out.print("1.Login\n2.Register\n\n");
System.out.print("Enter Your Choice : ");
int choice = scan.nextInt();
if (choice == 1){
if (Login.adminOrvoter()){
//Voter
System.out.print("Enter Aadhaar Number : ");
scan.nextLine();
String aadhaar = scan.nextLine();
System.out.print("Enter Password : ");
String password = scan.nextLine();
Login.loginSystem(aadhaar, password);
}else {
//Admin
System.out.print("Enter Admin Username : ");
scan.nextLine();
String uname = scan.nextLine();
System.out.print("Enter Admin Password : ");
String pass = scan.nextLine();
if (uname.equals("admin") && pass.equals("admin")){
while(true){
System.out.println("1.Add Party\n2.Declare Result");
System.out.print("Enter Your Choice : ");
int ch = scan.nextInt();
if (ch == 1){
Admin.addParty();
}else {
Admin.declareResults();
System.exit(0);
}
}
}else{
System.out.println("MESSAGE : Wrong Admin
Credentials..\n");
}
}
}else {
System.out.print("Enter Aadhaar Number : ");
scan.nextLine();
String aadhaar = scan.nextLine();
System.out.print("Enter Name : ");
String name = scan.nextLine();
System.out.print("Enter Password : ");
String pass = scan.nextLine();
System.out.print("Enter Gender : ");
String gender = scan.nextLine();
System.out.print("Enter Age : ");
int age = scan.nextInt();
System.out.print("Enter DoB (YYYY-MM-DD): ");
scan.nextLine();
String DoB = scan.nextLine();
String year = DoB.substring(0,4);
String month = DoB.substring(5,7);
String date = DoB.substring(8,10);
Date D = new
Date(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(dat
e));
System.out.print("Enter Address : ");
String address = scan.nextLine();

Register.registerVoter(aadhaar, name, pass, gender, age, D,


address);
System.out.println("MESSAGE : Successfully Registered!\n");
}
}
}

Module : Login

package vote;
import java.sql.*;
import java.util.Scanner;
class Login {
static String ano;
static String pass;
public static void loginSystem(String aadhaar,String password){
Scanner scan = new Scanner(System.in);
try{
Connection con = ConnectDB.connectDB();
PreparedStatement pst = con.prepareStatement("SELECT
AADHAAR,PASSWORD FROM VOTER WHERE AADHAAR = ? AND
PASSWORD = ?");
pst.setString(1,aadhaar);
pst.setString(2,password);
ResultSet rs = pst.executeQuery();
if (rs.next()){
System.out.println("MESSAGE : Login Successful...");
while(true){
System.out.println("\n1.Vote\n2.View Party");
System.out.print("Enter Your Choice : ");
int ch = scan.nextInt();
if (ch == 1){
Vote.Vote();
}else {
Vote.viewParty();
}
}
}else{
System.out.println("MESSAGE : Invalid Credentials\n");
}
}catch(Exception e){
}
}

public static boolean adminOrvoter(){


Scanner sc = new Scanner(System.in);
System.out.print("1.Voter Login\n2.Admin Login\n");
System.out.print("Enter Your Choice : ");
int choice = sc.nextInt();
if (choice == 1)
return true;
else
return false;
}

public void viewPartyList(){

Module: Register

package vote;
import java.sql.*;
import java.util.Date;
class Register {
public static void registerVoter(String aadhaar,String name,String
pass,String gender,int age,Date dob,String address){
try{
Connection con = ConnectDB.connectDB();
PreparedStatement pst = con.prepareStatement("INSERT INTO
VOTER VALUES (?,?,?,?,?,?,?)");
pst.setString(1,aadhaar);
pst.setString(2,name);
pst.setString(3,pass);
pst.setInt(4,age);
pst.setString(5,gender);
pst.setDate(6,new java.sql.Date(dob.getTime()));
pst.setString(7,address);
pst.executeUpdate();

System.out.println("MESSAGE : Registered Successfully!");


Home.main(new String[]{});
}catch(Exception e){
System.err.println(e.getMessage());
}
}
}

Module : Admin

package vote;
import java.sql.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
class Admin{

static String uname = "admin";


static String pass = "admin";
// String[] results = new String[2];
static String getAdminUname(){
return uname;
}

static String getAdminPass(){


return pass;
}

public static String[] declareResults(){

System.out.println();
try{
String query = "SELECT PARTYNAME,VOTES FROM PARTY";
Connection con = ConnectDB.connectDB();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
HashMap<String,Integer> party = new HashMap<>();
while(rs.next()){
party.put(rs.getString(1),rs.getInt(2));
}

for (Entry<String, Integer> entry : party.entrySet()) {


System.out.println("Party Name : " + entry.getKey() + "\tVotes :
" + entry.getValue());
}

int maxVote = Collections.max(party.values());


String partyName = "";

for (Entry<String, Integer> entry : party.entrySet()) {


if (entry.getValue() == maxVote) {
System.out.println(entry.getKey() + " has won the election
securing " + entry.getValue() + " votes.");
partyName = entry.getKey();
}
}
return new String[]{partyName,Integer.toString(maxVote)};
// System.exit(0);
}catch(Exception e){
}
return new String[]{};
}

public static void addParty(){


Scanner scan = new Scanner(System.in);
try {
String insert = "INSERT INTO PARTY VALUES (?,?,?,0)";

System.out.print("Enter Party Name : ");


String party = scan.nextLine();
System.out.print("Enter Candidate Name : ");
String cand = scan.nextLine();
System.out.print("Enter Region : ");
String region = scan.nextLine();

PreparedStatement pst =
ConnectDB.connectDB().prepareStatement(insert);
pst.setString(1,party);
pst.setString(2,cand);
pst.setString(3,region);

pst.executeUpdate();

System.out.println("MESSAGE : Party has been added!\n");


}catch(Exception e){
System.out.println(e.getMessage());
}
}

Module: ConnectDB

package vote;
import java.sql.*;
class ConnectDB {
public static Connection connectDB(){
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/spm","root","
");
}catch(Exception e){
System.out.println(e.getMessage());
con = null;
}
return con;
}
}

Module: Vote

package vote;
import java.sql.*;
import java.util.HashMap;
import java.util.Scanner;
class Vote{
public static void Vote(){
try{
Scanner scan = new Scanner(System.in);
Connection con = ConnectDB.connectDB();

System.out.print("Enter Your Party Name : ");


String party = scan.nextLine();

String voteQuery = "UPDATE PARTY SET VOTES = VOTES + 1


WHERE PARTYNAME = ?";

PreparedStatement pst = con.prepareStatement(voteQuery);


pst.setString(1,party);

pst.executeUpdate();
System.out.println("MESSAGE : Voted Successfully!\n");
System.exit(0);

}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static HashMap viewParty(){
HashMap<String,String> partyList = new HashMap<>();
try{
Connection con = ConnectDB.connectDB();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT
PARTYNAME,CANDIDATE,REGION FROM PARTY");
while(rs.next()){
partyList.put(rs.getString(1),rs.getString(2));
System.out.print("Party Name : " + rs.getString(1) + "\tCandidate : "
+ rs.getString(2) + "\tRegion : " + rs.getString(3) + "\n");
}
}catch(Exception e){
System.out.println(e.getMessage());
return null;
}
return partyList;
}

}
Application Output

JUnit

Admin Test Script

package vote;

import org.junit.Test;

import static org.junit.Assert.*;


public class AdminTest {
@Test
public void testAdminUsername() {
assertEquals("admin",Admin.getAdminUname());
}

@Test
public void testAdminPassword() throws Exception {
assertEquals("admin",Admin.getAdminPass());
}

@Test
public void testResults() throws Exception {
assertArrayEquals(Admin.declareResults(),new
String[]{"ADMK","16"});
}
}

Admin – Output#1

Admin – Output#2
Admin – Output#3

Connect DB Test Script

package vote;

import org.junit.Test;

import java.sql.Connection;

import static org.junit.Assert.*;

public class ConnectDBTest {


@Test
//DBTestScript#1
public void connectDB() throws Exception {
if(ConnectDB.connectDB() instanceof Connection){
assertEquals("class
com.mysql.jdbc.JDBC4Connection",ConnectDB.connectDB().getClass().
toString());
}
}

@Test
//DBTestScript#2
public void connectdB() throws Exception {
if(ConnectDB.connectDB() instanceof Connection){
assertEquals(null,ConnectDB.connectDB);
}
}

ConnectDB – Output#1

ConnectDB – Output#2

Vote Test Script

package vote;

import org.junit.Test;

import java.util.HashMap;

import static org.junit.Assert.*;

public class VoteTest {


@Test
public void testPartyList() throws Exception {
HashMap<String,String> tempPartyList = new HashMap<>();

tempPartyList.put("ADMK","XYXY");
tempPartyList.put("Congress","XYXY");
tempPartyList.put("DMK","XYXY");
tempPartyList.put("MNM","XYXY");
tempPartyList.put("TCM","XYXY");
tempPartyList.put("DMDK","XYXY");
tempPartyList.put("BJP","XYXY");
tempPartyList.put("QWE","xyxy");
tempPartyList.put("AAA","ZAZAZA");
// tempPartyList.put("KCM","XYXY");

assertEquals(Vote.viewParty(),tempPartyList);
}

Vote – Output#1

You might also like