RideShare

This is not a complete final version of the RideShare files, but it should be enough to give you some ideas on how to accomplish certain tasks. Do not copy-paste this code, nor copy-type this code into your own documents! Use it as a reference for ideas on how to accomplish certain tasks, and then write your own code without making direct reference to these files.

If you find that you need more assistance, feel free to come see me at school to examine the code further, or to have me look at your code if you think there's something I can help with.

/** * The Passenger class describes a RideShare passenger in * terms of its starting and ending stations. * * @author Richard White * @version 2019-02-08 */ public class Passenger { private int startLoc; private int dest; /** * Constructs a Passenger object with a specified * starting and ending location. * @param start the initial station number * @param end the final station number (destination) */ public Passenger(int start, int end) { startLoc = start; dest = end; } /** * This method identifies the destination of this * passenger * @return the station number we're trying to reach */ public int getDest() { return dest; } /** * This method identifies the initial location of this * passenger * @return the initial location */ public int getStartLoc() { return startLoc; } /** * Overrides the .toString() method */ public String toString() { return super.toString() + "[startLoc=" + startLoc + ",dest=" + dest + "]"; } }
/** * The Station class manages the passengers and cars that * are currently located at this station. * * @author Richard White * @version 2019-02-13 */ import java.util.ArrayList; public class Station { private int stationNumber; private ArrayList<Passenger> passengers; private ArrayList<Car> cars; /** * Constructs a station which is identified by its * number. Each station keeps track of the cars that * are currently there and the passengers waiting to * get a ride. * @param stationNumber the number of the station */ public Station(int stationNumber) { this.stationNumber = stationNumber; passengers = new ArrayList<Passenger>(); cars = new ArrayList<Car>(); } /** * Returns the number of the station * @return the stationNumber, an integer */ public int getStationNumber() { return stationNumber; } /** * Adds a passenger to the list waiting at this station * @param newPass the Passenger object waiting here */ public void addPassenger(Passenger newPass) { passengers.add(newPass); } /** * Adds a car to the list of cars dropping off and * picking up at this station * @param newCar the Car object currently located here */ public void addCar(Car newCar) { cars.add(newCar); } /** * A list of all the passengers waiting for a ride at * this station. * @return an ArrayList of Passenger objects */ public ArrayList<Passenger> getPassengers() { return passengers; } /** * A list of all the cars picking up and dropping off * at this station. * @return an ArrayList of Car objects */ public ArrayList<Car> getCars() { return cars; } /** * Takes a passenger from the station (to be loaded * into a Car) * @return the Passenger removed, or null if the passenger * doesn't exist */ public Passenger removePassenger(Passenger pass) { if (passengers.remove(pass)) return pass; return null; } /** * Takes a Car from the station (to be moved and added * to a new Station) * @return the Car removed, or null if the car * doesn't exist */ public Car removeCar(Car car) { if (cars.remove(car)) return car; return null; } /** * Overrides the toString method so we can easily print * out the station, its passengers waiting, and the cars * here. */ public String toString() { return "Station[stationNumber=" + stationNumber + ",passengers=" + passengers + ",cars=" + cars + "]"; } }
/** * The Car class describes a vehicle used in the RideShare simulation * * @author Richard White * @version 2019-02-08 */ import java.util.ArrayList; public class Car { private int loc; private int dest; private ArrayList<Passenger> passList; public final int MAX_PASS = 3; /** * Each car begins with an initial location, a destination * and an empty passenger list. * @param loc the car's current (starting) location * @param dest the car's final destination */ public Car(int loc, int dest) { // initialise instance variables passList = new ArrayList<Passenger>(); this.loc = loc; this.dest = dest; } /** * Moves the car in the correct direction, as long as it * hasn't reached its destination yet. * */public void move() { if (dest > loc) { loc++; } else if (dest < loc) { loc--; } else { // we must be at our location and we're not going // to go anywhere } } /** * Drops off any passengers that have arrived at their destination * (ie. removes them from the list), or returns a list of passengers * that haven't been deposited yet so that the simulation can add * them to the appropriate station. */ public ArrayList<Passenger> dropoff() { for (int i = 0; i < passList.size(); i++) { if (passList.get(i).getDest() == loc) { passList.remove(i); } } // What if we've arrived at our destination but // there are passengers that we haven't deposited // yet? if (dest == loc) { return passList; } return null; } /** * Used to add a new passenger to our passlist * @param newPassenger a new Passenger object * (PRECONDITION: There must be room for the passenger. May be * checked by using the boolean .hasRoom() method.) */ public void pickup(Passenger newPassenger) { passList.add(newPassenger); } /** * Used to check if the car has room to take on an * additional passenger. * @return true if there's room for another passenger */ public boolean hasRoom() { return passList.size() + 1 < MAX_PASS; } /** * Returns the current location of the car. * @return the car's station location, an integer */ public int getLoc() { return loc; } /** * Returns the destination of the car. * @return the car's destination station number, an integer */ public int getDest() { return dest; } /** * Sets the location of the car. Not needed, maybe, * but it may come in handy. * @param stationNumber the station at which to place the car */ public void setLoc(int stationNumber) { loc = stationNumber; } /** * Returns an ArrayList of current passengers * @return the ArrayList of current passengers */ public ArrayList<Passenger> getPassList() { return passList; } /** * Overrides the toString method from the object * @return information about the car */ public String toString() { return super.toString() + "[loc=" + this.loc + ",dest=" + this.dest + ",passList=" + this.passList + "]"; } }
/** * This Simulation class manipulates the Passenger, Car, and Station classes * to demonstrate a proof-of-concept for the RideShare system. This version * of the Simulation only includes a limited number of cars, passengers, and * stations for testing purposes. * * @author Richard White * @version 2019-02-11 */ public class Simulation { public static final int NUM_OF_STATIONS = 3; public static final int NUM_OF_CARS = 3; public static final int NUM_OF_PASSENGERS = 4; public static final int NUM_OF_MOVES = NUM_OF_STATIONS + 1; // Needed to make sure passengers get off last time /** * This static method is used by the main() method to display the status * of each station in the RideShare system. The toString() method for * the Station class goes on to display the status of the Car and Passenger * objects in the system. * @param stations the list of stations being tracked by the system */ public static void show(Station[] stations) { System.out.println("\n######### RideShare Status Report #########\n"); for (Station station : stations) { System.out.println(station); } System.out.println(); } public static void main(String[] args) { System.out.println("RideShare development test"); System.out.println("Initializing stations..."); Station[] stations = new Station[NUM_OF_STATIONS]; for (int i = 0; i < NUM_OF_STATIONS; i++) { stations[i] = new Station(i); } System.out.println("Initializing cars..."); Car[] cars = new Car[NUM_OF_CARS]; cars[0] = new Car(0,2); cars[1] = new Car(2,0); cars[2] = new Car(2,0); System.out.println("Initializing passengers..."); Passenger[] passengers = new Passenger[NUM_OF_PASSENGERS]; passengers[0] = new Passenger(0,1); passengers[1] = new Passenger(0,2); passengers[2] = new Passenger(0,2); passengers[3] = new Passenger(0,2); System.out.println("Placing passengers at stations..."); for (Passenger passenger : passengers) { stations[passenger.getStartLoc()].addPassenger(passenger); } System.out.println("Placing cars at stations..."); for (Car car : cars) { stations[car.getLoc()].addCar(car); } show(stations); System.out.println("Starting main loop..."); for (int move = 1; move <= NUM_OF_MOVES; move++) { System.out.println("Move #" + move); System.out.println("Dropping off passengers at all stations..."); for (Station station : stations) { for (Car car : station.getCars()) { car.dropoff(); } } System.out.println("Picking up passengers at all stations..."); for (Station station : stations) { for (Car car : station.getCars()) { for (int i = station.getPassengers().size() - 1; i >= 0; i--) { // Work our way backwards through passengers at this station // Why is this loop indexed, as opposed to the for-each loops // used for Station and Car? As I'm picking up passengers // I'm changing the size of the passenger list for each station, // and this loop needs to be able to dynamically respond to // those changes. The for-each loop doesn't allow for that. // If passenger is heading in same direction as car and car has room, // add them to the car, and remove them from the station. if (((car.getDest() > car.getLoc() && station.getPassengers().get(i).getDest() > car.getLoc()) || (car.getDest() < car.getLoc() && station.getPassengers().get(i).getDest() < car.getLoc())) && car.hasRoom()) { car.pickup(station.getPassengers().get(i)); station.removePassenger(station.getPassengers().get(i)); } } } } System.out.println("Having cars move..."); for (Car car : cars) { if (car.getLoc() != car.getDest()) { stations[car.getLoc()].removeCar(car); car.move(); stations[car.getLoc()].addCar(car); } } show(stations); } } }