📄 electionframe.java
字号:
ballot.addCandidate(id);
}
ballotPapers.add(ballot);
}
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
System.exit(1);
}
}// initBallotPapers end
public void doElect()
{
Candidate candidate = new Candidate() ;
int CandidateNumber;
curRound ++;
CandidateNumber = candidateList.getNumberOfCandidates();
if (curRound == 1)
{
firstElect();
for (int i = 0; i<CandidateNumber ; i++)
{
showStr += candidateList.getCandidate(i).getId() + " " ;
}
showStr += '\n';
for (int i = 0; i<CandidateNumber ; i++)
{
showStr += candidateList.getCandidate(i).getVotesNumber() + " ";
}
showStr += '\n';
showStr += '\n';
}
else
{
otherElect();
for (int i = 0; i<CandidateNumber ; i++)
{
if(candidateList.getCandidate(i).isEliminated())
{
showStr += "-" + " ";
}
else
{
showStr += candidateList.getCandidate(i).getVotesNumber() +
candidateList.getCandidate(i).getOtherVotesNumber()+" ";
}
}
showStr += '\n';
showStr += '\n';
}
if (isEnd())
{
electionRoundButton.setEnabled(false);
candidate = candidateList.getWinner();
if (candidate !=null)
{
showStr += candidate.getId() + ":";
showStr += candidate.getName();
showStr += " is elected" + '\n';
}
else
{
showStr += " No one is elected" + '\n';
}
showVote(showStr);
}
else
{
candidate = eliminateCandidate();
showStr += candidate.getId() + ",";
showStr += candidate.getName();
showStr += " is eliminated" + '\n';
showVote(showStr);
}
updateDisplays();
}//doElect() end
public void firstElect()
{
Candidate candidate = new Candidate();
int i;
for(i = 0 ; i< ballotPapers.getNumberOfBallot() ; i++)
{
candidate = ballotPapers.getBallot(i).getCandidate(curRound);
candidateList.find(candidate.getId()).addVotesNumber();
}
}
public void otherElect()
{
boolean isAdd = true;
String id;
int i;
int j ;
for(i = 0 ; i< ballotPapers.getNumberOfBallot() ; i++ )
{
Candidate candidate = new Candidate();
// Judge if all the candidate who list before be elimitated
// if is ture add for this candidate
// else next ballot
for (j = 1 ; j< curRound ; j++ )
{
id = ballotPapers.getBallot(i).getCandidate(j).getId();
candidate = candidateList.find(id);
isAdd = true ;
if (candidate.isEliminated() == false)
{
isAdd = false;
break;
}
}
if(isAdd)
{
id = ballotPapers.getBallot(i).getCandidate(curRound).getId();
candidateList.find(id).addOtherVotesNumber();
}
}
}
public Candidate eliminateCandidate()
{
int min;
int voteNumber;
int index;
int numOfCandidate;
int i;
Candidate candidate = new Candidate();
numOfCandidate = candidateList.getNumberOfCandidates();
candidate = candidateList.getCandidate(0);
min = candidate.getVotesNumber() + candidate.getOtherVotesNumber();
index = 0;
for (i = 1 ; i < numOfCandidate ; i++ )
{
candidate = candidateList.getCandidate(i);
if (candidate.isEliminated() == false)
{
voteNumber = candidate.getVotesNumber() + candidate.getOtherVotesNumber();
if (voteNumber < min)
{
min = voteNumber;
index = i;
}
}
}
candidateList.getCandidate(index).setEliminated();
candidateList.getCandidate(index).setEliminatedRound(curRound);
return candidateList.getCandidate(index) ;
}
// Judge if the election is end
public boolean isEnd()
{
Candidate candidate = new Candidate();
int i;
int index = 0;
boolean endBefore = false;
double ratio;
int unEliminatedNumber = 0;
for (i = 0 ; i < candidateList.getNumberOfCandidates() ; i++ )
{
candidate = candidateList.getCandidate(i);
ratio = (double)(candidate.getVotesNumber()+candidate.getOtherVotesNumber()) / ballotPapers.getNumberOfBallot();
if (ratio>0.5)
{
index = i;
endBefore = true;
break;
}
if(candidate.isEliminated()) continue;
index = i;
unEliminatedNumber ++;
if (unEliminatedNumber > 1) return false;
}
candidateList.getCandidate(index).setElected();
candidateList.setWinner(candidate);
if (endBefore)
{
for (i = 0 ; i < candidateList.getNumberOfCandidates() ; i++ )
{
candidate = candidateList.getCandidate(i);
if (candidate.isEliminated() == false && candidate.isElected() == false)
{
candidateList.getCandidate(i).setEliminated();
candidateList.getCandidate(i).setEliminatedRound(curRound);
}
}
}
return true;
}
// display bar chart in seperate frame
public void doBarChart()
{
aChartFrame = new ChartFrame(candidateList);
aChartFrame.setLocation(0, 400);
aChartFrame.show();
}
// exit program
public void doClose()
{
System.exit(0);
}
// Find details for a candidate
public boolean doFind(String candidateId)
{
int round ;
Candidate aCandidate = candidateList.find(candidateId); // find Candidate
if (aCandidate == null) // if the Candidate not exists
{
JOptionPane.showMessageDialog(null, "Candidate does not exist");
return false;
}
else
{
candidateName = aCandidate.getName();
firstVotesNumber = aCandidate.getVotesNumber();
otherVotesNumber = aCandidate.getOtherVotesNumber();
round = aCandidate.getEliminatedRound();
elected = aCandidate.isElected();
return true;
}
}//doFind() end
// show the vote of every round to screen display
public void showVote(String voteStr)
{
displayArea.setText(voteStr); // add the string to text area
outputFile(voteStr);
}
public void outputFile(String voteStr)
{
try
{
FileWriter fw = new FileWriter ("ElectionOut.txt",false);
BufferedWriter bw = new BufferedWriter (fw);
bw.write(voteStr);
bw.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
System.exit(1);
}
}
// update bar chart if they are showing
public void updateDisplays()
{
if (aChartFrame != null && aChartFrame.isShowing())
{
aChartFrame.dispose();
doBarChart();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -