I am trying to separate Passenger objects from an ArrayList into eight smaller lists. I am traversing the list of Passengers using iterator.
I have tried to use the iterator without explicitly casting to the Passenger class but it does not seem to work. It prompts to cast the iterator to Passenger class, still.
Here is my Passenger class:
package application;
public class Passenger implements Comparable<Passenger>{
private int key;
private int row;
private String name;
private String type;
public Passenger() {
}
public Passenger(int key, int row, String name, String type) {
this.key = key;
this.row = row;
this.name = name;
this.type = type;
}
//The custom copy constructor
public Passenger(Passenger passenger) {
this.key = passenger.key;//This field should be null at time of instantiation
this.row = passenger.row;
this.name = passenger.name;
this.type = passenger.type;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public int compareTo(Passenger o) {
if(this.key > o.key) {
return 1;
}
else if(this.key < o.key){
return -1;
}
else {
return 0;
}
}
//copy method
public Passenger copy() {
return new Passenger(this);
}
@Override
public String toString(){
String passengerString = String.format("%-15s%-5s%-5d%-5d", name, type, row, key);
return passengerString;
}
}
Here is the method where the exception is thrown:
public void loadOldPQ(ArrayList<Passenger> list) throws IOException {
int priorityNumber = list.size();
ArrayList<Passenger> firstGroup = new ArrayList<Passenger>();
ArrayList<Passenger> secondGroup = new ArrayList<Passenger>();
ArrayList<Passenger> thirdGroup = new ArrayList<Passenger>();
ArrayList<Passenger> fourthGroup = new ArrayList<Passenger>();
ArrayList<Passenger> fifthGroup = new ArrayList<Passenger>();
ArrayList<Passenger> sixthGroup = new ArrayList<Passenger>();
ArrayList<Passenger> seventhGroup = new ArrayList<Passenger>();
ArrayList<Passenger> eighthGroup = new ArrayList<Passenger>();
Iterator<Passenger> iter = list.iterator();
while(iter.hasNext()) {
if(((Passenger) iter).getType().equals("H")) {
firstGroup.add(iter.next());
}
else if(((Passenger) iter).getRow() >= 1 && ((Passenger) iter).getRow() <= 4
&& ((Passenger) iter).getType().equals("E")){
secondGroup.add(iter.next());
}else if(((Passenger) iter).getRow() >= 1 && ((Passenger) iter).getRow() <= 4) {
thirdGroup.add(iter.next());
}else if(((Passenger) iter).getType().equalsIgnoreCase("E") || ((Passenger) iter).getRow() == 10
|| ((Passenger) iter).getRow() == 11) {
fourthGroup.add(iter.next());
}else if(((Passenger) iter).getRow() >= 23 && ((Passenger) iter).getRow() <= 26) {
fifthGroup.add(iter.next());
}else if(((Passenger) iter).getRow() >= 17 && ((Passenger) iter).getRow() <= 22) {
sixthGroup.add(iter.next());
}else if(((Passenger) iter).getRow() >= 11 && ((Passenger) iter).getRow() <= 16) {
seventhGroup.add(iter.next());
}else if(((Passenger) iter).getRow() >= 5 && ((Passenger) iter).getRow() <= 10) {
eighthGroup.add(iter.next());
}else {
System.out.println("Some error!");
}
}
for(Passenger passenger: firstGroup) {
passenger.setKey(priorityNumber);
System.out.println(passenger);
priorityNumber--;
}
for(Passenger passenger: secondGroup) {
passenger.setKey(priorityNumber);
System.out.println(passenger);
priorityNumber--;
}
for(Passenger passenger: thirdGroup) {
passenger.setKey(priorityNumber);
System.out.println(passenger);
priorityNumber--;
}
for(Passenger passenger: fourthGroup) {
passenger.setKey(priorityNumber);
System.out.println(passenger);
priorityNumber--;
}
for(Passenger passenger: fifthGroup) {
passenger.setKey(priorityNumber);
System.out.println(passenger);
priorityNumber--;
}
for(Passenger passenger: sixthGroup) {
passenger.setKey(priorityNumber);
System.out.println(passenger);
priorityNumber--;
}
for(Passenger passenger: seventhGroup) {
passenger.setKey(priorityNumber);
System.out.println(passenger);
priorityNumber--;
}
for(Passenger passenger: eighthGroup) {
passenger.setKey(priorityNumber);
System.out.println(passenger);
priorityNumber--;
}
}
The error occurs at the first if statement:
if(((Passenger) iter).getType().equals("H"))
Here is the stack trace:
Exception in thread "main" java.lang.ClassCastException: class java.util.ArrayList$Itr cannot be cast to class application.Passenger (java.util.ArrayList$Itr is in module java.base of loader 'bootstrap'; application.Passenger is in unnamed module of loader 'app')
at application.Airworthy.loadOldPQ(Airworthy.java:172)
at application.Airworthy.readInputFile(Airworthy.java:151)
at application.Airworthy.<init>(Airworthy.java:44)
at application.Simulation.main(Simulation.java:20)
I did not test this piece of code, but I think this is what you need. I just refactoring your code without diving into the details.
private int findGroupNumber(Passenger passenger) {
if (passenger.getType().equals("H"))
return 1;
if (passenger.getRow() >= 1 && passenger.getRow() <= 4 && passenger.getType().equalsIgnoreCase("E"))
return 2;
if (passenger.getRow() >= 1 && passenger.getRow() <= 4)
return 3;
if (passenger.getType().equalsIgnoreCase("E") || passenger.getRow() == 10 || passenger.getRow() == 11)
return 4;
if (passenger.getRow() >= 23 && passenger.getRow() <= 26)
return 5;
if (passenger.getRow() >= 17 && passenger.getRow() <= 22)
return 6;
if (passenger.getRow() >= 11 && passenger.getRow() <= 16)
return 7;
if (passenger.getRow() >= 5 && passenger.getRow() <= 10)
return 8;
else
throw new RuntimeException("Some error.");
}
public void loadOldPQ(List<Passenger> list) throws IOException {
List<List<Passenger>> groups = new ArrayList<>(8);
for (int i = 0; i < 8; i++) {
groups.add(new ArrayList<>());
}
for (var passenger : list) {
groups.get(findGroupNumber(passenger) - 1).add(passenger);
}
int priorityNumber = list.size();
for (var group : groups) {
for (var passenger : group) {
passenger.setKey(priorityNumber--);
System.out.println(passenger);
}
}
}