-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebate.java
More file actions
150 lines (131 loc) · 4.38 KB
/
Debate.java
File metadata and controls
150 lines (131 loc) · 4.38 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.util.*;
public class Debate
{
private String location = "DEFAULT";
private ArrayList<Candidate> candidates = new ArrayList<Candidate>();
private Random rand = new Random();
/**
*
* @param inLoc Location of where Debate will be
* @param newList ArrayList of Candidates
* @throws TooLowInPollsException
*/
public Debate(String inLoc, ArrayList<Candidate> newList) throws TooLowInPollsException
{
setLocation(inLoc);
setCandidates(newList);
boolean sameParty = true;
if (candidates.size() != 0)
{
String party = candidates.get(0).getParty();
for (Candidate person : candidates)
{
if (!(person.getParty().equals(party)))
{
sameParty = false;
}
}
if (!sameParty)
{
for (Candidate person : candidates)
{
long total = Candidate.getAllMoney();
if (person.getMoney() < total * .03)
{
throw new TooLowInPollsException();
}
}
}
}
}
public String toString()
{
return "This debate takes place at " + getLocation() + ". The Candates participating are:\n" + getCandidates() + "\n";
}
public void setLocation(String inLoc)
{
location = inLoc;
}
public String getLocation()
{
return location;
}
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 duplicates 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()
{
if (candidates.size() == 0)
{
return "NO WINNER";
}
//Points array
int[] points = new int[candidates.size()];
int total = 0;
for (int x = 0; x < candidates.size(); x++)
{
points[x] = (int) (rand.nextInt(101) * candidates.get(x).getDebateMod());
total += points[x];
}
//Determine Winner!
int winnerIndex = -1;
int maxPoints = -1;
for (int x = 0; x < points.length; x++)
{
if (points[x] > maxPoints || (points[x] == maxPoints && rand.nextBoolean()) )
{
maxPoints = points[x];
winnerIndex = x;
}
}
String output = "The winner of this debate is " + candidates.get(winnerIndex).getName() + ". This winning candidate recieved the following payouts:";
//Handle payouts
int payouts = 0;
for (int x = 0; x < candidates.size(); x++)
{
if (x != winnerIndex)
{
int payout = (candidates.get(x).getMoney() * (total - points[x])) / total;
payouts += payout;
candidates.get(x).addMoney(-1 * payout);
candidates.get(winnerIndex).addMoney(payout);
output = output + "\n\t" + candidates.get(x).getName() + " paid out $" + payout;
}
}
return output;
}
}