Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Pontus Eklund
- poek1067
- */
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Result implements Comparable<Result>{
- private Event event;
- private double result;
- private Participant participant;
- private ArrayList<Result> participantResultList = new ArrayList<>();
- public Result(Participant participant, Event event, double result) {
- this.participant = participant;
- this.event = event;
- this.result = result;
- }
- public void addParticipantResult(Result r) {
- this.participantResultList.add(r);
- }
- public ArrayList<Result> getParticipantResultList() {
- return this.participantResultList;
- }
- public Participant getParticipant() {
- return this.participant;
- }
- public Event getEvent() {
- return event;
- }
- public double getResult() {
- return result;
- }
- public String toString() {
- return ""+this.result;
- }
- @Override
- public int compareTo(Result o) {
- if (this.result<o.result){
- return 1;
- }
- if(this.result>o.result){
- return -1;
- }
- participant.getFullName().compareTo(o.participant.getFullName());
- return 1;
- }
- private static Participant findParticipant(int id) {
- for (Participant p : Participant.participantList) {
- if (p.getNumber() == id) {
- return p;
- }
- }
- return null;
- }
- private static Event findEvent(String name) {
- for (int i = 0; i < Event.events.size(); i++) {
- if (Event.events.get(i).getSport().equalsIgnoreCase(name)) {
- return Event.events.get(i);
- }
- }
- return null;
- }
- public static void addResult(Scanner myScanner) {
- System.out.println("Number: ");
- int number = myScanner.nextInt();
- myScanner.nextLine();
- Participant p = findParticipant(number);
- if (p == null) {
- System.out.println("Error, participant with number " + number + " does not exist");
- return;
- }
- System.out.println("Event: ");
- String event = myScanner.nextLine();
- Event e = findEvent(event);
- if (e == null) {
- System.out.println("Error, event with name " + event + " does not exist");
- return;
- }
- System.out.println("Result: ");
- double resultA = myScanner.nextDouble();
- myScanner.nextLine();
- while (resultA <= 0){
- System.out.println("Error, must be greater or equal to 0");
- resultA = myScanner.nextDouble();
- myScanner.nextLine();
- }
- Result result = new Result(p, e, resultA);
- e.addResult(result);
- p.addResult(result);
- ArrayList<Result> results = new ArrayList<>();
- for (int i = 0; i < p.getResultList().size(); i++) {
- if (p.getResultList().get(i).getEvent().getSport().equals(event)) {
- p.getResultList().get(i).addParticipantResult(result);
- results = p.getResultList().get(i).getParticipantResultList();
- break;
- }
- }
- System.out.println("Results for " + p.getFullName() + " from " + p.getTeam() + " in " + e.getSport() + ": " + results);
- }
- }import java.util.ArrayList;
- import java.util.Scanner;
- public class Result implements Comparable<Result>{
- private Event event;
- private double result;
- private Participant participant;
- private ArrayList<Result> participantResultList = new ArrayList<>();
- public Result(Participant participant, Event event, double result) {
- this.participant = participant;
- this.event = event;
- this.result = result;
- }
- public void addParticipantResult(Result r) {
- this.participantResultList.add(r);
- }
- public ArrayList<Result> getParticipantResultList() {
- return this.participantResultList;
- }
- public Participant getParticipant() {
- return this.participant;
- }
- public Event getEvent() {
- return event;
- }
- public double getResult() {
- return result;
- }
- public String toString() {
- return ""+this.result;
- }
- @Override
- public int compareTo(Result o) {
- if (this.result<o.result){
- return 1;
- }
- if(this.result>o.result){
- return -1;
- }
- participant.getFullName().compareTo(o.participant.getFullName());
- return 1;
- }
- private static Participant findParticipant(int id) {
- for (Participant p : Participant.participantList) {
- if (p.getNumber() == id) {
- return p;
- }
- }
- return null;
- }
- private static Event findEvent(String name) {
- for (int i = 0; i < Event.events.size(); i++) {
- if (Event.events.get(i).getSport().equalsIgnoreCase(name)) {
- return Event.events.get(i);
- }
- }
- return null;
- }
- public static void addResult(Scanner myScanner) {
- System.out.println("Number: ");
- int number = myScanner.nextInt();
- myScanner.nextLine();
- Participant p = findParticipant(number);
- if (p == null) {
- System.out.println("Error, participant with number " + number + " does not exist");
- return;
- }
- System.out.println("Event: ");
- String event = myScanner.nextLine();
- Event e = findEvent(event);
- if (e == null) {
- System.out.println("Error, event with name " + event + " does not exist");
- return;
- }
- System.out.println("Result: ");
- double resultA = myScanner.nextDouble();
- myScanner.nextLine();
- while (resultA <= 0){
- System.out.println("Error, must be greater or equal to 0");
- resultA = myScanner.nextDouble();
- myScanner.nextLine();
- }
- Result result = new Result(p, e, resultA);
- e.addResult(result);
- p.addResult(result);
- ArrayList<Result> results = new ArrayList<>();
- for (int i = 0; i < p.getResultList().size(); i++) {
- if (p.getResultList().get(i).getEvent().getSport().equals(event)) {
- p.getResultList().get(i).addParticipantResult(result);
- results = p.getResultList().get(i).getParticipantResultList();
- break;
- }
- }
- System.out.println("Results for " + p.getFullName() + " from " + p.getTeam() + " in " + e.getSport() + ": " + results);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement