Advertisement
Guest User

Result

a guest
Jul 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. /*
  2. Pontus Eklund
  3. poek1067
  4. */
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. public class Result implements Comparable<Result>{
  9. private Event event;
  10. private double result;
  11. private Participant participant;
  12. private ArrayList<Result> participantResultList = new ArrayList<>();
  13.  
  14. public Result(Participant participant, Event event, double result) {
  15. this.participant = participant;
  16. this.event = event;
  17. this.result = result;
  18. }
  19.  
  20.  
  21. public void addParticipantResult(Result r) {
  22. this.participantResultList.add(r);
  23. }
  24.  
  25. public ArrayList<Result> getParticipantResultList() {
  26. return this.participantResultList;
  27. }
  28.  
  29. public Participant getParticipant() {
  30. return this.participant;
  31. }
  32.  
  33. public Event getEvent() {
  34. return event;
  35. }
  36.  
  37. public double getResult() {
  38. return result;
  39. }
  40.  
  41. public String toString() {
  42. return ""+this.result;
  43. }
  44.  
  45. @Override
  46. public int compareTo(Result o) {
  47. if (this.result<o.result){
  48. return 1;
  49. }
  50. if(this.result>o.result){
  51. return -1;
  52. }
  53. participant.getFullName().compareTo(o.participant.getFullName());
  54. return 1;
  55. }
  56.  
  57. private static Participant findParticipant(int id) {
  58. for (Participant p : Participant.participantList) {
  59. if (p.getNumber() == id) {
  60. return p;
  61. }
  62. }
  63. return null;
  64. }
  65.  
  66. private static Event findEvent(String name) {
  67. for (int i = 0; i < Event.events.size(); i++) {
  68. if (Event.events.get(i).getSport().equalsIgnoreCase(name)) {
  69. return Event.events.get(i);
  70. }
  71. }
  72. return null;
  73. }
  74.  
  75. public static void addResult(Scanner myScanner) {
  76. System.out.println("Number: ");
  77. int number = myScanner.nextInt();
  78. myScanner.nextLine();
  79. Participant p = findParticipant(number);
  80. if (p == null) {
  81. System.out.println("Error, participant with number " + number + " does not exist");
  82. return;
  83. }
  84.  
  85. System.out.println("Event: ");
  86. String event = myScanner.nextLine();
  87. Event e = findEvent(event);
  88. if (e == null) {
  89. System.out.println("Error, event with name " + event + " does not exist");
  90. return;
  91. }
  92.  
  93. System.out.println("Result: ");
  94. double resultA = myScanner.nextDouble();
  95. myScanner.nextLine();
  96.  
  97. while (resultA <= 0){
  98. System.out.println("Error, must be greater or equal to 0");
  99. resultA = myScanner.nextDouble();
  100. myScanner.nextLine();
  101. }
  102.  
  103. Result result = new Result(p, e, resultA);
  104. e.addResult(result);
  105. p.addResult(result);
  106. ArrayList<Result> results = new ArrayList<>();
  107. for (int i = 0; i < p.getResultList().size(); i++) {
  108. if (p.getResultList().get(i).getEvent().getSport().equals(event)) {
  109. p.getResultList().get(i).addParticipantResult(result);
  110. results = p.getResultList().get(i).getParticipantResultList();
  111. break;
  112. }
  113. }
  114.  
  115. System.out.println("Results for " + p.getFullName() + " from " + p.getTeam() + " in " + e.getSport() + ": " + results);
  116. }
  117. }import java.util.ArrayList;
  118. import java.util.Scanner;
  119.  
  120. public class Result implements Comparable<Result>{
  121. private Event event;
  122. private double result;
  123. private Participant participant;
  124. private ArrayList<Result> participantResultList = new ArrayList<>();
  125.  
  126. public Result(Participant participant, Event event, double result) {
  127. this.participant = participant;
  128. this.event = event;
  129. this.result = result;
  130. }
  131.  
  132.  
  133. public void addParticipantResult(Result r) {
  134. this.participantResultList.add(r);
  135. }
  136.  
  137. public ArrayList<Result> getParticipantResultList() {
  138. return this.participantResultList;
  139. }
  140.  
  141. public Participant getParticipant() {
  142. return this.participant;
  143. }
  144.  
  145. public Event getEvent() {
  146. return event;
  147. }
  148.  
  149. public double getResult() {
  150. return result;
  151. }
  152.  
  153. public String toString() {
  154. return ""+this.result;
  155. }
  156.  
  157. @Override
  158. public int compareTo(Result o) {
  159. if (this.result<o.result){
  160. return 1;
  161. }
  162. if(this.result>o.result){
  163. return -1;
  164. }
  165. participant.getFullName().compareTo(o.participant.getFullName());
  166. return 1;
  167. }
  168.  
  169. private static Participant findParticipant(int id) {
  170. for (Participant p : Participant.participantList) {
  171. if (p.getNumber() == id) {
  172. return p;
  173. }
  174. }
  175. return null;
  176. }
  177.  
  178. private static Event findEvent(String name) {
  179. for (int i = 0; i < Event.events.size(); i++) {
  180. if (Event.events.get(i).getSport().equalsIgnoreCase(name)) {
  181. return Event.events.get(i);
  182. }
  183. }
  184. return null;
  185. }
  186.  
  187. public static void addResult(Scanner myScanner) {
  188. System.out.println("Number: ");
  189. int number = myScanner.nextInt();
  190. myScanner.nextLine();
  191. Participant p = findParticipant(number);
  192. if (p == null) {
  193. System.out.println("Error, participant with number " + number + " does not exist");
  194. return;
  195. }
  196.  
  197. System.out.println("Event: ");
  198. String event = myScanner.nextLine();
  199. Event e = findEvent(event);
  200. if (e == null) {
  201. System.out.println("Error, event with name " + event + " does not exist");
  202. return;
  203. }
  204.  
  205. System.out.println("Result: ");
  206. double resultA = myScanner.nextDouble();
  207. myScanner.nextLine();
  208.  
  209. while (resultA <= 0){
  210. System.out.println("Error, must be greater or equal to 0");
  211. resultA = myScanner.nextDouble();
  212. myScanner.nextLine();
  213. }
  214.  
  215. Result result = new Result(p, e, resultA);
  216. e.addResult(result);
  217. p.addResult(result);
  218. ArrayList<Result> results = new ArrayList<>();
  219. for (int i = 0; i < p.getResultList().size(); i++) {
  220. if (p.getResultList().get(i).getEvent().getSport().equals(event)) {
  221. p.getResultList().get(i).addParticipantResult(result);
  222. results = p.getResultList().get(i).getParticipantResultList();
  223. break;
  224. }
  225. }
  226.  
  227. System.out.println("Results for " + p.getFullName() + " from " + p.getTeam() + " in " + e.getSport() + ": " + results);
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement