-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElection.java
More file actions
84 lines (73 loc) · 2.26 KB
/
Election.java
File metadata and controls
84 lines (73 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.*;
public class Election
{
private String title = "DEFAULT";
private ArrayList<Candidate> candidates = new ArrayList<Candidate>();
private Random rand = new Random();
public Election(String inTitle, ArrayList<Candidate> newList)
{
setTitle(inTitle);
setCandidates(newList);
}
public String toString()
{
return "This is the " + getTitle() + " election. The Candates participating are:\n" + getCandidates() + "\n" + declareWinner() + "\n";
}
public void setTitle(String inTitle)
{
title = inTitle;
}
public String getTitle()
{
return title;
}
public void setCandidates(ArrayList<Candidate> newList)
{
//If using list mode, reset old list
candidates.clear();
//Go through new List
for (Candidate newCand : newList)
{
//Add each newCandidate
addCandidate(newCand);
}
}
//This method not required by OoL #2, but seems like it would be useful
public void addCandidate(Candidate newCand)
{
//Verify no dupes before adding the new Candidate
boolean noMatch = true;
for (Candidate current : candidates)
{
if (newCand.equals(current))
{
noMatch = false;
}
}
if (noMatch)
{
candidates.add(newCand);
}
}
public String getCandidates()
{
String candList = "";
for (Candidate cand : candidates)
{
candList = candList + "\n" + cand;
}
return candList;
}
public String declareWinner()
{
Candidate winner = candidates.get(0);
for (int x = 1; x < candidates.size(); x++)
{
if ( (winner.getMoney() < candidates.get(x).getMoney()) || (rand.nextBoolean() && winner.getMoney() == candidates.get(x).getMoney()) )
{
winner = candidates.get(x);
}
}
return winner.getName() + " is the winner of the " + getTitle() + " election";
}
}