Advertisement
Guest User

Participant

a guest
Jul 22nd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. /*
  2. Pontus Eklund
  3. poek1067
  4. */
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Scanner;
  8.  
  9. public class Participant {
  10. private String name;
  11. private String surName;
  12. private static int number = 100;
  13. private int instanceStartNum = number;
  14. private ArrayList<Result> resultList = new ArrayList<>();
  15. private String team;
  16. static ArrayList<Participant> participantList = new ArrayList<>();
  17.  
  18. public Participant(String name,String surName, String team) {
  19. this.name = name;
  20. this.surName = surName;
  21. this.team = team;
  22. this.instanceStartNum = number++;
  23. }
  24.  
  25. public void addResult(Result res) {
  26. resultList.add(res);
  27. }
  28.  
  29. public void removeResults() {
  30. for (int i = 0; i < resultList.size(); i++) {
  31. resultList.remove(i);
  32. }
  33. }
  34.  
  35.  
  36.  
  37. public ArrayList<Result> getResultList() {
  38. return this.resultList;
  39. }
  40.  
  41. public String getFullName() {
  42. return this.name + " " + this.surName;
  43. }
  44.  
  45. public int getNumber() {
  46. return this.instanceStartNum;
  47. }
  48.  
  49. public String getTeam() {
  50. return this.team;
  51. }
  52.  
  53. public String toString() {
  54. return this.name + " " + this.surName + " " + this.instanceStartNum + " " + this.team;
  55. }
  56.  
  57. private static StringBuilder normalizeString(String string) {
  58. StringBuilder str = new StringBuilder();
  59. for (int i = 0; i < string.length(); i++) {
  60. if (i == 0) {
  61. str.append(Character.toString(string.charAt(i)).toUpperCase());
  62. } else {
  63. str.append(Character.toString(string.charAt(i)).toLowerCase());
  64. }
  65. }
  66. return str;
  67. }
  68.  
  69. public static void addParticipant(Scanner myScanner) {
  70. System.out.println("First name: ");
  71. String name = myScanner.nextLine();
  72. while (name.equals("")){
  73.  
  74. System.out.println("Error, name can't be empty");
  75. name = myScanner.nextLine();
  76. name =name.trim();
  77. }
  78. StringBuilder firstNameBuilder = normalizeString(name.trim());
  79.  
  80. System.out.println("Surname: ");
  81. String surName = myScanner.nextLine();
  82.  
  83. while (surName.equals("")){
  84.  
  85. System.out.println("Error, name can't be empty");
  86. surName = myScanner.nextLine();
  87. surName = surName.trim();
  88. }
  89. StringBuilder surNameBuilder = normalizeString(surName.trim());
  90.  
  91. System.out.println("Team ");
  92. String team = myScanner.nextLine();
  93.  
  94. while (team.equals("")){
  95.  
  96. System.out.println("Error, name can't be empty");
  97. team = myScanner.nextLine();
  98. team =team.trim();
  99. }
  100. StringBuilder teamNameBuilder = normalizeString(team.trim());
  101.  
  102. Participant participant = new Participant(firstNameBuilder.toString(), surNameBuilder.toString(), teamNameBuilder.toString());
  103. participantList.add(participant);
  104. System.out.println(participantList);
  105.  
  106. boolean teamExists = false;
  107. Team t = new Team(team);
  108. for (int i = 0; i < Team.teams.size(); i++) {
  109. if (Team.teams.get(i).getTeamName().equals(team)) {
  110. teamExists = true;
  111. t = Team.teams.get(i);
  112. }
  113. }
  114.  
  115. if (teamExists) {
  116. t.addToTeam(participant);
  117. } else {
  118. Team.teams.add(t);
  119. }
  120. }
  121.  
  122. private static Participant findParticipant(int id) {
  123. for (Participant p : participantList) {
  124. if (p.getNumber() == id) {
  125. return p;
  126. }
  127. }
  128. return null;
  129. }
  130.  
  131. public static void removeParticipant(Scanner myScanner) {
  132. System.out.println("Number: ");
  133. int number = myScanner.nextInt();
  134. myScanner.nextLine();
  135. Participant p = findParticipant(number);
  136. if (p == null) {
  137. System.out.println("Error, participant does not exist");
  138. return;
  139. }
  140.  
  141. for (int i = 0; i < Team.teams.size(); i++) {
  142. if (p.getTeam().equals(Team.teams.get(i).getTeamName())) {
  143. Team.teams.get(i).removeParticipant(p);
  144. if (Team.teams.get(i).getParticipants().isEmpty()) {
  145. Team.teams.remove(Team.teams.get(i));
  146. }
  147. }
  148. }
  149.  
  150. participantList.remove(p);
  151. p.removeResults();
  152. System.out.println(p.getFullName() + " from " + p.getTeam() + " with number " + p.getNumber() + " removed");
  153. System.out.println(participantList);
  154. }
  155.  
  156. public static void participant(Scanner myScanner) {
  157. System.out.println("number: ");
  158. int participantNumber = myScanner.nextInt();
  159. myScanner.nextLine();
  160.  
  161. Participant p = findParticipant(participantNumber);
  162. if (p == null) {
  163. System.out.println("Error, does not exist");
  164. return;
  165. }
  166. ArrayList<Double> result;
  167. boolean found = false;
  168. for (int i = 0; i < Event.events.size(); i++) {
  169. result = new ArrayList<>();
  170. for (int j = 0; j < p.getResultList().size(); j++) {
  171. if (Event.events.get(i).getSport().equals(p.getResultList().get(j).getEvent().getSport())) {
  172. found = true;
  173. result.add(p.getResultList().get(j).getResult());
  174. }
  175. }
  176.  
  177. if (found) {
  178. System.out.println("Results for " + p.getFullName() + " in " + Event.events.get(i) + ": " + result.toString());
  179. }
  180. found = false;
  181. }
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement