Answer:
See explaination
Step-by-step explanation:
import java.util.ArrayList;
public class Main
{
public static void main(String[] args)
{
ArrayList<AirlineTicket> tickets = new ArrayList<AirlineTicket>();
//This creates a randomized list of passengers
addPassengers(tickets);
for(AirlineTicket elem: tickets)
{
System.out.println(elem);
}
//This creates a TicketOrganizer object
TicketOrganizer ticketOrganizer = new TicketOrganizer(tickets);
//These are the methods of the ticketOrganizer in action
System.out.println("\\Passengers Ordered by Boarding Group:");
ticketOrganizer.printPassengersByBoardingGroup();
System.out.println("\\Passengers in line who can board together:");
ticketOrganizer.canBoardTogether();
}
//Do not touch this method! It is adding random passengers to the AirlineTicket array
public static void addPassengers(ArrayList<AirlineTicket> tickets)
{
String[] seats = {"A","B","C","D","E","F"};
for(int index = 0; index< 15; index++)
{
int random = (int)(Math.random() * 5);
AirlineTicket ticket = new AirlineTicket("Passenger " + (index+1), seats[random], ((int)(Math.random()*5)+1), ((int)(Math.random()*8)+1));
tickets.add(ticket);
}
}
}
class TicketOrganizer
{
private ArrayList<AirlineTicket> tickets ;
//constructor with parameter as an arraylist of AirlineTicket
public TicketOrganizer(ArrayList<AirlineTicket> tickets)
{
this.tickets = tickets;
}
//methhods to return the arraylist of airlines
public ArrayList<AirlineTicket> getTickets()
{ //re-organize the ticket first
ArrayList<AirlineTicket> tickets_organized = new ArrayList<AirlineTicket>();
for(int i=1; i<=5; i++)
{
for(AirlineTicket ticket: tickets)
{
if(ticket.getBoardingGroup() == i)
{
tickets_organized.add(ticket);
}
}
}
return tickets_organized;
}
//print the tickets based on boardingGroup
public void printPassengersByBoardingGroup()
{
int count = 0;
for(int i=1; i<=5; i++)
{
System.out.println("Boarding Group " + i + ":");
for(AirlineTicket ticket : tickets)
{
if(ticket.getBoardingGroup() == i)
{
System.out.println("Passenger " + ticket.getName());
}
}
}
}
//check if two persons can sit together
public void canBoardTogether()
{
for(int i=0; i<15; i++)
{
String str = "Passenger "+ (i + 1) +" can board with Passengers";
for(int j=i+1; j<14; j++)
{
boolean isRowEqual = tickets.get(i).getRow() == tickets.get(j).getRow();
boolean isBoardGroupEqual = tickets.get(i).getBoardingGroup() == tickets.get(j).getBoardingGroup();
//if one of them false, print the all added Passengers
if(!(isRowEqual && isBoardGroupEqual))
{
if(j != i+1)
{
System.out.println(str);
}
break;
}
str += " " + (j+1);
}
}
}
}
class AirlineTicket
{
private String[] seats = {"A","B","C","D","E","F"};
private String name;
private String seat;
private int boardingGroup;
private int row;
public AirlineTicket(String name, String seat, int boardingGroup, int row)
{
this.name = name;
if(isValidSeat(seat))
{
this.seat = seat;
}
this.boardingGroup = boardingGroup;
this.row = row;
}
private boolean isValidSeat(String seat)
{
boolean isValidSeat = false;
for(String elem: seats)
{
if(seat.equals(elem))
{
isValidSeat = true;
}
}
return isValidSeat;
}
public String getSeat()
{
return this.seat;
}
public String getName()
{
return this.name;
}
public int getBoardingGroup()
{
return this.boardingGroup;
}
public int getRow()
{
return this.row;
}
public String toString()
{
return name + " Seat: " +seat + " Row: " + row + " Boarding Group: " + boardingGroup;
}
}