2

I have 3 different classes; Contestant, Event and Result.

public class Contestant {

public static ArrayList<Contestant> allContestants = new ArrayList<>();

private int contestantId;
private String firstName;
private String lastName;

public Contestant (int contestantId, String firstName, String lastName) {
    this.contestantId = contestantId;
    this.firstName = firstName;
    this.lastName = lastName;
}

public class Event {

public static ArrayList<Event> allEvents = new ArrayList<>();

private String eventName;

public Event (String eventName) {
    this.eventName = eventName;
}

public class Result {

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

private double result;
private int attemptNumber;

public Result (double result, int attemptNumber) {
    this.result = result;
    this.attemptNumber = attemptNumber:
}

The classes have different methods for adding new Contestant objects, new Event objects and new Result objects to each ArrayList. Each contestant can participate in multiple events and for each event they can create multiple results.

What I want to achieve is for each Result object to reference a Contestant ArrayList object as well as an Event ArrayList object - how can I best link them?

2
  • why you want to refer the complete arraylist objects of all contestants and all events, is it fine to refer only a single event or single contestant corresponding to that result Commented Jan 12, 2017 at 10:26
  • Create a ResultList of Result and pass it to Event, create EventList of Event and pass it to Contestent. Outside all these class make CollectionList of Collection. Commented Jan 12, 2017 at 10:32

1 Answer 1

2

Your event class should be like this ,instead of array list you can use Hashmap.

public class Event {
//In this you can have contestantId as key and Event as value
public static Map<String, Event> allEvents = new HashMap<String, Event>();

private String eventName;

public Event (String eventName) {
    this.eventName = eventName;
}

And your result class should be like this:

public class Result {
//In this you can have eventName as key and Result as value
public static Map<String, Result> allResults = new HashMap<String, Result>();

private double result;
private int attemptNumber;

public Result (double result, int attemptNumber) {
    this.result = result;
    this.attemptNumber = attemptNumber:
}
Sign up to request clarification or add additional context in comments.

1 Comment

With Result you can access eventName and with eventName you can have contestantId

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.