Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda