Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!