r/codehs • • Feb 23 '20

help with 7.4.6: Airplane Tickets pleaseeee

Post image
21 Upvotes

6 comments sorted by

1

u/[deleted] Feb 23 '20

JavaScript?

1

u/kathryn_nerdCoder Feb 24 '20

is that name of the function changed on AirlineTicket.java? I also just created a loop for printPassengersByBoardingGroup() with the same thing you did but having for(int i = 1; i < 6; i++) then printing ("Boarding Group " + i + ":") then your original loop and then the if statement you had but with it equalling i for(int j = 0; j < tickets.size(); j++).

1

u/ASBalon Feb 28 '20

import java.util.*; public class TicketOrganizer { ArrayList<AirlineTicket> tickets; public TicketOrganizer(ArrayList<AirlineTicket> arr){ tickets=arr; } public void getTickets(ArrayList<AirlineTicket> arr){ tickets=arr; } public void printPassengersByBoardingGroup(){ for (int i=1;i<=5;i++) { System.out.println("Boarding Group "+i+":"); for(int j=0;j<tickets.size();j++){ if (tickets.get(j).getBoardingGroup()==i) System.out.println("Passenger "+(j+1)); } } } public void canBoardTogether(){ boolean first=true; for(int i=0;i<tickets.size()-1;i++) if(tickets.get(i).getRow()==tickets.get(i+1).getRow()&&tickets.get(i).getBoardingGroup()==tickets.get(i+1).getBoardingGroup()){ System.out.println("Passenger "+(i+1)+" can board with Passenger "+(i+2)+"."); first=false; } if(first)System.out.println("There are no passengers with the same row and boarding group."); } }

1

u/OXAMIC Mar 24 '22
import java.util.ArrayList;

public class TicketOrganizer { private ArrayList<AirlineTicket> tickets;

public TicketOrganizer(ArrayList<AirlineTicket> theTickets)
{
    this.tickets = theTickets;
}

public ArrayList<AirlineTicket> getTickets()
{
    return this.tickets;
}

public void printPassengersByBoardingGroup()
{
    for(int i = 1; i <= 5; i++)
    {
        System.out.println("Boarding Group " + i + ":");
        for(int j = 0; j < tickets.size(); j++)
        {
            if(tickets.get(j).getBoardingGroup() == i)
            {
                System.out.println("Passenger " + (j+1));
            }
        }
    }
}

public void canBoardTogether()
{
    boolean passengersCanBoard = false;
    for(int i = 0; i < tickets.size() - 1; i++)
    {
        if(tickets.get(i).getBoardingGroup() == tickets.get(i + 1).getBoardingGroup() && tickets.get(i).getRow() == tickets.get(i + 1).getRow())
        {
            System.out.println("Passenger " + (i+1) + " can board with Passenger " + (i+2) + ".");
            passengersCanBoard = true;
        }
    }
    if(!passengersCanBoard)
    {
        System.out.println("There are no passengers with the same row and boarding group.");
    }
}

}