Define class Event

Define class Event: The program must accept the title, the registration fee and the duration(in minutes) of N events as the input. The program must sort the events based on the following conditions.
– The program must sort the events in ascending order based on the registration fee.
– If two or more events have the same registration fee, then the program must sort those events in ascending order based on the duration.
– If two or more events have the same registration fee and duration, then the program must sort those events based on the name.

Your task is to define the class Event so that the program runs successfully.

Example Input/Output 1:
Input:
5
TreasureHunt 200 60
ProjectExpo 500 180
RapidFireQuiz 200 20
PaperPresentation 400 120
Hackathon 500 180

Output:
RapidFireQuiz 200 20
TreasureHunt 200 60
PaperPresentation 400 120
Hackathon 500 180
ProjectExpo 500 180

Exaplanation:
Here N = 5.
After sorting the events based on the given conditions, the events become
RapidFireQuiz 200 20
TreasureHunt 200 60
PaperPresentation 400 120
Hackathon 500 180
ProjectExpo 500 180

Example Input/Output 2:
Input:
6
ProjectExpo 800 180
TreasureHunt 100 90
Hackathon 800 240
TechQuiz 100 30
HexCoding 800 180
PaperPresentation 800 180

Output:
TechQuiz 100 30
TreasureHunt 100 90
HexCoding 800 180
PaperPresentation 800 180
ProjectExpo 800 180
Hackathon 800 240

import java.util.*;
class Event implements Comparable<Event>{
    private String title;private int registrationFee,duration;
    public Event(String title,int registrationFee,int duration){
        this.title=title;this.registrationFee=registrationFee;this.duration=duration;
    }
    @Override
    public String toString(){
        return this.title+ " " +this.registrationFee +" "+this.duration;
    }
    @Override
    public int compareTo(Event de){
        if(this.registrationFee==de.registrationFee){
            if(this.duration==de.duration){
                    return this.title.compareTo(de.title);
                }
            return this.duration-de.duration;
        }
        return this.registrationFee - de.registrationFee;
    }
}
public class Hello {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.nextLine().trim());
        List<Event> events = new ArrayList<>();
        for (int ctr = 1; ctr <= N; ctr++) {
            String currEvent[] = sc.nextLine().trim().split("\s+");
            String title = currEvent[0];
            int registrationFee = Integer.parseInt(currEvent[1]);
            int duration = Integer.parseInt(currEvent[2]);
            events.add(new Event(title, registrationFee, duration));
        }
        Collections.sort(events);
        for (Event event : events) {
            System.out.println(event);
        }
    }
}

Previous Article

Function calculateDistance

Next Article

Compare Two integers with Logical NOT and AND Operators (Boolean )

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *