简体   繁体   中英

Pass a created object from one class to another and add to ArrayList?

I´ve created a function where you add a result for a participant in a event.

Now I want to pass the created object to an addResult-method in my participant class and thereafter add it to an ArrayList in the same class, but I can´t really figure out how to do this. I´ve been stuck on this for a while, and could need some help how to approach this further.

This is what I´ve coded so far for this:

    public Participant getParticipant() {
    int startNumber = readInt();

    boolean participantFound = false;

    for (int i = 0; i < allParticipants.size(); i++) {

        if (allParticipants.get(i).getStartNumber() == (startNumber)) {

            participantFound = true;
            return allParticipants.get(i);
        }

    }

    if (!participantFound) {

        System.out.println("No participant with number " + startNumber + " exists. " + "\n");
        runCommandLoop();
    }

    return null;
}

public Event getEvent() {
    String eventName = norm();

    boolean eventFound = false;

    for (int a = 0; a < allEvents.size(); a++) {

        if (allEvents.get(a).getEventName().equalsIgnoreCase(eventName)) {
            eventFound = true;

            return allEvents.get(a);


        }

    }

    if (!eventFound) {

        System.out.println("No event called " + eventName + " found! ");
        runCommandLoop();
    }

    else {

        System.out.println("To many attempts! ");
        runCommandLoop();
    }

    return null;

}

public void addResult() {

    System.out.println("Number: ");
    Participant p = getParticipant();

    if (p == null) {

    }

    System.out.println("Event: ");
    Event e = getEvent();

    if (e == null) {

    }

    System.out.println("Type in the result for " + p.getFirstName() + " " + p.getLastName() + " in "
            + e.getEventName() + ": ");

    double result = readDouble();

    while (result < 0) {
        System.out.println("Must be greater than or equal to zero! Try again: ");
        result = readDouble();

    }

Result r = new Result();

                          **// THIS IS WHERE I´M STUCK RIGHT NOW**

r.addResult();



    System.out.println("The result " + result + " is now registred");
}

And this is my Participant class:

import java.util.ArrayList;

public class Participant {

public ArrayList<Result> allResults = new ArrayList<>();

private String firstName;
private String lastName;
private String team;
private int startNumber;

public Participant(String firstName, String lastName, String team, int startNumber) {

    this.firstName = firstName;
    this.lastName = lastName;
    this.team = team;
    this.startNumber = startNumber;

}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getTeam() {
    return team;
}

public void setTeam(String team) {
    this.team = team;
}

public int getStartNumber() {
    return startNumber;
}

public void setStartNumber(int startNumber) {
    this.startNumber = startNumber;
}

public void addResult(Result r) {

    allResults.add(r);
    }





public String toString() {
    return "\n" + "Name: " + firstName + "\n" + "Last name: " + lastName + "\n" + "Team: " + team + "\n"
            + "Start number: " + startNumber;

}
}

And my Result class:

public class Result {

private double result;

public Result(double result) {


    this.result = result;


}

public double getResult() {
    return result;
}

public void setResult(double result) {
    this.result = result;

}


public void printResult() {
    System.out.println(result);
}


}

What you are doing is

Result r = new Result();
**// THIS IS WHERE I´M STUCK RIGHT NOW**
r.addResult();

but I do not see addResult() Method in Result Class. What you need to do is

p.addResult(r); // p is your Participant

hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM