attempting to create a main object with sub object from another class

Viewed 25

I have been trying to create a main object which is the cabin class that holds sub object that is passengers class. the main object will hold 3 sub object. the purpose is to fit 3 passengers in one room. been trying but were unable to figure out how can I add 3 objects to 1 main object without using an array list. I have removed all the content of cabin as nothing seems to work as I were not able to figure the structure. can someone please help. thanks for the help in advance.

package com.mo;
import java.util.*;
public class Main {



    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        passengers customer1= new passengers();
        passengers customer2 = new passengers();
        passengers customer3 = new passengers();
        System.out.println("Enter first Name of first customer?");
        customer1.setFName(in.nextLine());
        System.out.println("Enter sur Name of first customer?");

        customer1.setSName(in.nextLine());
        System.out.println("Enter price for the cabin of first customer?");

        customer1.setExpense(in.nextDouble());
        System.out.println("Enter first Name of second customer ?");

        customer2.setFName(in.nextLine());
        System.out.println("Enter sur Name of second customer?");

        customer2.setSName(in.nextLine());
        System.out.println("Enter price for the cabin of second customer?");

        customer2.setExpense(in.nextDouble());
        System.out.println("Enter first Name of third customer?");

        customer3.setFName(in.nextLine());
        System.out.println("Enter sur Name of third customer?");

        customer3.setSName(in.nextLine());
        System.out.println("Enter price for the cabin of third customer?");

        customer3.setExpense(in.nextDouble());

    }
}

    package com.mo;
    
    public class passengers {
        private String FName;
        private String SName;
        private double Expense;
        private static int count;
        public passengers(){
            FName=null;
            SName=null;
            Expense=0;
            count=0;
        }
        public void setFName(String FName){
           this.FName=FName;
        }
        public void setSName(String SName){
            this.SName=SName;
    
        }
        public void setExpense(double Expense){
            this.Expense=Expense;
    
        }
        public String getFName(){
            return FName;
        }
        public String getSName(){
            return SName;
        }
        public double getExpense(){
            return Expense;
        }
    }



    package com.mo;
    
    public class cabin {
        String Room;
    
    }
0 Answers
Related