Car Java Question

View: New views
2 Messages — Rating Filter:   Alert me  

Car Java Question

by CGagnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am trying to make a program that will first create a car which is an array and then load passengers into the car.  Now for the part I am having trouble with.  Before I can put the passenger (which is passed in from a test class) into the car I must first check to see if the person is already in the car, if they are, I don't put them in, if they aren't already in the car I need to find and empty seat and put them in it.  This is what I have so far for the loading.

public class Car
{
    private Person[] Passengers;
    private int maxcap;
    private int occupancy;
    private Person Driver;
   
    public Car(){
        /**
        (constructor) It creates a small car seating 4 people.
        */
       Passengers = new Person[4];
       maxcap = 4;
       occupancy = 0;
       Driver = null;
    }
   
    public Car(int aCapacity){
        /**
        (constructor) It creates a car whose capacity is set by
        the parameter given. If the parameter is not from 4 to 8
        then the closest value should be used; for example, if an
        occupancy value of 2 is given then the car’s occupancy will
        be set to 4.
        */
        if(aCapacity <= 4){
            maxcap = 4;
        }
        else if(aCapacity >= 8){
            maxcap = 8;
        }
        Passengers = new Person[maxcap];
        occupancy = 0;
        Driver = null;
    }
   
    public int getCapacity(){
        /**
        (method) It returns the maximum capacity of the car.
        */
        return maxcap;
    }
   
    public int getOccupancy(){
        /**
        (method) It returns the current number of passengers.
        */
        return occupancy;
    }
   
    public boolean addPassenger(Person person){
        /**
        (method) It adds a person as a passenger. The method fails if the
        person to add is “null”, if the person is a passenger already, or
        if there is no room left in the car to add it.
        */
        if(person==null){
            return false;
        }
        else if (occupancy < maxcap){
            for(int i=0;i<maxcap;i++){
                for(int j=0;j<maxcap;j++){
                    if(Passengers[j]==person){
                        return false;
                    }
                }
                if(Passengers[i]==null){
                    Passengers[i] = person;
                    return true;
                }
            }
        }
        return false;
    }

I must then test removing the passengers. like so:

    public boolean removePassenger(Person person){
        /**
        (method) It receives a person and removes it as a passenger. If the
        person is also the driver, then the driver seat becomes available
        (that is, it is set to “null”). The method fails if the person is
        “null”, or if he/she is not a passenger.
        */
        }

Any and all help is greatly appreciated!   If there is any important information I left out that you need to answer the question let me know.
Thanks,
CGagnon

Re: Car Java Question

by dhawken :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I must then test removing the passengers. like so:

    public boolean removePassenger(Person person){
        /**
        (method) It receives a person and removes it as a passenger. If the
        person is also the driver, then the driver seat becomes available
        (that is, it is set to “null”). The method fails if the person is
        “null”, or if he/she is not a passenger.
        */
        }

Any and all help is greatly appreciated!   If there is any important information I left out that you need to answer the question let me know.
Thanks,
CGagnon
Hey, why an Array for the "holder" of passengers?  Do you need to keep track of which seat a person is a passenger is in? (array loc could indicate seat).  Even still, I would probably recommend a parameterized List or Collection.  The insertion and removal criteria can be handled automatically by overriding equals() on Person, and the using contains(), insert(), remove() etc and the like on whatever List/Collection you choose.