Home > Software design >  How to store only multiple passengers inside the cabin class?
How to store only multiple passengers inside the cabin class?

Time:03-16

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;
    }
}

CodePudding user response:

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).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).

CodePudding user response:

Your relationship become as per your code is 1 Cabin have multiple Passager so relationship is OneToMany. The best and easiest way to solve your problem is Composition in java. You are working with Inheritance, It has IS-A relationship but Compostion has HAS-A relationship. Composition is best to worked on relationship.

Here down is code that solved your problem using `Composition Technique:

Passenger.java

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

    // No argument constructor
    public Passenger() {
        
    }
    
    // All argument constructor
    public Passenger(String firstName, String surName, int expenses) {
        this.firstName = firstName;
        this.surName = surName;
        this.expenses = 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;
    }
}

Cabin.java

public class Cabin {

    int cabinNumber;
    String cabinName;
    
    List<Passenger> passenger = new ArrayList<>();
    
    // No argument constructor
    public Cabin() {
        
    }
    
    // All argument constructor
    public Cabin(int cabinNumber, String cabinName, List<Passenger> passenger) {
        this.cabinNumber = cabinNumber;
        this.cabinName = cabinName;
        this.passenger = passenger;
    }
    
    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;
    }
    
    public List<Passenger> getPassenger() {
        return passenger;
    }
    
    public void setPassenger(List<Passenger> passenger) {
        this.passenger = passenger;
    }
}

Here down is Main class which insert record in Passanger and Cabin with relationship.

public static void main (String[] args) {
        Cabin cabin = new Cabin();
        
        // Insert and Put all Passanger in ArrayList
        List<Passenger> passenger = new ArrayList<>();
        passenger.add(new Passenger("Jack", "Crawly", 1000));
        passenger.add(new Passenger("Michel", "Jordan", 2000));
        passenger.add(new Passenger("Tim", "Leach", 3000));
        
        if(cabin.getCabinName() == null)
        {
            // Insert Cabin with all Passenger
            cabin = new Cabin(1, "Cabin1", passenger);   
        }
        
        // Get all Passangers with Cabin
        List<Passenger> passengers = cabin.getPassenger();   
        for (Passenger psg : passengers) {
            System.out.println("Cabin Number : "   cabin.getCabinNumber());
            System.out.println("FirstName : "   psg.getFirstName());
            System.out.println("LastName : "   psg.getSurName());
            System.out.println();
        }   
}
  • Related