Events

mail@pastecode.io avatar
unknown
java
2 years ago
3.2 kB
3
Indexable
Never
package ee.ttu.algoritmid.events;

import java.util.*;


public class Events {

    private final int maxParticipants;
    // Key: Event, Value: Participants
    private final Map<Event, Integer> eventsMap = new HashMap<>();

    public Events(int maxParticipants) {
        this.maxParticipants = maxParticipants;

    }

    public void registerParticipant(String eventName, int eventLengthMinutes, boolean freeTickets) {
        Event event = new Event(eventName, eventLengthMinutes, freeTickets);

        eventsMap.putIfAbsent(event, 0);

        if (eventsMap.get(event) < maxParticipants) {
            eventsMap.put(event, eventsMap.get(event) + 1);
        }
    }

    public int eventPopularity(String eventName, int eventLengthMinutes, boolean freeTickets) {
        Event event = new Event(eventName, eventLengthMinutes, freeTickets);

        if (eventsMap.get(event) != null) {
            return eventsMap.get(event);
        }

        return 0;
    }

    public int getTop1Participants() {
        Collection<Integer> eventParticipants = eventsMap.values();

        try {
            return Collections.max(eventParticipants);
        } catch (NoSuchElementException e) {
            // No events exist, return 0
            return 0;
        }
    }

    public List<Integer> getTop2Participants() {
        Collection<Integer> eventParticipants = eventsMap.values();
        List<Integer> participantList = new ArrayList<>(eventParticipants);

        participantList.sort(Collections.reverseOrder());

        if (participantList.size() == 2) {
            return participantList;
        }
        if (participantList.size() > 2) {
            return participantList.subList(0, 2);
        }
        if (participantList.size() == 1) {
            // only 1 event exists return that events popularity and a 0

            participantList.add(0);
            return participantList;
        }

        // No events exist return list of [0, 0]
        return new ArrayList<Integer>() {
            {
                add(0);
                add(0);
            }
        };
    }
}

class Event {
    private final String eventName;
    private final boolean freeTickets;
    private final int eventLengthInMinutes;
    private final int hashCode;

    public Event(String eventName, int eventLengthMinutes, boolean freeTickets) {
        this.eventName = eventName;
        this.eventLengthInMinutes = eventLengthMinutes;
        this.freeTickets = freeTickets;
        this.hashCode = Objects.hash(eventName, eventLengthMinutes, freeTickets);
    }
    
    //https://www.baeldung.com/java-custom-class-map-key
    
    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        Event that = (Event) o;
        return eventName == that.eventName && eventLengthInMinutes == that.eventLengthInMinutes && freeTickets == that.freeTickets;
    }

    @Override
    public int hashCode() {
        return this.hashCode;
    }
}