Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
How to store only multiple passengers inside the cabin class?

I have three classes in my program. Ship.java, Cabin.java and Passenger.java. According to the program a single cabin should hold up to 3 passengers only. But I'm stuck on how to do this. I have created an array of cabin objects in my Ship.java class. I can only add one passenger into a cabin with below mentioned addCustomer method

Cabin[] cruiseShip = new Cabin[12];
for (int i = 0; i < cruiseShip.length; i++) {
    cruiseShip[i] = new Cabin();
}

public static void addCustomer(Cabin[] cruiseShip, String firstName, String surName, int expenses, int cabinNumber){
    if (cruiseShip[cabinNumber].getCabinName().equals("empty")){
        cruiseShip[cabinNumber].setFirstName(firstName);
        cruiseShip[cabinNumber].setSurName(surName);
        cruiseShip[cabinNumber].setExpenses(expenses);
        cruiseShip[cabinNumber].setCabinName("not empty");

        System.out.println("Cabin number " + cruiseShip[cabinNumber].getCabinNumber() + " is occupied by " + cruiseShip[cabinNumber].getFirstName() + " " + cruiseShip[cabinNumber].getSurName() );
    }
}

This is how Cabin.java looks :

public class Cabin extends Passenger {
    int cabinNumber;
    String cabinName;

    public String getCabinName() {
        return cabinName;
    }

    public void setCabinName(String cabinName) {
        this.cabinName = cabinName;
    }

    public int getCabinNumber() {
        return cabinNumber;
    }

    public void setCabinNumber(int cabinNumber) {
        this.cabinNumber = cabinNumber;
    }
}

This is how Passenger.java looks :

public class Passenger {
    String firstName;
    String surName;
    int expenses;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getSurName() {
        return surName;
    }

    public void setSurName(String surName) {
        this.surName = surName;
    }

    public int getExpenses() {
        return expenses;
    }

    public void setExpenses(int expenses) {
        this.expenses = expenses;
    }
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Cabin should contain a data-structure which holds passengers.(association 1-n, from 1_cabin-N_passengers) You could also restrict the no. of passengers regarding to cabin type (up to 2-3-n passengers) and also check not to add n-times the same passenger in the same cabin for a specific time. Same logic with Ship which have Cabins.

class Cabin
{
  ... etc ... as u did 
  List<Passenger> listP = new ArrayList<Passenger>();
}
listP.add(new Passenger(...));

class Ship
{
   ...
   List<Cabin> listC = new ArrayList<Cabin>();
}
listC.add(new Cabin(...));
//get a specific cabin from the ship and add a new Passenger
//note maybe it's better to do your custom methods for add,get_Ship, Cabin (based on the requiremts). 
//Standard List Methods usually do not fit exactly custom requirements, so need to be enhanced 
ship.getlistC().get(i_specificCabin).listP.add(new Passenger(...)); 

Be carefully not to mix semantics, think how in real world things works (see @Jim Garrison).

Note: Maybe a Map<String/Integer,CustomObject> can fit well for ease of access based on key(id).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda