Professional Documents
Culture Documents
ST Lab AutomationTesting
ST Lab AutomationTesting
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();
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){
}
}
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();
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{
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));
}
PreparedStatement pst =
ConnectDB.connectDB().prepareStatement(insert);
pst.setString(1,party);
pst.setString(2,cand);
pst.setString(3,region);
pst.executeUpdate();
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();
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
package vote;
import org.junit.Test;
@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
package vote;
import org.junit.Test;
import java.sql.Connection;
@Test
//DBTestScript#2
public void connectdB() throws Exception {
if(ConnectDB.connectDB() instanceof Connection){
assertEquals(null,ConnectDB.connectDB);
}
}
ConnectDB – Output#1
ConnectDB – Output#2
package vote;
import org.junit.Test;
import java.util.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