Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. import java.util.*;
  2. public class Event {
  3.  
  4. private String name;
  5. private int maxAttempts;
  6. private boolean biggerBetter;
  7. private ArrayList<Result> results;
  8.  
  9. /**
  10. * Konstruktor för gren-klassen
  11. */
  12. public Event(String name, int maxAttempts, String biggerBetterAns) {
  13. this.name = name;
  14. this.maxAttempts = maxAttempts;
  15.  
  16. if (biggerBetterAns.equals("yes") || biggerBetterAns.equals("y")) {
  17. biggerBetter = true;
  18. } else {
  19. biggerBetter = false;
  20. }
  21.  
  22. results = new ArrayList<Result>();
  23. }
  24.  
  25.  
  26. public void printResults() {
  27. Result res;
  28. int rank = 1;
  29. int numSameRes = 0;
  30.  
  31. for (int i = 0; i < results.size(); i++) {
  32. res = results.get(i);
  33.  
  34. //Kolla om samma resultat finns
  35. if (i != 0) {
  36. if (res.getResult() == results.get(i-1).getResult()) {
  37. numSameRes++;
  38. } else {
  39. if (i == 1) {
  40. rank++;
  41. }
  42. rank = rank + numSameRes;
  43. numSameRes = 1;
  44. }
  45. }
  46.  
  47. System.out.println(rank + " " + res.getResult() + " " + res.getName());
  48. }
  49. }
  50.  
  51. /**
  52. * Lägg till resultat
  53. */
  54. public void addResult(double result, String participantName, String team) {
  55. Result res;
  56. Result existingRes;
  57. boolean foundBetterResult = false;
  58.  
  59. // kolla efter tidigare resultat för denna deltagare
  60. for (int i = 0; i < results.size(); i++) {
  61. existingRes = results.get(i);
  62.  
  63. //deltagarnamnjämförelse
  64. if (existingRes.getName().equals(participantName)) {
  65. if (biggerBetter) {
  66. if (existingRes.getResult() < result) {
  67. foundBetterResult = true;
  68. break; // avsluta loop
  69. }
  70. } else {
  71. if (existingRes.getResult() > result) {
  72. foundBetterResult = true;
  73. break; // avsluta loop
  74. }
  75. }
  76. }
  77. }
  78.  
  79. if (foundBetterResult) {
  80. return; // avsluta metod
  81. }
  82.  
  83.  
  84. // skapa nytt resultat
  85. res = new Result(result, participantName, name, team);
  86.  
  87. //Kolla var i listan resultatet ska ligga
  88. for (int i = 0; i < results.size(); i++) {
  89.  
  90. existingRes = results.get(i);
  91.  
  92. if (biggerBetter) {
  93.  
  94. if (result > existingRes.getResult()) {
  95. results.add(i, res); //spara på rätt plats i sorterad lista
  96. return; // avsluta metod
  97. }
  98.  
  99. } else {
  100.  
  101. if (result < existingRes.getResult()) {
  102. results.add(i, res); //spara på rätt plats i sorterad lista
  103. return; //avsluta metod
  104. }
  105. }
  106. }
  107.  
  108. //spara i sorterad lista
  109. results.add(res);
  110. }
  111.  
  112. // returnerar en matris med 4 värden per resultat: antal guld, antal silver, antal brons och ett hashvärde för teamnamnet
  113. public int[][] getTeamResults() {
  114.  
  115. int[][] teamResults;
  116. teamResults = new int[0][4]; // programmet stödjer max 100 olika lag
  117. int index = 0;
  118.  
  119. String team;
  120. Result res;
  121. int rank = 1;
  122. int numSameRes = 0;
  123.  
  124. for (int i = 0; i < results.size(); i++) {
  125.  
  126. res = results.get(i);
  127. team = res.getTeam();
  128.  
  129. //lägg till team i hashmap
  130. teamResults = incrementMatrixRowLength(teamResults);
  131. teamResults[index][0] = 0;
  132. teamResults[index][1] = 0;
  133. teamResults[index][2] = 0;
  134. teamResults[index][3] = team.hashCode();
  135.  
  136. //Kolla om samma resultat finns
  137. if (i != 0) {
  138. if (res.getResult() == results.get(i-1).getResult()) {
  139. numSameRes++;
  140. } else {
  141. if (i == 1) {
  142. rank++;
  143. }
  144. rank = rank + numSameRes;
  145. numSameRes = 1;
  146.  
  147. if (rank > 3) {
  148. break;
  149. }
  150. }
  151. }
  152.  
  153. teamResults[index][rank-1]++;
  154. index++;
  155.  
  156. }
  157.  
  158. return teamResults;
  159.  
  160. }
  161.  
  162. /**
  163. * Lägg till 1 rad och bevara data (för int matris)
  164. */
  165. private static int[][] incrementMatrixRowLength(int[][] a) {
  166.  
  167. int[][] newArray;
  168.  
  169. //skapa ny array med dubbel längd
  170. newArray = new int[a.length+1][4];
  171.  
  172. //för över all data till ny array
  173. for(int i = 0; i < a.length; i++) {
  174. for(int j = 0; j < a[0].length; j++) {
  175. newArray[i][j] = a[i][j];
  176. }
  177. }
  178.  
  179. return newArray;
  180.  
  181. }
  182.  
  183. public String toString() {
  184. return name;
  185. }
  186.  
  187. public String getName() {
  188. return name;
  189. }
  190.  
  191. public int getMaxAttempts() {
  192. return maxAttempts;
  193. }
  194.  
  195. public boolean getBiggerBetter() {
  196. return biggerBetter;
  197. }
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement