In: Computer Science
In Java
class PassengerComparator implements Comparator for a Passenger
Implement a comparator where a FirstClassPassenger precedes a CoachPassenger,. If both Passengers are of the same TicketClass then the Passenger with the lower TicketNumber precedes the Passenger with the higher TicketNumber.
Hello,
I have developed PassengerComparator class based on your requirement. Attached PassengerComparator class. And also I have created Passenger and PassengerComparatorTester class to test PassengerComparator class. Passenger and PassengerComparatorTester classes are purely based on my assumption as there is no specification provided in your requirement. Attaching Passenger and PassengerComparatorTester classes as well. Please let me know if any changes required.
PassengerComparator.java:
import java.util.Comparator;
// PassengerComparator class that implements Comparator interface with type Passenger
public class PassengerComparator implements Comparator<Passenger>{
// compare method implementation
@Override
public int compare(Passenger p1, Passenger p2) {
// Verify class type of two passengers.
// If any person has class type First and other passenger has class type Coach then return -1 that denotes passenger with class type First come before other passenger
if(p1.getClassType().equals("First") && p2.getClassType().equals("Coach")) {
return -1;
} else if(p1.getClassType().equals("Coach") && p2.getClassType().equals("First")) {
// If any person has class type Coach and other passenger has class type First then return 1 that denotes passenger with class type Coach come after other passenger
return 1;
} else if(p1.getClassType().equals(p2.getClassType())) {
// If both passengers class type is same
// Then return -1 if p1 ticket number is lower than p2 ticket number else return 1
return p1.getTicketNumber() < p2.getTicketNumber() ? -1 : 1;
}
return 0;
}
}
Output:
Passenger.java:
public class Passenger {
// Class fields
private String fullName;
private String classType;
private int ticketNumber;
// Parameterized constructor with all fields
public Passenger(String fullName, String classType, int ticketNumber) {
this.fullName = fullName;
this.classType = classType;
this.ticketNumber = ticketNumber;
}
// Getters and Setters for all fields
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getClassType() {
return classType;
}
public void setClassType(String classType) {
this.classType = classType;
}
public int getTicketNumber() {
return ticketNumber;
}
public void setTicketNumber(int ticketNumber) {
this.ticketNumber = ticketNumber;
}
// toString method
@Override
public String toString() {
return "Passenger [fullName=" + fullName + ", classType=" + classType + ", ticketNumber=" + ticketNumber + "]";
}
}
PassengerComparatorTester.java:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
// PassengerComparatorTester class to test PassengerComparator class
public class PassengerComparatorTester {
public static void main(String[] args) {
// Created 6 Passenger objects using parameterized constructor of Passenger class
Passenger passenger1 = new Passenger("John Doe", "Coach", 12345878);
Passenger passenger2 = new Passenger("Michael Misha", "First", 12457893);
Passenger passenger3 = new Passenger("Rich Cohen", "First", 1245);
Passenger passenger4 = new Passenger("Dennis Lee", "Coach", 1245879);
Passenger passenger5 = new Passenger("Robert Key", "Coach", 124);
Passenger passenger6 = new Passenger("Lewis Jerry", "First", 145785);
// Created new ArrayList of type Passenger
List<Passenger> passengersList = new ArrayList<Passenger>();
// Add all 6 passenger objects to the list
passengersList.add(passenger1);
passengersList.add(passenger2);
passengersList.add(passenger3);
passengersList.add(passenger4);
passengersList.add(passenger5);
passengersList.add(passenger6);
System.out.println("**************************************************************************");
System.out.println("Before Sorting");
System.out.println("**************************************************************************");
// Before sorting, printing all passengers by using for each loop
for (Passenger passenger : passengersList) {
System.out.println(passenger);
}
// Call sort method in Collections interface with PassengerComparator
Collections.sort(passengersList, new PassengerComparator());
System.out.println("**************************************************************************");
System.out.println("After Sorting");
System.out.println("**************************************************************************");
// After sorting, printing all passengers by using for each loop
for (Passenger passenger : passengersList) {
System.out.println(passenger);
}
}
}