Using JAVA
You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner.
This program will have three classes:
Candidate, Results and ElectionApp
Candidate Class: This class records the information for each candidate that is running for office.
Instance variables:
Methods:
Results Class: This class stores all of the candidates in an array using aggregation.
Constants:
Instance variables:
Methods:
ElectionApp:This is your main program. This class will display the menu and drive all the activities.
Methods:
Choose from the following options:
1- Add a candidate
2- Add votes
3- Determine winner
4- Display a list of candidates
5- Display winners
6- Exit
Design Requirements
====================================Candidate.java==========================================
public class Candidate {
String firstName;
String lastName;
String office;
String party;
int totalVotes=0;
boolean winOrLose;
public Candidate(String fName,String lName,String
office,String party){
this.firstName=fName;
this.lastName=lName;
this.office=office;
this.party=party;
}
public Candidate(Candidate customer){
this.firstName=customer.firstName;
this.lastName=customer.lastName;
this.office=customer.office;
this.party=customer.party;
}
//setters
public void setTotalVotes(int totalVotes) {
this.totalVotes = totalVotes;
}
public void setWinOrLose(boolean winOrLose) {
this.winOrLose = winOrLose;
}
//getters for all instance variable
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getOffice() {
return office;
}
public String getParty() {
return party;
}
public int getTotalVotes() {
return totalVotes;
}
public boolean isWinOrLose() {
return winOrLose;
}
public String toString()
{
return "First Name: "+firstName + ",Last Name: " + lastName +
",Party: " + party + ",Office:" + office;
}
//custom toString method to display winner and
votes.......(NOT IN THE REQUIREMENTS)
public String toString_winner(){
return firstName+" "+lastName+"
party:"+party+",Office: "+office+",number_of_votes: "+
totalVotes;
}
public boolean equals(Candidate c) {
return (this.firstName==c.firstName
&&this.lastName==c.lastName&&
this.party==c.party);
}
}
========================================Results.java===========================================
import java.security.AlgorithmConstraints;
import java.util.ArrayList;
import java.util.Scanner;
public class Results {
int BLOCK_SIZE=3;
// ArrayList<Candidate> c;
Candidate[] cand;
int arrayLength;
// ArrayList<String> Office;
String[] office;
int numOffice;
public Results(){
cand= new
Candidate[BLOCK_SIZE];
office=new
String[BLOCK_SIZE];
arrayLength=numOffice=0;
}
public int getArrayLength() {
return arrayLength;
}
public int getNumOffice() {
return numOffice;
}
public String toString(){
String str="";
for(int
i=0;i<cand.length;i++){
str=str+cand[i].toString()+"\n";
}
return str;
}
public boolean hasCandidate(Candidate c){
for(int
i=0;i<cand.length;i++){
if(cand[i]!=null){
if(cand[i].equals(c))return true;
}
}
return false;
}
public boolean isCandidatesFull(){
return
cand.length==arrayLength;
}
public boolean isOfficeFull(){
return
office.length==numOffice;
}
public void addCandidate(){
Scanner sc=new
Scanner(System.in);
System.out.println("Enter first
name");
String fn= sc.nextLine();
System.out.println("Enter last
name");
String ln= sc.nextLine();
System.out.println("Enter
office");
String o= sc.nextLine();
System.out.println("Enter
party");
String p= sc.nextLine();
Candidate candidate=new
Candidate(fn,ln,o,p);
if(hasCandidate(candidate)){
System.out.println("Candidate already on ballot");
}else{
addCandidate(candidate);
//............
arrayLength++;
}
}
//overloaded
private void addCandidate(Candidate c){
boolean
full=isCandidatesFull();
if(full){resizeCandidate();}
for(int
i=0;i<cand.length;i++){
if(cand[i]==null){
cand[i]=c;
break;
}
}
//.............
boolean check=false;
for(int
i=0;i<office.length;i++){
if(office[i]!=null){
if(office[i].equals(c.getOffice())){
check=true;
break;
}
}
}
if(!check){
numOffice++;
if(isOfficeFull()){resizeOffice();}
for(int
i=0;i<office.length;i++){
if(office[i]==null){
office[i]=c.office;
break;
}
}
}
}
public void resizeCandidate(){
Candidate[] temp= new
Candidate[arrayLength+BLOCK_SIZE];
for(int
i=0;i<cand.length;i++){
temp[i]=cand[i];
}
cand=temp;
}
public void resizeOffice(){
String[] temp= new
String[numOffice+BLOCK_SIZE];
for(int
i=0;i<office.length;i++){
temp[i]=office[i];
}
office=temp;
}
//only one vote can be given to the candidate
public void addVotes(){
boolean vote;
Scanner sc=new
Scanner(System.in);
System.out.println("Enter votes for
each candidae");
for(int
i=0;i<cand.length;i++){
if(cand[i]!=null){
System.out.println("do you want to vote for "+cand[i].toString()+"
(true OR false)");
vote=sc.nextBoolean();
if(vote){
cand[i].totalVotes=cand[i].totalVotes+1;
}
}
}
//................
//setting winOrLose=false of every
candidate to false every time you add votes
for(int i=0;i<cand.length;i++){
if(cand[i]!=null){
cand[i].winOrLose=false;
}
}
}
//...................
//(NOT IN THE REQUIREMENTS)
// custom method to add votes to vote for candidate in
numbers (ie more than one vote can be given to a candidate)
public void addVotes2(){
int vote;
Scanner sc=new
Scanner(System.in);
System.out.println("Enter votes for
each candidae");
for(int
i=0;i<cand.length;i++){
if(cand[i]!=null){
//System.out.println("do you want to vote for"+cand[i].toString()+"
(true OR false)");
System.out.println("enter number! of votes to vote for
"+cand[i].toString());
vote=sc.nextInt();
if(vote>0){
cand[i].totalVotes=cand[i].totalVotes+vote;
}
}
}
//setting winOrLose=false of every
candidate to false every time you add votes
for(int
i=0;i<cand.length;i++){
if(cand[i]!=null){
cand[i].winOrLose=false;
}
}
}
public void determineWinner(){
System.out.println("===OFFICE
MENU===");
for(int
i=0;i<office.length;i++){
if(office[i]!=null)System.out.println(office[i]+"\n");
}
System.out.println("===MENU
Ends===\n");
System.out.println("Pleas enter
office you want to detemine winner ! \n");
Scanner sc=new
Scanner(System.in);
String s=sc.nextLine();
Candidate[]
list=createCandidateListByOffice(s);
findHighestVotes(list,list.length);
}
private Candidate[] createCandidateListByOffice(String
off){
Candidate[] temp=null;
ArrayList<Candidate>
temp2=new ArrayList<Candidate>() ;
System.out.println("candidate
running for office-"+off+ "are:");
for(int i=0;i<cand.length;i++){
if(cand[i]!=null){
if(cand[i].getOffice().equals(off)){
System.out.println(cand[i].getFirstName()+"
"+cand[i].getLastName());
temp2.add(cand[i]);
}
}
}
System.out.println();
//for casting Object[] to Candidate[]
Candidate[] arr= new Candidate[temp2.size()];
temp= temp2.toArray(arr);
return temp;
}
private void findHighestVotes(Candidate[] c,int
size){
if(size==1){
if(c[0].getTotalVotes()>=1){
c[0].winOrLose=true;
// set win or lose in orignal candidate array
for(int i=0;i<cand.length;i++){
if(cand[i]!=null){
if(cand[i].equals(c[0])){
cand[i].winOrLose=true;
}
}
}
}
}
if(size>1){
int tempcount=0;
int tempWinnerIndex=0;
//...............
// getting index of the highest voted candidate
for(int i=0;i<c.length;i++){
if(c[i].getTotalVotes()>tempcount){
tempcount=c[i].getTotalVotes();
tempWinnerIndex=i;
}
}
// checking if any other candiadates have same number
of votes
int tempi=0;
for(int i=0;i<c.length;i++){
if(c[i].getTotalVotes()==c[tempWinnerIndex].getTotalVotes()){
tempi++;
}
}
if(tempi>1){
System.out.println("Following
candidte has same number of votes no one wins");
for(int
i=0;i<c.length;i++){
if(c[i].getTotalVotes()==c[tempWinnerIndex].getTotalVotes()){
System.out.println(c[i].toString());
}
}
System.out.println();
}else{
// set win or lose in orignal
candidate array
for(int i=0;i<cand.length;i++){
if(cand[i]!=null){
if(cand[i].equals(c[tempWinnerIndex])){
cand[i].winOrLose=true;
}
}
}
}
}
}
public void displayWinners(){
//determineWinner();
System.out.println("candidate who
won are");
for(int
i=0;i<cand.length;i++){
if(cand[i]!=null){
if(cand[i].winOrLose==true){
System.out.println(cand[i].toString_winner());
}
}
}
}
public void displayListOfCandidates(){
for(int
i=0;i<cand.length;i++){
if(cand[i]!=null){
System.out.println(cand[i].toString());
}
}
}
}
=================================== ElectionApp.java==========================
import java.util.Scanner;
public class ElectionApp {
public static void main(String[] args) {
Results r=new Results();
// TODO Auto-generated method
stub
while(true){
System.out.println("Choose from the
following options: \n"+
"1- Add a candidate \n"+
"2- Add votes \n"+
"3- Determine winner \n"+
"4- Display a list of candidates
\n"+
"5- Display winners \n"+
"6- Exit\n");
Scanner sc=new
Scanner(System.in);
int choice = sc.nextInt();
switch(choice){
case 1:
r.addCandidate();break;
case 2:
//r.addVotes();
r.addVotes2();break;
case 3:
r.determineWinner();break;
case
4:r.displayListOfCandidates();break;
case 5:r.displayWinners();
break;
case 6: System.exit(0);
}
}
}
}
Using JAVA You have been asked to write a program that can manage candidates for an...