first commit
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=void\ act()
|
||||
comment0.text=\n\ Implement\ the\ actor's\ behavior.\n
|
||||
numComments=1
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* An actor in the taxi-company simulation.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public interface Actor
|
||||
{
|
||||
/**
|
||||
* Implement the actor's behavior.
|
||||
*/
|
||||
public void act();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#BlueJ class context
|
||||
comment0.params=width\ height
|
||||
comment0.target=City(int,\ int)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ City\n\ @param\ width\ The\ city's\ width.\n\ @param\ height\ The\ city's\ height.\n
|
||||
comment1.params=
|
||||
comment1.target=City()
|
||||
comment1.text=\n\ Create\ a\ city\ of\ default\ size.\n
|
||||
comment2.params=
|
||||
comment2.target=java.util.Iterator\ getItems()
|
||||
comment2.text=\n\ @return\ An\ iterator\ over\ the\ items.\n
|
||||
comment3.params=item
|
||||
comment3.target=void\ addItem(Item)
|
||||
comment3.text=\n\ Add\ the\ given\ item\ to\ the\ city's\ collection.\n\ @param\ item\ The\ item\ to\ be\ added.\n
|
||||
comment4.params=item
|
||||
comment4.target=void\ removeItem(Item)
|
||||
comment4.text=\n\ Remove\ the\ given\ item\ from\ the\ city's\ collection.\n\ @param\ item\ The\ item\ to\ be\ removed.\n
|
||||
comment5.params=
|
||||
comment5.target=java.lang.String\ toString()
|
||||
comment5.text=\n\ @return\ A\ string\ representation\ of\ the\ city.\n
|
||||
comment6.params=
|
||||
comment6.target=int\ getWidth()
|
||||
comment6.text=\n\ @return\ The\ width.\n
|
||||
comment7.params=
|
||||
comment7.target=int\ getHeight()
|
||||
comment7.text=\n\ @return\ The\ height.\n
|
||||
numComments=8
|
||||
@@ -0,0 +1,106 @@
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A collection of items in the city.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class City
|
||||
{
|
||||
private List<Item> items;
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
private static final int DEFAULT_WIDTH = 35;
|
||||
private static final int DEFAULT_HEIGHT = 35;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class City
|
||||
* @param width The city's width.
|
||||
* @param height The city's height.
|
||||
*/
|
||||
public City(int width, int height)
|
||||
{
|
||||
if(width < 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"Width must be positive: " +
|
||||
width);
|
||||
}
|
||||
if(height < 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"Height must be positive: " +
|
||||
height);
|
||||
}
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
items = new LinkedList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a city of default size.
|
||||
*/
|
||||
public City()
|
||||
{
|
||||
this(DEFAULT_WIDTH, DEFAULT_HEIGHT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return An iterator over the items.
|
||||
*/
|
||||
public Iterator<Item> getItems()
|
||||
{
|
||||
return items.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given item to the city's collection.
|
||||
* @param item The item to be added.
|
||||
*/
|
||||
public void addItem(Item item)
|
||||
{
|
||||
if(items.contains(item)) {
|
||||
throw new IllegalArgumentException(
|
||||
item + " already recorded in the city.");
|
||||
}
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the given item from the city's collection.
|
||||
* @param item The item to be removed.
|
||||
*/
|
||||
public void removeItem(Item item)
|
||||
{
|
||||
if(!items.remove(item)) {
|
||||
throw new IllegalArgumentException(
|
||||
item + " is not in the city.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A string representation of the city.
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return "City size " + width + " by " + height;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The width.
|
||||
*/
|
||||
public int getWidth()
|
||||
{
|
||||
return width;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The height.
|
||||
*/
|
||||
public int getHeight()
|
||||
{
|
||||
return height;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#BlueJ class context
|
||||
comment0.params=city
|
||||
comment0.target=CityGUI(City)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ CityGUI\n\ @param\ city\ The\ city\ whose\ state\ is\ to\ be\ displayed.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ act()
|
||||
comment1.text=\n\ Display\ the\ current\ state\ of\ the\ city.\n
|
||||
numComments=2
|
||||
@@ -0,0 +1,151 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
|
||||
/**
|
||||
* Provide a view of the vehicles and passengers in the city.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class CityGUI extends JFrame implements Actor
|
||||
{
|
||||
// The dimensions of the GUI.
|
||||
public static final int CITY_VIEW_WIDTH = 600;
|
||||
public static final int CITY_VIEW_HEIGHT = 600;
|
||||
private City city;
|
||||
private CityView cityView;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class CityGUI
|
||||
* @param city The city whose state is to be displayed.
|
||||
*/
|
||||
public CityGUI(City city)
|
||||
{
|
||||
this.city = city;
|
||||
cityView = new CityView(city.getWidth(), city.getHeight());
|
||||
getContentPane().add(cityView);
|
||||
setTitle("Taxiville");
|
||||
setSize(CITY_VIEW_WIDTH, CITY_VIEW_HEIGHT);
|
||||
setVisible(true);
|
||||
cityView.preparePaint();
|
||||
cityView.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the current state of the city.
|
||||
*/
|
||||
public void act()
|
||||
{
|
||||
cityView.preparePaint();
|
||||
Iterator<Item> items = city.getItems();
|
||||
while(items.hasNext()) {
|
||||
Item item = items.next();
|
||||
if(item instanceof DrawableItem){
|
||||
DrawableItem drawable = (DrawableItem) item;
|
||||
Location location = item.getLocation();
|
||||
cityView.drawImage(location.getX(), location.getY(), drawable.getImage());
|
||||
}
|
||||
}
|
||||
cityView.repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a graphical view of a rectangular city. This is
|
||||
* a nested class (a class defined inside a class) which
|
||||
* defines a custom component for the user interface. This
|
||||
* component displays the city.
|
||||
* This is rather advanced GUI stuff - you can ignore this
|
||||
* for your project if you like.
|
||||
*/
|
||||
private class CityView extends JPanel
|
||||
{
|
||||
private final int VIEW_SCALING_FACTOR = 6;
|
||||
|
||||
private int cityWidth, cityHeight;
|
||||
private int xScale, yScale;
|
||||
private Dimension size;
|
||||
private Graphics g;
|
||||
private Image cityImage;
|
||||
|
||||
/**
|
||||
* Create a new CityView component.
|
||||
*/
|
||||
public CityView(int width, int height)
|
||||
{
|
||||
cityWidth = width;
|
||||
cityHeight = height;
|
||||
setBackground(Color.white);
|
||||
size = new Dimension(0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell the GUI manager how big we would like to be.
|
||||
*/
|
||||
public Dimension getPreferredSize()
|
||||
{
|
||||
return new Dimension(cityWidth * VIEW_SCALING_FACTOR,
|
||||
cityHeight * VIEW_SCALING_FACTOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare for a new round of painting. Since the component
|
||||
* may be resized, compute the scaling factor again.
|
||||
*/
|
||||
public void preparePaint()
|
||||
{
|
||||
if(!size.equals(getSize())) { // if the size has changed...
|
||||
size = getSize();
|
||||
cityImage = cityView.createImage(size.width, size.height);
|
||||
g = cityImage.getGraphics();
|
||||
|
||||
xScale = size.width / cityWidth;
|
||||
if(xScale < 1) {
|
||||
xScale = VIEW_SCALING_FACTOR;
|
||||
}
|
||||
yScale = size.height / cityHeight;
|
||||
if(yScale < 1) {
|
||||
yScale = VIEW_SCALING_FACTOR;
|
||||
}
|
||||
}
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(0, 0, size.width, size.height);
|
||||
g.setColor(Color.gray);
|
||||
for(int i = 0, x = 0; x < size.width; i++, x = i * xScale) {
|
||||
g.drawLine(x, 0, x, size.height - 1);
|
||||
}
|
||||
for(int i = 0, y = 0; y < size.height; i++, y = i * yScale) {
|
||||
g.drawLine(0, y, size.width - 1, y);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the image for a particular item.
|
||||
*/
|
||||
public void drawImage(int x, int y, Image image)
|
||||
{
|
||||
g.drawImage(image, x * xScale + 1, y * yScale + 1,
|
||||
xScale - 1, yScale - 1, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* The city view component needs to be redisplayed. Copy the
|
||||
* internal image to screen.
|
||||
*/
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
if(cityImage != null) {
|
||||
Dimension currentSize = getSize();
|
||||
if(size.equals(currentSize)) {
|
||||
g.drawImage(cityImage, 0, 0, null);
|
||||
}
|
||||
else {
|
||||
// Rescale the previous image.
|
||||
g.drawImage(cityImage, 0, 0, currentSize.width, currentSize.height, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=java.awt.Image\ getImage()
|
||||
numComments=1
|
||||
@@ -0,0 +1,13 @@
|
||||
import java.awt.Image;
|
||||
|
||||
/**
|
||||
* An item that is able to return an image of itself.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
|
||||
public interface DrawableItem extends Item
|
||||
{
|
||||
public Image getImage();
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Location\ getLocation()
|
||||
numComments=1
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* An item in the city.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
|
||||
public interface Item
|
||||
{
|
||||
public Location getLocation();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#BlueJ class context
|
||||
comment0.params=x\ y
|
||||
comment0.target=Location(int,\ int)
|
||||
comment0.text=\n\ Model\ a\ location\ in\ the\ city.\n\ @param\ x\ The\ x\ coordinate.\ Must\ be\ positive.\n\ @param\ y\ The\ y\ coordinate.\ Must\ be\ positive.\n\ @throws\ IllegalArgumentException\ If\ a\ coordinate\ is\ negative.\n
|
||||
comment1.params=destination
|
||||
comment1.target=Location\ nextLocation(Location)
|
||||
comment1.text=\n\ Generate\ the\ next\ location\ to\ visit\ in\ order\ to\n\ reach\ the\ destination.\n\ @param\ destination\ Where\ we\ want\ to\ get\ to.\n\ @return\ A\ location\ in\ a\ direct\ line\ from\ this\ to\n\ \ \ \ \ \ \ \ \ destination.\n
|
||||
comment2.params=destination
|
||||
comment2.target=int\ distance(Location)
|
||||
comment2.text=\n\ Determine\ the\ number\ of\ movements\ required\ to\ get\n\ from\ here\ to\ the\ destination.\n\ @param\ destination\ The\ required\ destination.\n\ @return\ The\ number\ of\ movement\ steps.\n
|
||||
comment3.params=other
|
||||
comment3.target=boolean\ equals(java.lang.Object)
|
||||
comment3.text=\n\ Implement\ content\ equality\ for\ locations.\n\ @return\ true\ if\ this\ location\ matches\ the\ other,\n\ \ \ \ \ \ \ \ \ false\ otherwise.\n
|
||||
comment4.params=
|
||||
comment4.target=java.lang.String\ toString()
|
||||
comment4.text=\n\ @return\ A\ representation\ of\ the\ location.\n
|
||||
comment5.params=
|
||||
comment5.target=int\ hashCode()
|
||||
comment5.text=\n\ Use\ the\ top\ 16\ bits\ for\ the\ y\ value\ and\ the\ bottom\ for\ the\ x.\n\ Except\ for\ very\ big\ grids,\ this\ should\ give\ a\ unique\ hash\ code\n\ for\ each\ (x,\ y)\ pair.\n\ @return\ A\ hashcode\ for\ the\ location.\n
|
||||
comment6.params=
|
||||
comment6.target=int\ getX()
|
||||
comment6.text=\n\ @return\ The\ x\ coordinate.\n
|
||||
comment7.params=
|
||||
comment7.target=int\ getY()
|
||||
comment7.text=\n\ @return\ The\ y\ coordinate.\n
|
||||
numComments=8
|
||||
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* Model a location in a city.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Location
|
||||
{
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
/**
|
||||
* Model a location in the city.
|
||||
* @param x The x coordinate. Must be positive.
|
||||
* @param y The y coordinate. Must be positive.
|
||||
* @throws IllegalArgumentException If a coordinate is negative.
|
||||
*/
|
||||
public Location(int x, int y)
|
||||
{
|
||||
if(x < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Negative x-coordinate: " + x);
|
||||
}
|
||||
if(y < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Negative y-coordinate: " + y);
|
||||
}
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the next location to visit in order to
|
||||
* reach the destination.
|
||||
* @param destination Where we want to get to.
|
||||
* @return A location in a direct line from this to
|
||||
* destination.
|
||||
*/
|
||||
public Location nextLocation(Location destination)
|
||||
{
|
||||
int destX = destination.getX();
|
||||
int destY = destination.getY();
|
||||
int offsetX = x < destX ? 1 : x > destX ? -1 : 0;
|
||||
int offsetY = y < destY ? 1 : y > destY ? -1 : 0;
|
||||
if(offsetX != 0 || offsetY != 0) {
|
||||
return new Location(x + offsetX, y + offsetY);
|
||||
}
|
||||
else {
|
||||
return destination;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the number of movements required to get
|
||||
* from here to the destination.
|
||||
* @param destination The required destination.
|
||||
* @return The number of movement steps.
|
||||
*/
|
||||
public int distance(Location destination)
|
||||
{
|
||||
int xDist = Math.abs(destination.getX() - x);
|
||||
int yDist = Math.abs(destination.getY() - y);
|
||||
return Math.max(xDist, yDist);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement content equality for locations.
|
||||
* @return true if this location matches the other,
|
||||
* false otherwise.
|
||||
*/
|
||||
public boolean equals(Object other)
|
||||
{
|
||||
if(other instanceof Location) {
|
||||
Location otherLocation = (Location) other;
|
||||
return x == otherLocation.getX() &&
|
||||
y == otherLocation.getY();
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A representation of the location.
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return "location " + x + "," + y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the top 16 bits for the y value and the bottom for the x.
|
||||
* Except for very big grids, this should give a unique hash code
|
||||
* for each (x, y) pair.
|
||||
* @return A hashcode for the location.
|
||||
*/
|
||||
public int hashCode()
|
||||
{
|
||||
return (y << 16) + x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The x coordinate.
|
||||
*/
|
||||
public int getX()
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The y coordinate.
|
||||
*/
|
||||
public int getY()
|
||||
{
|
||||
return y;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#BlueJ class context
|
||||
comment0.params=vehicle
|
||||
comment0.target=MissingPassengerException(Vehicle)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ MissingPassengerException.\n\ @param\ vehicle\ The\ vehicle\ expecting\ a\ passenger.\n
|
||||
comment1.params=
|
||||
comment1.target=Vehicle\ getVehicle()
|
||||
comment1.text=\n\ @return\ The\ vehicle\ for\ which\ there\ was\ no\ passenger.\n
|
||||
numComments=2
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
/**
|
||||
* Indicate that there was no passenger at a pickup point.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class MissingPassengerException extends RuntimeException
|
||||
{
|
||||
private Vehicle vehicle;
|
||||
/**
|
||||
* Constructor for objects of class MissingPassengerException.
|
||||
* @param vehicle The vehicle expecting a passenger.
|
||||
*/
|
||||
public MissingPassengerException(Vehicle vehicle)
|
||||
{
|
||||
super("Missing passenger at pickup location.");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The vehicle for which there was no passenger.
|
||||
*/
|
||||
public Vehicle getVehicle()
|
||||
{
|
||||
return vehicle;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#BlueJ class context
|
||||
comment0.params=pickup\ destination
|
||||
comment0.target=Passenger(Location,\ Location)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Passenger\n\ @param\ pickup\ The\ pickup\ location,\ must\ not\ be\ null.\n\ @param\ destination\ The\ destination\ location,\ must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ either\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=java.lang.String\ toString()
|
||||
comment1.text=\n\ @return\ A\ string\ representation\ of\ this\ person.\n
|
||||
comment2.params=
|
||||
comment2.target=java.awt.Image\ getImage()
|
||||
comment2.text=\n\ @return\ The\ image\ to\ be\ displayed\ on\ a\ GUI.\n
|
||||
comment3.params=
|
||||
comment3.target=Location\ getLocation()
|
||||
comment3.text=\n\ @return\ The\ passenger's\ pickup\ location.\n
|
||||
comment4.params=
|
||||
comment4.target=Location\ getPickupLocation()
|
||||
comment4.text=\n\ @return\ The\ pickup\ location.\n
|
||||
comment5.params=
|
||||
comment5.target=Location\ getDestination()
|
||||
comment5.text=\n\ @return\ The\ destination\ location.\n
|
||||
numComments=6
|
||||
@@ -0,0 +1,78 @@
|
||||
import java.awt.Image;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
/**
|
||||
* Model a passenger wishing to get from one
|
||||
* location to another.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Passenger implements DrawableItem
|
||||
{
|
||||
private Location pickup;
|
||||
private Location destination;
|
||||
private Image image;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Passenger
|
||||
* @param pickup The pickup location, must not be null.
|
||||
* @param destination The destination location, must not be null.
|
||||
* @throws NullPointerException If either location is null.
|
||||
*/
|
||||
public Passenger(Location pickup, Location destination)
|
||||
{
|
||||
if(pickup == null) {
|
||||
throw new NullPointerException("Pickup location");
|
||||
}
|
||||
if(destination == null) {
|
||||
throw new NullPointerException("Destination location");
|
||||
}
|
||||
this.pickup = pickup;
|
||||
this.destination = destination;
|
||||
// Load the image used to represent a person.
|
||||
image = new ImageIcon(getClass().getResource(
|
||||
"images/person.jpg")).getImage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A string representation of this person.
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return "Passenger travelling from " +
|
||||
pickup + " to " + destination;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The image to be displayed on a GUI.
|
||||
*/
|
||||
public Image getImage()
|
||||
{
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The passenger's pickup location.
|
||||
*/
|
||||
public Location getLocation()
|
||||
{
|
||||
return pickup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The pickup location.
|
||||
*/
|
||||
public Location getPickupLocation()
|
||||
{
|
||||
return pickup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The destination location.
|
||||
*/
|
||||
public Location getDestination()
|
||||
{
|
||||
return destination;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=city\ company
|
||||
comment0.target=PassengerSource(City,\ TaxiCompany)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ PassengerSource.\n\ @param\ city\ The\ city.\ Must\ not\ be\ null.\n\ @param\ company\ The\ company\ to\ be\ used.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ if\ city\ or\ company\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ act()
|
||||
comment1.text=\n\ Randomly\ generate\ a\ new\ passenger.\n\ Keep\ a\ count\ of\ missed\ pickups.\n
|
||||
comment2.params=
|
||||
comment2.target=int\ getMissedPickups()
|
||||
comment2.text=\n\ @return\ The\ number\ of\ passengers\ for\ whom\ a\ pickup\n\ \ \ \ \ \ \ \ \ could\ not\ be\ found.\n
|
||||
comment3.params=
|
||||
comment3.target=Passenger\ createPassenger()
|
||||
comment3.text=\n\ Create\ a\ new\ passenger\ with\ distinct\ pickup\ and\n\ destination\ locations.\n\ @return\ The\ created\ passenger.\n
|
||||
numComments=4
|
||||
@@ -0,0 +1,91 @@
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Periodically generate passengers.
|
||||
* Keep track of the number of passengers for whom
|
||||
* a vehicle cannot be found.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class PassengerSource implements Actor
|
||||
{
|
||||
private City city;
|
||||
private TaxiCompany company;
|
||||
private Random rand;
|
||||
private static final double CREATION_PROBABILITY = 0.06;
|
||||
private int missedPickups;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class PassengerSource.
|
||||
* @param city The city. Must not be null.
|
||||
* @param company The company to be used. Must not be null.
|
||||
* @throws NullPointerException if city or company is null.
|
||||
*/
|
||||
public PassengerSource(City city, TaxiCompany company)
|
||||
{
|
||||
if(city == null) {
|
||||
throw new NullPointerException("city");
|
||||
}
|
||||
if(company == null) {
|
||||
throw new NullPointerException("company");
|
||||
}
|
||||
this.city = city;
|
||||
this.company = company;
|
||||
// Use a fixed random seed for repeatable effects.
|
||||
// Change this to produce more random effects.
|
||||
rand = new Random(12345);
|
||||
missedPickups = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Randomly generate a new passenger.
|
||||
* Keep a count of missed pickups.
|
||||
*/
|
||||
public void act()
|
||||
{
|
||||
if(rand.nextDouble() <= CREATION_PROBABILITY) {
|
||||
Passenger passenger = createPassenger();
|
||||
if(company.requestPickup(passenger)) {
|
||||
city.addItem(passenger);
|
||||
}
|
||||
else {
|
||||
missedPickups++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The number of passengers for whom a pickup
|
||||
* could not be found.
|
||||
*/
|
||||
public int getMissedPickups()
|
||||
{
|
||||
return missedPickups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new passenger with distinct pickup and
|
||||
* destination locations.
|
||||
* @return The created passenger.
|
||||
*/
|
||||
private Passenger createPassenger()
|
||||
{
|
||||
int cityWidth = city.getWidth();
|
||||
int cityHeight = city.getHeight();
|
||||
|
||||
Location pickupLocation =
|
||||
new Location(rand.nextInt(cityWidth),
|
||||
rand.nextInt(cityHeight));
|
||||
Location destination;
|
||||
do{
|
||||
destination =
|
||||
new Location(rand.nextInt(cityWidth),
|
||||
rand.nextInt(cityHeight));
|
||||
} while(pickupLocation.equals(destination));
|
||||
return new Passenger(pickupLocation, destination);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
Project: taxi-company-later-stage
|
||||
Authors: David J. Barnes and Michael Kölling
|
||||
|
||||
This project is part of the material for chapter 14 of the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
This project provides a partial implementation of a simulation of
|
||||
taxis operating on a city grid to pick up and transport passengers.
|
||||
|
||||
This is the fourth stage of this project. It illustrates the
|
||||
implementation of operation with multiple taxis.
|
||||
|
||||
How to start this project:
|
||||
Create a Simulation object and invoke its run method.
|
||||
|
||||
Use this version to explore further development ideas, such
|
||||
as implementation of the Shuttle class.
|
||||
@@ -0,0 +1,23 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company\ location
|
||||
comment0.target=Shuttle(TaxiCompany,\ Location)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Shuttle\n\ @param\ company\ The\ taxi\ company.\ Must\ not\ be\ null.\n\ @param\ location\ The\ vehicle's\ starting\ point.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ company\ or\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ act()
|
||||
comment1.text=\n\ Carry\ out\ a\ shuttle's\ actions.\n
|
||||
comment2.params=
|
||||
comment2.target=boolean\ isFree()
|
||||
comment2.text=\n\ Is\ the\ shuttle\ free?\n\ @return\ Whether\ or\ not\ this\ vehicle\ is\ free.\n
|
||||
comment3.params=location
|
||||
comment3.target=void\ setPickupLocation(Location)
|
||||
comment3.text=\n\ Receive\ a\ pickup\ location.\n\ @param\ location\ The\ pickup\ location.\n
|
||||
comment4.params=passenger
|
||||
comment4.target=void\ pickup(Passenger)
|
||||
comment4.text=\n\ Receive\ a\ passenger.\n\ Add\ their\ destination\ to\ the\ list.\n\ @param\ passenger\ The\ passenger.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ chooseTargetLocation()
|
||||
comment5.text=\n\ Decide\ where\ to\ go\ next,\ based\ on\ the\ list\ of\n\ possible\ destinations.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ offloadPassenger()
|
||||
comment6.text=\n\ Offload\ a\ passenger\ whose\ destination\ is\ the\n\ current\ location.\n
|
||||
numComments=7
|
||||
@@ -0,0 +1,84 @@
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* A shuttle is able to carry multiple passengers.
|
||||
* This implementation is non-functional.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Shuttle extends Vehicle
|
||||
{
|
||||
// The list of destinations for the shuttle.
|
||||
private List<Location> destinations;
|
||||
// The list of passengers on the shuttle.
|
||||
private List<Passenger> passengers;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Shuttle
|
||||
* @param company The taxi company. Must not be null.
|
||||
* @param location The vehicle's starting point. Must not be null.
|
||||
* @throws NullPointerException If company or location is null.
|
||||
*/
|
||||
public Shuttle(TaxiCompany company, Location location)
|
||||
{
|
||||
super(company, location);
|
||||
destinations = new LinkedList<>();
|
||||
passengers = new LinkedList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Carry out a shuttle's actions.
|
||||
*/
|
||||
public void act()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the shuttle free?
|
||||
* @return Whether or not this vehicle is free.
|
||||
*/
|
||||
public boolean isFree()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a pickup location.
|
||||
* @param location The pickup location.
|
||||
*/
|
||||
public void setPickupLocation(Location location)
|
||||
{
|
||||
destinations.add(location);
|
||||
chooseTargetLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a passenger.
|
||||
* Add their destination to the list.
|
||||
* @param passenger The passenger.
|
||||
*/
|
||||
public void pickup(Passenger passenger)
|
||||
{
|
||||
passengers.add(passenger);
|
||||
destinations.add(passenger.getDestination());
|
||||
chooseTargetLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide where to go next, based on the list of
|
||||
* possible destinations.
|
||||
*/
|
||||
private void chooseTargetLocation()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Offload a passenger whose destination is the
|
||||
* current location.
|
||||
*/
|
||||
public void offloadPassenger()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Simulation()
|
||||
comment0.text=\n\ Create\ the\ initial\ set\ of\ actors\ for\ the\ simulation.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ run()
|
||||
comment1.text=\n\ Run\ the\ simulation\ for\ a\ fixed\ number\ of\ steps.\n\ Pause\ after\ each\ step\ to\ allow\ the\ GUI\ to\ keep\ up.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ step()
|
||||
comment2.text=\n\ Take\ a\ single\ step\ of\ the\ simulation.\n
|
||||
comment3.params=milliseconds
|
||||
comment3.target=void\ wait(int)
|
||||
comment3.text=\n\ Wait\ for\ a\ specified\ number\ of\ milliseconds\ before\ finishing.\n\ This\ provides\ an\ easy\ way\ to\ cause\ a\ small\ delay.\n\ @param\ milliseconds\ The\ number\ of\ milliseconds\ to\ wait.\n
|
||||
numComments=4
|
||||
@@ -0,0 +1,71 @@
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Run the simulation by asking a collection of actors to act.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Simulation
|
||||
{
|
||||
private List<Actor> actors;
|
||||
private int step;
|
||||
|
||||
/**
|
||||
* Create the initial set of actors for the simulation.
|
||||
*/
|
||||
public Simulation()
|
||||
{
|
||||
actors = new LinkedList<>();
|
||||
step = 0;
|
||||
City city = new City();
|
||||
TaxiCompany company = new TaxiCompany(city);
|
||||
PassengerSource source = new PassengerSource(city, company);
|
||||
|
||||
actors.addAll(company.getVehicles());
|
||||
actors.add(source);
|
||||
actors.add(new CityGUI(city));
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the simulation for a fixed number of steps.
|
||||
* Pause after each step to allow the GUI to keep up.
|
||||
*/
|
||||
public void run()
|
||||
{
|
||||
for(int i = 0; i < 500; i++){
|
||||
step++;
|
||||
step();
|
||||
wait(100);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Take a single step of the simulation.
|
||||
*/
|
||||
public void step()
|
||||
{
|
||||
for(Actor actor : actors) {
|
||||
actor.act();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for a specified number of milliseconds before finishing.
|
||||
* This provides an easy way to cause a small delay.
|
||||
* @param milliseconds The number of milliseconds to wait.
|
||||
*/
|
||||
private void wait(int milliseconds)
|
||||
{
|
||||
try
|
||||
{
|
||||
Thread.sleep(milliseconds);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
// ignore the exception
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company\ location
|
||||
comment0.target=Taxi(TaxiCompany,\ Location)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Taxi\n\ @param\ company\ The\ taxi\ company.\ Must\ not\ be\ null.\n\ @param\ location\ The\ vehicle's\ starting\ point.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ company\ or\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ act()
|
||||
comment1.text=\n\ Move\ towards\ the\ target\ location\ if\ we\ have\ one.\n\ Otherwise\ record\ that\ we\ are\ idle.\n
|
||||
comment2.params=
|
||||
comment2.target=boolean\ isFree()
|
||||
comment2.text=\n\ Is\ the\ taxi\ free?\n\ @return\ Whether\ or\ not\ this\ taxi\ is\ free.\n
|
||||
comment3.params=location
|
||||
comment3.target=void\ setPickupLocation(Location)
|
||||
comment3.text=\n\ Receive\ a\ pickup\ location.\ This\ becomes\ the\n\ target\ location.\n\ @param\ location\ The\ pickup\ location.\n
|
||||
comment4.params=passenger
|
||||
comment4.target=void\ pickup(Passenger)
|
||||
comment4.text=\n\ Receive\ a\ passenger.\n\ Set\ their\ destination\ as\ the\ target\ location.\n\ @param\ passenger\ The\ passenger.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ offloadPassenger()
|
||||
comment5.text=\n\ Offload\ the\ passenger.\n
|
||||
comment6.params=
|
||||
comment6.target=java.awt.Image\ getImage()
|
||||
comment6.text=\n\ Return\ an\ image\ that\ describes\ our\ state\:\n\ either\ empty\ or\ carrying\ a\ passenger.\n
|
||||
comment7.params=
|
||||
comment7.target=java.lang.String\ toString()
|
||||
comment7.text=\n\ Return\ details\ of\ the\ taxi,\ such\ as\ where\ it\ is.\n\ @return\ A\ string\ representation\ of\ the\ taxi.\n
|
||||
numComments=8
|
||||
@@ -0,0 +1,121 @@
|
||||
import java.awt.Image;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
/**
|
||||
* A taxi is able to carry a single passenger.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Taxi extends Vehicle implements DrawableItem
|
||||
{
|
||||
private Passenger passenger;
|
||||
// Maintain separate images for when the taxi is empty
|
||||
// and full.
|
||||
private Image emptyImage, passengerImage;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Taxi
|
||||
* @param company The taxi company. Must not be null.
|
||||
* @param location The vehicle's starting point. Must not be null.
|
||||
* @throws NullPointerException If company or location is null.
|
||||
*/
|
||||
public Taxi(TaxiCompany company, Location location)
|
||||
{
|
||||
super(company, location);
|
||||
// Load the two images.
|
||||
emptyImage = new ImageIcon(getClass().getResource(
|
||||
"images/taxi.jpg")).getImage();
|
||||
|
||||
passengerImage = new ImageIcon(getClass().getResource(
|
||||
"images/taxi+person.jpg")).getImage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Move towards the target location if we have one.
|
||||
* Otherwise record that we are idle.
|
||||
*/
|
||||
public void act()
|
||||
{
|
||||
Location target = getTargetLocation();
|
||||
if(target != null) {
|
||||
// Find where to move to next.
|
||||
Location next = getLocation().nextLocation(target);
|
||||
setLocation(next);
|
||||
if(next.equals(target)) {
|
||||
if(passenger != null) {
|
||||
notifyPassengerArrival(passenger);
|
||||
offloadPassenger();
|
||||
}
|
||||
else {
|
||||
notifyPickupArrival();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
incrementIdleCount();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the taxi free?
|
||||
* @return Whether or not this taxi is free.
|
||||
*/
|
||||
public boolean isFree()
|
||||
{
|
||||
return getTargetLocation() == null && passenger == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a pickup location. This becomes the
|
||||
* target location.
|
||||
* @param location The pickup location.
|
||||
*/
|
||||
public void setPickupLocation(Location location)
|
||||
{
|
||||
setTargetLocation(location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a passenger.
|
||||
* Set their destination as the target location.
|
||||
* @param passenger The passenger.
|
||||
*/
|
||||
public void pickup(Passenger passenger)
|
||||
{
|
||||
this.passenger = passenger;
|
||||
setTargetLocation(passenger.getDestination());
|
||||
}
|
||||
|
||||
/**
|
||||
* Offload the passenger.
|
||||
*/
|
||||
public void offloadPassenger()
|
||||
{
|
||||
passenger = null;
|
||||
clearTargetLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an image that describes our state:
|
||||
* either empty or carrying a passenger.
|
||||
*/
|
||||
public Image getImage()
|
||||
{
|
||||
if(passenger != null) {
|
||||
return passengerImage;
|
||||
}
|
||||
else {
|
||||
return emptyImage;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return details of the taxi, such as where it is.
|
||||
* @return A string representation of the taxi.
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return "Taxi at " + getLocation();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#BlueJ class context
|
||||
comment0.params=city
|
||||
comment0.target=TaxiCompany(City)
|
||||
comment0.text=\n\ @param\ city\ The\ city.\n
|
||||
comment1.params=passenger
|
||||
comment1.target=boolean\ requestPickup(Passenger)
|
||||
comment1.text=\n\ Request\ a\ pickup\ for\ the\ given\ passenger.\n\ @param\ passenger\ The\ passenger\ requesting\ a\ pickup.\n\ @return\ Whether\ a\ free\ vehicle\ is\ available.\n
|
||||
comment2.params=vehicle
|
||||
comment2.target=void\ arrivedAtPickup(Vehicle)
|
||||
comment2.text=\n\ A\ vehicle\ has\ arrived\ at\ a\ pickup\ point.\n\ @param\ vehicle\ The\ vehicle\ at\ the\ pickup\ point.\n\ @throws\ MissingPassengerException\ If\ there\ is\ no\ passenger\ waiting.\n
|
||||
comment3.params=vehicle\ passenger
|
||||
comment3.target=void\ arrivedAtDestination(Vehicle,\ Passenger)
|
||||
comment3.text=\n\ A\ vehicle\ has\ arrived\ at\ a\ passenger's\ destination.\n\ @param\ vehicle\ The\ vehicle\ at\ the\ destination.\n\ @param\ passenger\ The\ passenger\ being\ dropped\ off.\n
|
||||
comment4.params=
|
||||
comment4.target=java.util.List\ getVehicles()
|
||||
comment4.text=\n\ @return\ The\ list\ of\ vehicles.\n
|
||||
comment5.params=
|
||||
comment5.target=Vehicle\ scheduleVehicle()
|
||||
comment5.text=\n\ Find\ a\ free\ vehicle,\ if\ any.\n\ @return\ A\ free\ vehicle,\ or\ null\ if\ there\ is\ none.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ setupVehicles()
|
||||
comment6.text=\n\ Set\ up\ this\ company's\ vehicles.\ The\ optimum\ number\ of\n\ vehicles\ should\ be\ determined\ by\ analysis\ of\ the\n\ data\ gathered\ from\ the\ simulation.\n\n\ Vehicles\ start\ at\ random\ locations.\n
|
||||
numComments=7
|
||||
@@ -0,0 +1,129 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Model the operation of a taxi company, operating different
|
||||
* types of vehicle. This version operates a only taxis.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class TaxiCompany
|
||||
{
|
||||
// The vehicles operated by the company.
|
||||
private List<Vehicle> vehicles;
|
||||
private City city;
|
||||
// The associations between vehicles and the passengers
|
||||
// they are to pick up.
|
||||
private Map<Vehicle, Passenger> assignments;
|
||||
|
||||
private static final int NUMBER_OF_TAXIS = 3;
|
||||
|
||||
/**
|
||||
* @param city The city.
|
||||
*/
|
||||
public TaxiCompany(City city)
|
||||
{
|
||||
this.city = city;
|
||||
vehicles = new LinkedList<>();
|
||||
assignments = new HashMap<>();
|
||||
setupVehicles();
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a pickup for the given passenger.
|
||||
* @param passenger The passenger requesting a pickup.
|
||||
* @return Whether a free vehicle is available.
|
||||
*/
|
||||
public boolean requestPickup(Passenger passenger)
|
||||
{
|
||||
Vehicle vehicle = scheduleVehicle();
|
||||
if(vehicle != null) {
|
||||
assignments.put(vehicle, passenger);
|
||||
vehicle.setPickupLocation(passenger.getPickupLocation());
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A vehicle has arrived at a pickup point.
|
||||
* @param vehicle The vehicle at the pickup point.
|
||||
* @throws MissingPassengerException If there is no passenger waiting.
|
||||
*/
|
||||
public void arrivedAtPickup(Vehicle vehicle)
|
||||
{
|
||||
Passenger passenger = assignments.remove(vehicle);
|
||||
if(passenger == null) {
|
||||
throw new MissingPassengerException(vehicle);
|
||||
}
|
||||
city.removeItem(passenger);
|
||||
vehicle.pickup(passenger);
|
||||
}
|
||||
|
||||
/**
|
||||
* A vehicle has arrived at a passenger's destination.
|
||||
* @param vehicle The vehicle at the destination.
|
||||
* @param passenger The passenger being dropped off.
|
||||
*/
|
||||
public void arrivedAtDestination(Vehicle vehicle,
|
||||
Passenger passenger)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The list of vehicles.
|
||||
*/
|
||||
public List<Vehicle> getVehicles()
|
||||
{
|
||||
return vehicles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a free vehicle, if any.
|
||||
* @return A free vehicle, or null if there is none.
|
||||
*/
|
||||
private Vehicle scheduleVehicle()
|
||||
{
|
||||
Iterator<Vehicle> it = vehicles.iterator();
|
||||
while(it.hasNext()) {
|
||||
Vehicle vehicle = it.next();
|
||||
if(vehicle.isFree()) {
|
||||
return vehicle;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up this company's vehicles. The optimum number of
|
||||
* vehicles should be determined by analysis of the
|
||||
* data gathered from the simulation.
|
||||
*
|
||||
* Vehicles start at random locations.
|
||||
*/
|
||||
private void setupVehicles()
|
||||
{
|
||||
int cityWidth = city.getWidth();
|
||||
int cityHeight = city.getHeight();
|
||||
// Used a fixed random seed for predictable behavior.
|
||||
// Use different seeds for less predictable behavior.
|
||||
Random rand = new Random(12345);
|
||||
|
||||
// Create the taxis.
|
||||
for(int i = 0; i < NUMBER_OF_TAXIS; i++){
|
||||
Taxi taxi =
|
||||
new Taxi(this,
|
||||
new Location(rand.nextInt(cityWidth),
|
||||
rand.nextInt(cityHeight)));
|
||||
vehicles.add(taxi);
|
||||
city.addItem(taxi);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company\ location
|
||||
comment0.target=Vehicle(TaxiCompany,\ Location)
|
||||
comment0.text=\n\ Constructor\ of\ class\ Vehicle\n\ @param\ company\ The\ taxi\ company.\ Must\ not\ be\ null.\n\ @param\ location\ The\ vehicle's\ starting\ point.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ company\ or\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ notifyPickupArrival()
|
||||
comment1.text=\n\ Notify\ the\ company\ of\ our\ arrival\ at\ a\ pickup\ location.\n
|
||||
comment10.params=location
|
||||
comment10.target=void\ setTargetLocation(Location)
|
||||
comment10.text=\n\ Set\ the\ required\ target\ location.\n\ @param\ location\ Where\ to\ go.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ location\ is\ null.\n
|
||||
comment11.params=
|
||||
comment11.target=void\ clearTargetLocation()
|
||||
comment11.text=\n\ Clear\ the\ target\ location.\n
|
||||
comment12.params=
|
||||
comment12.target=int\ getIdleCount()
|
||||
comment12.text=\n\ @return\ On\ how\ many\ steps\ this\ vehicle\ has\ been\ idle.\n
|
||||
comment13.params=
|
||||
comment13.target=void\ incrementIdleCount()
|
||||
comment13.text=\n\ Increment\ the\ number\ of\ steps\ on\ which\ this\ vehicle\n\ has\ been\ idle.\n
|
||||
comment2.params=passenger
|
||||
comment2.target=void\ notifyPassengerArrival(Passenger)
|
||||
comment2.text=\n\ Notify\ the\ company\ of\ our\ arrival\ at\ a\n\ passenger's\ destination.\n
|
||||
comment3.params=location
|
||||
comment3.target=void\ setPickupLocation(Location)
|
||||
comment3.text=\n\ Receive\ a\ pickup\ location.\n\ How\ this\ is\ handled\ depends\ on\ the\ type\ of\ vehicle.\n\ @param\ location\ The\ pickup\ location.\n
|
||||
comment4.params=passenger
|
||||
comment4.target=void\ pickup(Passenger)
|
||||
comment4.text=\n\ Receive\ a\ passenger.\n\ How\ this\ is\ handled\ depends\ on\ the\ type\ of\ vehicle.\n\ @param\ passenger\ The\ passenger.\n
|
||||
comment5.params=
|
||||
comment5.target=boolean\ isFree()
|
||||
comment5.text=\n\ Is\ the\ vehicle\ free?\n\ @return\ Whether\ or\ not\ this\ vehicle\ is\ free.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ offloadPassenger()
|
||||
comment6.text=\n\ Offload\ any\ passengers\ whose\ destination\ is\ the\n\ current\ location.\n
|
||||
comment7.params=
|
||||
comment7.target=Location\ getLocation()
|
||||
comment7.text=\n\ @return\ Where\ this\ vehicle\ is\ currently\ located.\n
|
||||
comment8.params=location
|
||||
comment8.target=void\ setLocation(Location)
|
||||
comment8.text=\n\ Set\ the\ current\ location.\n\ @param\ location\ Where\ it\ is.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ location\ is\ null.\n
|
||||
comment9.params=
|
||||
comment9.target=Location\ getTargetLocation()
|
||||
comment9.text=\n\ Get\ the\ target\ location.\n\ @return\ Where\ this\ vehicle\ is\ currently\ headed,\ or\ null\n\ \ \ \ \ \ \ \ \ if\ it\ is\ idle.\n
|
||||
numComments=14
|
||||
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* Model the common elements of taxis and shuttles.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public abstract class Vehicle implements Actor
|
||||
{
|
||||
private TaxiCompany company;
|
||||
// Where the vehicle is.
|
||||
private Location location;
|
||||
// Where the vehicle is headed.
|
||||
private Location targetLocation;
|
||||
// Record how often the vehicle has nothing to do.
|
||||
private int idleCount;
|
||||
|
||||
/**
|
||||
* Constructor of class Vehicle
|
||||
* @param company The taxi company. Must not be null.
|
||||
* @param location The vehicle's starting point. Must not be null.
|
||||
* @throws NullPointerException If company or location is null.
|
||||
*/
|
||||
public Vehicle(TaxiCompany company, Location location)
|
||||
{
|
||||
if(company == null) {
|
||||
throw new NullPointerException("company");
|
||||
}
|
||||
if(location == null) {
|
||||
throw new NullPointerException("location");
|
||||
}
|
||||
this.company = company;
|
||||
this.location = location;
|
||||
targetLocation = null;
|
||||
idleCount = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the company of our arrival at a pickup location.
|
||||
*/
|
||||
public void notifyPickupArrival()
|
||||
{
|
||||
company.arrivedAtPickup(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the company of our arrival at a
|
||||
* passenger's destination.
|
||||
*/
|
||||
public void notifyPassengerArrival(Passenger passenger)
|
||||
{
|
||||
company.arrivedAtDestination(this, passenger);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a pickup location.
|
||||
* How this is handled depends on the type of vehicle.
|
||||
* @param location The pickup location.
|
||||
*/
|
||||
public abstract void setPickupLocation(Location location);
|
||||
|
||||
/**
|
||||
* Receive a passenger.
|
||||
* How this is handled depends on the type of vehicle.
|
||||
* @param passenger The passenger.
|
||||
*/
|
||||
public abstract void pickup(Passenger passenger);
|
||||
|
||||
/**
|
||||
* Is the vehicle free?
|
||||
* @return Whether or not this vehicle is free.
|
||||
*/
|
||||
public abstract boolean isFree();
|
||||
|
||||
/**
|
||||
* Offload any passengers whose destination is the
|
||||
* current location.
|
||||
*/
|
||||
public abstract void offloadPassenger();
|
||||
|
||||
/**
|
||||
* @return Where this vehicle is currently located.
|
||||
*/
|
||||
public Location getLocation()
|
||||
{
|
||||
return location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current location.
|
||||
* @param location Where it is. Must not be null.
|
||||
* @throws NullPointerException If location is null.
|
||||
*/
|
||||
public void setLocation(Location location)
|
||||
{
|
||||
if(location != null) {
|
||||
this.location = location;
|
||||
}
|
||||
else {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the target location.
|
||||
* @return Where this vehicle is currently headed, or null
|
||||
* if it is idle.
|
||||
*/
|
||||
public Location getTargetLocation()
|
||||
{
|
||||
return targetLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the required target location.
|
||||
* @param location Where to go. Must not be null.
|
||||
* @throws NullPointerException If location is null.
|
||||
*/
|
||||
public void setTargetLocation(Location location)
|
||||
{
|
||||
if(location != null) {
|
||||
targetLocation = location;
|
||||
}
|
||||
else {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the target location.
|
||||
*/
|
||||
public void clearTargetLocation()
|
||||
{
|
||||
targetLocation = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return On how many steps this vehicle has been idle.
|
||||
*/
|
||||
public int getIdleCount()
|
||||
{
|
||||
return idleCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the number of steps on which this vehicle
|
||||
* has been idle.
|
||||
*/
|
||||
public void incrementIdleCount()
|
||||
{
|
||||
idleCount++;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.7 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.3 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
@@ -0,0 +1,294 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=Shuttle
|
||||
dependency1.to=TaxiCompany
|
||||
dependency1.type=UsesDependency
|
||||
dependency10.from=MissingPassengerException
|
||||
dependency10.to=Vehicle
|
||||
dependency10.type=UsesDependency
|
||||
dependency11.from=Taxi
|
||||
dependency11.to=Passenger
|
||||
dependency11.type=UsesDependency
|
||||
dependency12.from=Taxi
|
||||
dependency12.to=TaxiCompany
|
||||
dependency12.type=UsesDependency
|
||||
dependency13.from=Taxi
|
||||
dependency13.to=Location
|
||||
dependency13.type=UsesDependency
|
||||
dependency14.from=Item
|
||||
dependency14.to=Location
|
||||
dependency14.type=UsesDependency
|
||||
dependency15.from=TaxiCompany
|
||||
dependency15.to=City
|
||||
dependency15.type=UsesDependency
|
||||
dependency16.from=TaxiCompany
|
||||
dependency16.to=Passenger
|
||||
dependency16.type=UsesDependency
|
||||
dependency17.from=TaxiCompany
|
||||
dependency17.to=Vehicle
|
||||
dependency17.type=UsesDependency
|
||||
dependency18.from=TaxiCompany
|
||||
dependency18.to=MissingPassengerException
|
||||
dependency18.type=UsesDependency
|
||||
dependency19.from=TaxiCompany
|
||||
dependency19.to=Taxi
|
||||
dependency19.type=UsesDependency
|
||||
dependency2.from=Shuttle
|
||||
dependency2.to=Location
|
||||
dependency2.type=UsesDependency
|
||||
dependency20.from=TaxiCompany
|
||||
dependency20.to=Location
|
||||
dependency20.type=UsesDependency
|
||||
dependency21.from=CityGUI
|
||||
dependency21.to=City
|
||||
dependency21.type=UsesDependency
|
||||
dependency22.from=CityGUI
|
||||
dependency22.to=DrawableItem
|
||||
dependency22.type=UsesDependency
|
||||
dependency23.from=CityGUI
|
||||
dependency23.to=Location
|
||||
dependency23.type=UsesDependency
|
||||
dependency24.from=PassengerSource
|
||||
dependency24.to=City
|
||||
dependency24.type=UsesDependency
|
||||
dependency25.from=PassengerSource
|
||||
dependency25.to=TaxiCompany
|
||||
dependency25.type=UsesDependency
|
||||
dependency26.from=PassengerSource
|
||||
dependency26.to=Passenger
|
||||
dependency26.type=UsesDependency
|
||||
dependency27.from=PassengerSource
|
||||
dependency27.to=Location
|
||||
dependency27.type=UsesDependency
|
||||
dependency28.from=Vehicle
|
||||
dependency28.to=TaxiCompany
|
||||
dependency28.type=UsesDependency
|
||||
dependency29.from=Vehicle
|
||||
dependency29.to=Location
|
||||
dependency29.type=UsesDependency
|
||||
dependency3.from=Shuttle
|
||||
dependency3.to=Passenger
|
||||
dependency3.type=UsesDependency
|
||||
dependency30.from=Vehicle
|
||||
dependency30.to=Passenger
|
||||
dependency30.type=UsesDependency
|
||||
dependency31.from=City
|
||||
dependency31.to=Item
|
||||
dependency31.type=UsesDependency
|
||||
dependency32.from=CityGUI
|
||||
dependency32.to=Item
|
||||
dependency32.type=UsesDependency
|
||||
dependency4.from=Simulation
|
||||
dependency4.to=City
|
||||
dependency4.type=UsesDependency
|
||||
dependency5.from=Simulation
|
||||
dependency5.to=TaxiCompany
|
||||
dependency5.type=UsesDependency
|
||||
dependency6.from=Simulation
|
||||
dependency6.to=PassengerSource
|
||||
dependency6.type=UsesDependency
|
||||
dependency7.from=Simulation
|
||||
dependency7.to=CityGUI
|
||||
dependency7.type=UsesDependency
|
||||
dependency8.from=Simulation
|
||||
dependency8.to=Actor
|
||||
dependency8.type=UsesDependency
|
||||
dependency9.from=Passenger
|
||||
dependency9.to=Location
|
||||
dependency9.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=1080
|
||||
package.editor.height=541
|
||||
package.editor.width=972
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=32
|
||||
package.numTargets=14
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=567
|
||||
readme.editor.width=790
|
||||
readme.editor.x=53
|
||||
readme.editor.y=23
|
||||
target1.editor.height=700
|
||||
target1.editor.width=900
|
||||
target1.editor.x=53
|
||||
target1.editor.y=23
|
||||
target1.height=50
|
||||
target1.name=MissingPassengerException
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=190
|
||||
target1.x=130
|
||||
target1.y=430
|
||||
target10.editor.height=734
|
||||
target10.editor.width=763
|
||||
target10.editor.x=53
|
||||
target10.editor.y=60
|
||||
target10.height=50
|
||||
target10.name=Taxi
|
||||
target10.naviview.expanded=false
|
||||
target10.showInterface=false
|
||||
target10.type=ClassTarget
|
||||
target10.typeParameters=
|
||||
target10.width=80
|
||||
target10.x=570
|
||||
target10.y=270
|
||||
target11.editor.height=700
|
||||
target11.editor.width=900
|
||||
target11.editor.x=53
|
||||
target11.editor.y=23
|
||||
target11.height=50
|
||||
target11.name=Passenger
|
||||
target11.naviview.expanded=true
|
||||
target11.showInterface=false
|
||||
target11.type=ClassTarget
|
||||
target11.typeParameters=
|
||||
target11.width=90
|
||||
target11.x=760
|
||||
target11.y=270
|
||||
target12.editor.height=730
|
||||
target12.editor.width=785
|
||||
target12.editor.x=53
|
||||
target12.editor.y=60
|
||||
target12.height=50
|
||||
target12.name=PassengerSource
|
||||
target12.naviview.expanded=true
|
||||
target12.showInterface=false
|
||||
target12.type=ClassTarget
|
||||
target12.typeParameters=
|
||||
target12.width=130
|
||||
target12.x=320
|
||||
target12.y=130
|
||||
target13.editor.height=702
|
||||
target13.editor.width=828
|
||||
target13.editor.x=53
|
||||
target13.editor.y=60
|
||||
target13.height=50
|
||||
target13.name=Shuttle
|
||||
target13.naviview.expanded=false
|
||||
target13.showInterface=false
|
||||
target13.type=ClassTarget
|
||||
target13.typeParameters=
|
||||
target13.width=80
|
||||
target13.x=430
|
||||
target13.y=270
|
||||
target14.editor.height=700
|
||||
target14.editor.width=900
|
||||
target14.editor.x=53
|
||||
target14.editor.y=23
|
||||
target14.height=50
|
||||
target14.name=Location
|
||||
target14.naviview.expanded=true
|
||||
target14.showInterface=false
|
||||
target14.type=ClassTarget
|
||||
target14.typeParameters=
|
||||
target14.width=80
|
||||
target14.x=690
|
||||
target14.y=410
|
||||
target2.editor.height=728
|
||||
target2.editor.width=797
|
||||
target2.editor.x=53
|
||||
target2.editor.y=60
|
||||
target2.height=50
|
||||
target2.name=City
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=80
|
||||
target2.x=260
|
||||
target2.y=230
|
||||
target3.editor.height=700
|
||||
target3.editor.width=900
|
||||
target3.editor.x=53
|
||||
target3.editor.y=23
|
||||
target3.height=50
|
||||
target3.name=DrawableItem
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=InterfaceTarget
|
||||
target3.typeParameters=
|
||||
target3.width=100
|
||||
target3.x=660
|
||||
target3.y=180
|
||||
target4.editor.height=719
|
||||
target4.editor.width=796
|
||||
target4.editor.x=53
|
||||
target4.editor.y=60
|
||||
target4.height=50
|
||||
target4.name=Vehicle
|
||||
target4.naviview.expanded=false
|
||||
target4.showInterface=false
|
||||
target4.type=AbstractTarget
|
||||
target4.typeParameters=
|
||||
target4.width=80
|
||||
target4.x=490
|
||||
target4.y=130
|
||||
target5.editor.height=700
|
||||
target5.editor.width=900
|
||||
target5.editor.x=53
|
||||
target5.editor.y=23
|
||||
target5.height=50
|
||||
target5.name=Item
|
||||
target5.naviview.expanded=true
|
||||
target5.showInterface=false
|
||||
target5.type=InterfaceTarget
|
||||
target5.typeParameters=
|
||||
target5.width=80
|
||||
target5.x=850
|
||||
target5.y=90
|
||||
target6.editor.height=724
|
||||
target6.editor.width=802
|
||||
target6.editor.x=53
|
||||
target6.editor.y=60
|
||||
target6.height=50
|
||||
target6.name=TaxiCompany
|
||||
target6.naviview.expanded=true
|
||||
target6.showInterface=false
|
||||
target6.type=ClassTarget
|
||||
target6.typeParameters=
|
||||
target6.width=100
|
||||
target6.x=110
|
||||
target6.y=320
|
||||
target7.editor.height=719
|
||||
target7.editor.width=829
|
||||
target7.editor.x=53
|
||||
target7.editor.y=60
|
||||
target7.height=50
|
||||
target7.name=Simulation
|
||||
target7.naviview.expanded=true
|
||||
target7.showInterface=false
|
||||
target7.type=ClassTarget
|
||||
target7.typeParameters=
|
||||
target7.width=80
|
||||
target7.x=60
|
||||
target7.y=20
|
||||
target8.editor.height=735
|
||||
target8.editor.width=648
|
||||
target8.editor.x=53
|
||||
target8.editor.y=60
|
||||
target8.height=50
|
||||
target8.name=Actor
|
||||
target8.naviview.expanded=true
|
||||
target8.showInterface=false
|
||||
target8.type=InterfaceTarget
|
||||
target8.typeParameters=
|
||||
target8.width=80
|
||||
target8.x=330
|
||||
target8.y=30
|
||||
target9.editor.height=732
|
||||
target9.editor.width=974
|
||||
target9.editor.x=64
|
||||
target9.editor.y=83
|
||||
target9.height=50
|
||||
target9.name=CityGUI
|
||||
target9.naviview.expanded=true
|
||||
target9.showInterface=false
|
||||
target9.type=ClassTarget
|
||||
target9.typeParameters=
|
||||
target9.width=80
|
||||
target9.x=190
|
||||
target9.y=130
|
||||
@@ -0,0 +1,5 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Location()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Location\n
|
||||
numComments=1
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Model a location in a city.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Location
|
||||
{
|
||||
/**
|
||||
* Constructor for objects of class Location
|
||||
*/
|
||||
public Location()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#BlueJ class context
|
||||
comment0.params=pickup\ destination
|
||||
comment0.target=Passenger(Location,\ Location)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Passenger\n\ @param\ pickup\ The\ pickup\ location,\ must\ not\ be\ null.\n\ @param\ destination\ The\ destination\ location,\ must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ either\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=Location\ getPickupLocation()
|
||||
comment1.text=\n\ @return\ The\ pickup\ location.\n
|
||||
comment2.params=
|
||||
comment2.target=Location\ getDestination()
|
||||
comment2.text=\n\ @return\ The\ destination\ location.\n
|
||||
numComments=3
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Model a passenger wishing to get from one
|
||||
* location to another.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Passenger
|
||||
{
|
||||
private Location pickup;
|
||||
private Location destination;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Passenger
|
||||
* @param pickup The pickup location, must not be null.
|
||||
* @param destination The destination location, must not be null.
|
||||
* @throws NullPointerException If either location is null.
|
||||
*/
|
||||
public Passenger(Location pickup, Location destination)
|
||||
{
|
||||
if(pickup == null) {
|
||||
throw new NullPointerException("Pickup location");
|
||||
}
|
||||
if(destination == null) {
|
||||
throw new NullPointerException("Destination location");
|
||||
}
|
||||
this.pickup = pickup;
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The pickup location.
|
||||
*/
|
||||
public Location getPickupLocation()
|
||||
{
|
||||
return pickup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The destination location.
|
||||
*/
|
||||
public Location getDestination()
|
||||
{
|
||||
return destination;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company
|
||||
comment0.target=PassengerSource(TaxiCompany)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ PassengerSource.\n\ @param\ company\ The\ company\ to\ be\ used.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ if\ company\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=boolean\ requestPickup()
|
||||
comment1.text=\n\ Have\ the\ source\ generate\ a\ new\ passenger\ and\n\ request\ a\ pickup\ from\ the\ company.\n\ @return\ true\ If\ the\ request\ succeeds,\ false\ otherwise.\n
|
||||
comment2.params=
|
||||
comment2.target=Passenger\ createPassenger()
|
||||
comment2.text=\n\ Create\ a\ new\ passenger.\n\ @return\ The\ created\ passenger.\n
|
||||
numComments=3
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Simulate passengers requesting rides from a taxi company.
|
||||
* Passengers should be generated at random intervals.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class PassengerSource
|
||||
{
|
||||
private TaxiCompany company;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class PassengerSource.
|
||||
* @param company The company to be used. Must not be null.
|
||||
* @throws NullPointerException if company is null.
|
||||
*/
|
||||
public PassengerSource(TaxiCompany company)
|
||||
{
|
||||
if(company == null) {
|
||||
throw new NullPointerException("company");
|
||||
}
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
/**
|
||||
* Have the source generate a new passenger and
|
||||
* request a pickup from the company.
|
||||
* @return true If the request succeeds, false otherwise.
|
||||
*/
|
||||
public boolean requestPickup()
|
||||
{
|
||||
Passenger passenger = createPassenger();
|
||||
return company.requestPickup(passenger);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new passenger.
|
||||
* @return The created passenger.
|
||||
*/
|
||||
private Passenger createPassenger()
|
||||
{
|
||||
return new Passenger(new Location(), new Location());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=PassengerSourceTest()
|
||||
comment0.text=\n\ Default\ constructor\ for\ test\ class\ PassengerSourceTest\n
|
||||
comment1.params=
|
||||
comment1.target=void\ setUp()
|
||||
comment1.text=\n\ Sets\ up\ the\ test\ fixture.\n\n\ Called\ before\ every\ test\ case\ method.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ tearDown()
|
||||
comment2.text=\n\ Tears\ down\ the\ test\ fixture.\n\n\ Called\ after\ every\ test\ case\ method.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ testPickup()
|
||||
comment3.text=\n\ Test\ for\ successful\ pickup\ of\ a\ passenger.\n
|
||||
numComments=4
|
||||
@@ -0,0 +1,55 @@
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* The test class PassengerSourceTest.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class PassengerSourceTest
|
||||
{
|
||||
private PassengerSource source;
|
||||
|
||||
/**
|
||||
* Default constructor for test class PassengerSourceTest
|
||||
*/
|
||||
public PassengerSourceTest()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the test fixture.
|
||||
*
|
||||
* Called before every test case method.
|
||||
*/
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
TaxiCompany company = new TaxiCompany();
|
||||
source = new PassengerSource(company);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the test fixture.
|
||||
*
|
||||
* Called after every test case method.
|
||||
*/
|
||||
@After
|
||||
public void tearDown()
|
||||
{
|
||||
source = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for successful pickup of a passenger.
|
||||
*/
|
||||
@Test
|
||||
public void testPickup()
|
||||
{
|
||||
assertEquals(true, source.requestPickup());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=PassengerTest()
|
||||
comment0.text=\n\ Default\ constructor\ for\ test\ class\ PassengerTest\n
|
||||
comment1.params=
|
||||
comment1.target=void\ setUp()
|
||||
comment1.text=\n\ Sets\ up\ the\ test\ fixture.\n\n\ Called\ before\ every\ test\ case\ method.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ tearDown()
|
||||
comment2.text=\n\ Tears\ down\ the\ test\ fixture.\n\n\ Called\ after\ every\ test\ case\ method.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ testCreation()
|
||||
comment3.text=\n\ Test\ basic\ creation\ of\ a\ passenger.\n\ Ensure\ that\ the\ pickup\ and\ destination\ locations\n\ have\ been\ set.\n
|
||||
numComments=4
|
||||
@@ -0,0 +1,57 @@
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* The test class PassengerTest.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class PassengerTest
|
||||
{
|
||||
/**
|
||||
* Default constructor for test class PassengerTest
|
||||
*/
|
||||
public PassengerTest()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the test fixture.
|
||||
*
|
||||
* Called before every test case method.
|
||||
*/
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the test fixture.
|
||||
*
|
||||
* Called after every test case method.
|
||||
*/
|
||||
@After
|
||||
public void tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Test basic creation of a passenger.
|
||||
* Ensure that the pickup and destination locations
|
||||
* have been set.
|
||||
*/
|
||||
@Test
|
||||
public void testCreation()
|
||||
{
|
||||
Location pickup = new Location();
|
||||
Location destination = new Location();
|
||||
Passenger passenger1 = new Passenger(pickup, destination);
|
||||
assertEquals(destination, passenger1.getDestination());
|
||||
assertEquals(pickup, passenger1.getPickupLocation());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
Project: taxi-company-outline-testing
|
||||
Authors: David J. Barnes and Michael Kölling
|
||||
|
||||
This project is part of the material for chapter 14 of the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
This project provides a partial implementation of a simulation of
|
||||
taxis operating on a city grid to pick up and transport passengers.
|
||||
|
||||
This is the second stage of this project. It illustrates some basic
|
||||
testing of the outline implementation.
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company\ location
|
||||
comment0.target=Shuttle(TaxiCompany,\ Location)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Shuttle\n\ @param\ company\ The\ taxi\ company.\ Must\ not\ be\ null.\n\ @param\ location\ The\ vehicle's\ starting\ point.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ company\ or\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=boolean\ isFree()
|
||||
comment1.text=\n\ Is\ the\ shuttle\ free?\n\ @return\ Whether\ or\ not\ this\ vehicle\ is\ free.\n
|
||||
comment2.params=location
|
||||
comment2.target=void\ setPickupLocation(Location)
|
||||
comment2.text=\n\ Receive\ a\ pickup\ location.\n\ @param\ location\ The\ pickup\ location.\n
|
||||
comment3.params=passenger
|
||||
comment3.target=void\ pickup(Passenger)
|
||||
comment3.text=\n\ Receive\ a\ passenger.\n\ Add\ their\ destination\ to\ the\ list.\n\ @param\ passenger\ The\ passenger\ being\ picked\ up.\n
|
||||
comment4.params=
|
||||
comment4.target=void\ chooseTargetLocation()
|
||||
comment4.text=\n\ Decide\ where\ to\ go\ next,\ based\ on\ the\ list\ of\n\ possible\ destinations.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ offloadPassenger()
|
||||
comment5.text=\n\ Offload\ a\ passenger\ whose\ destination\ is\ the\n\ current\ location.\n
|
||||
numComments=6
|
||||
@@ -0,0 +1,76 @@
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* A shuttle is able to carry multiple passengers.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Shuttle extends Vehicle
|
||||
{
|
||||
// The list of destinations for the shuttle.
|
||||
private List<Location> destinations;
|
||||
// The list of passengers on the shuttle.
|
||||
private List<Passenger> passengers;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Shuttle
|
||||
* @param company The taxi company. Must not be null.
|
||||
* @param location The vehicle's starting point. Must not be null.
|
||||
* @throws NullPointerException If company or location is null.
|
||||
*/
|
||||
public Shuttle(TaxiCompany company, Location location)
|
||||
{
|
||||
super(company, location);
|
||||
destinations = new LinkedList<>();
|
||||
passengers = new LinkedList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the shuttle free?
|
||||
* @return Whether or not this vehicle is free.
|
||||
*/
|
||||
public boolean isFree()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a pickup location.
|
||||
* @param location The pickup location.
|
||||
*/
|
||||
public void setPickupLocation(Location location)
|
||||
{
|
||||
destinations.add(location);
|
||||
chooseTargetLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a passenger.
|
||||
* Add their destination to the list.
|
||||
* @param passenger The passenger being picked up.
|
||||
*/
|
||||
public void pickup(Passenger passenger)
|
||||
{
|
||||
passengers.add(passenger);
|
||||
destinations.add(passenger.getDestination());
|
||||
chooseTargetLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide where to go next, based on the list of
|
||||
* possible destinations.
|
||||
*/
|
||||
private void chooseTargetLocation()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Offload a passenger whose destination is the
|
||||
* current location.
|
||||
*/
|
||||
public void offloadPassenger()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company\ location
|
||||
comment0.target=Taxi(TaxiCompany,\ Location)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Taxi\n\ @param\ company\ The\ taxi\ company.\ Must\ not\ be\ null.\n\ @param\ location\ The\ vehicle's\ starting\ point.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ company\ or\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=boolean\ isFree()
|
||||
comment1.text=\n\ Is\ the\ taxi\ free?\n\ @return\ Whether\ or\ not\ this\ taxi\ is\ free.\n
|
||||
comment2.params=location
|
||||
comment2.target=void\ setPickupLocation(Location)
|
||||
comment2.text=\n\ Receive\ a\ pickup\ location.\ This\ becomes\ the\n\ target\ location.\n\ @param\ location\ The\ pickup\ location.\n
|
||||
comment3.params=passenger
|
||||
comment3.target=void\ pickup(Passenger)
|
||||
comment3.text=\n\ Receive\ a\ passenger.\n\ Set\ their\ destination\ as\ the\ target\ location.\n\ @param\ passenger\ The\ passenger.\n
|
||||
comment4.params=
|
||||
comment4.target=void\ offloadPassenger()
|
||||
comment4.text=\n\ Offload\ the\ passenger.\n
|
||||
numComments=5
|
||||
@@ -0,0 +1,57 @@
|
||||
|
||||
/**
|
||||
* A taxi is able to carry a single passenger.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Taxi extends Vehicle
|
||||
{
|
||||
/**
|
||||
* Constructor for objects of class Taxi
|
||||
* @param company The taxi company. Must not be null.
|
||||
* @param location The vehicle's starting point. Must not be null.
|
||||
* @throws NullPointerException If company or location is null.
|
||||
*/
|
||||
public Taxi(TaxiCompany company, Location location)
|
||||
{
|
||||
super(company, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the taxi free?
|
||||
* @return Whether or not this taxi is free.
|
||||
*/
|
||||
public boolean isFree()
|
||||
{
|
||||
return getTargetLocation() == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a pickup location. This becomes the
|
||||
* target location.
|
||||
* @param location The pickup location.
|
||||
*/
|
||||
public void setPickupLocation(Location location)
|
||||
{
|
||||
setTargetLocation(location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a passenger.
|
||||
* Set their destination as the target location.
|
||||
* @param passenger The passenger.
|
||||
*/
|
||||
public void pickup(Passenger passenger)
|
||||
{
|
||||
setTargetLocation(passenger.getDestination());
|
||||
}
|
||||
|
||||
/**
|
||||
* Offload the passenger.
|
||||
*/
|
||||
public void offloadPassenger()
|
||||
{
|
||||
clearTargetLocation();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=TaxiCompany()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ TaxiCompany\n
|
||||
comment1.params=passenger
|
||||
comment1.target=boolean\ requestPickup(Passenger)
|
||||
comment1.text=\n\ Request\ a\ pickup\ for\ the\ given\ passenger.\n\ @param\ passenger\ The\ passenger\ requesting\ a\ pickup.\n\ @return\ Whether\ a\ free\ vehicle\ is\ available.\n
|
||||
comment2.params=vehicle
|
||||
comment2.target=void\ arrivedAtPickup(Vehicle)
|
||||
comment2.text=\n\ A\ vehicle\ has\ arrived\ at\ a\ pickup\ point.\n\ @param\ vehicle\ The\ vehicle\ at\ the\ pickup\ point.\n
|
||||
comment3.params=vehicle\ passenger
|
||||
comment3.target=void\ arrivedAtDestination(Vehicle,\ Passenger)
|
||||
comment3.text=\n\ A\ vehicle\ has\ arrived\ at\ a\ passenger's\ destination.\n\ @param\ vehicle\ The\ vehicle\ at\ the\ destination.\n\ @param\ passenger\ The\ passenger\ being\ dropped\ off.\n
|
||||
comment4.params=
|
||||
comment4.target=Vehicle\ scheduleVehicle()
|
||||
comment4.text=\n\ Find\ a\ free\ vehicle,\ if\ any.\n\ @return\ A\ free\ vehicle,\ or\ null\ if\ there\ is\ none.\n
|
||||
numComments=5
|
||||
@@ -0,0 +1,74 @@
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* Model the operation of a taxi company, operating different
|
||||
* types of vehicle.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class TaxiCompany
|
||||
{
|
||||
private List<Vehicle> vehicles;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class TaxiCompany
|
||||
*/
|
||||
public TaxiCompany()
|
||||
{
|
||||
vehicles = new LinkedList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a pickup for the given passenger.
|
||||
* @param passenger The passenger requesting a pickup.
|
||||
* @return Whether a free vehicle is available.
|
||||
*/
|
||||
public boolean requestPickup(Passenger passenger)
|
||||
{
|
||||
Vehicle vehicle = scheduleVehicle();
|
||||
if(vehicle != null) {
|
||||
vehicle.setPickupLocation(passenger.getPickupLocation());
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A vehicle has arrived at a pickup point.
|
||||
* @param vehicle The vehicle at the pickup point.
|
||||
*/
|
||||
public void arrivedAtPickup(Vehicle vehicle)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* A vehicle has arrived at a passenger's destination.
|
||||
* @param vehicle The vehicle at the destination.
|
||||
* @param passenger The passenger being dropped off.
|
||||
*/
|
||||
public void arrivedAtDestination(Vehicle vehicle,
|
||||
Passenger passenger)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a free vehicle, if any.
|
||||
* @return A free vehicle, or null if there is none.
|
||||
*/
|
||||
private Vehicle scheduleVehicle()
|
||||
{
|
||||
Iterator<Vehicle> it = vehicles.iterator();
|
||||
while(it.hasNext()) {
|
||||
Vehicle vehicle = it.next();
|
||||
if(vehicle.isFree()) {
|
||||
return vehicle;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=TaxiTest()
|
||||
comment0.text=\n\ Default\ constructor\ for\ test\ class\ TaxiTest\n
|
||||
comment1.params=
|
||||
comment1.target=void\ setUp()
|
||||
comment1.text=\n\ Create\ a\ taxi.\n\n\ Called\ before\ every\ test\ case\ method.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ tearDown()
|
||||
comment2.text=\n\ Tears\ down\ the\ test\ fixture.\n\n\ Called\ after\ every\ test\ case\ method.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ testCreation()
|
||||
comment3.text=\n\ Test\ creation\ and\ the\ initial\ state\ of\ a\ taxi.\n
|
||||
comment4.params=
|
||||
comment4.target=void\ testPickup()
|
||||
comment4.text=\n\ Test\ that\ a\ taxi\ is\ no\ longer\ free\ after\ it\ has\n\ picked\ up\ a\ passenger.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ testOffload()
|
||||
comment5.text=\n\ Test\ that\ a\ taxi\ becomes\ free\ again\ after\ offloading\n\ a\ passenger.\n
|
||||
numComments=6
|
||||
@@ -0,0 +1,81 @@
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* The test class TaxiTest.
|
||||
*
|
||||
* @author (your name)
|
||||
* @version (a version number or a date)
|
||||
*/
|
||||
public class TaxiTest
|
||||
{
|
||||
private Taxi taxi;
|
||||
|
||||
/**
|
||||
* Default constructor for test class TaxiTest
|
||||
*/
|
||||
public TaxiTest()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a taxi.
|
||||
*
|
||||
* Called before every test case method.
|
||||
*/
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
TaxiCompany company = new TaxiCompany();
|
||||
Location taxiLocation = new Location();
|
||||
taxi = new Taxi(company, taxiLocation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the test fixture.
|
||||
*
|
||||
* Called after every test case method.
|
||||
*/
|
||||
@After
|
||||
public void tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creation and the initial state of a taxi.
|
||||
*/
|
||||
@Test
|
||||
public void testCreation()
|
||||
{
|
||||
assertEquals(true, taxi.isFree());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a taxi is no longer free after it has
|
||||
* picked up a passenger.
|
||||
*/
|
||||
@Test
|
||||
public void testPickup()
|
||||
{
|
||||
Location pickup = new Location();
|
||||
Location destination = new Location();
|
||||
Passenger passenger = new Passenger(pickup, destination);
|
||||
taxi.pickup(passenger);
|
||||
assertEquals(false, taxi.isFree());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a taxi becomes free again after offloading
|
||||
* a passenger.
|
||||
*/
|
||||
@Test
|
||||
public void testOffload()
|
||||
{
|
||||
testPickup();
|
||||
taxi.offloadPassenger();
|
||||
assertEquals(true, taxi.isFree());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company\ location
|
||||
comment0.target=Vehicle(TaxiCompany,\ Location)
|
||||
comment0.text=\n\ Constructor\ of\ class\ Vehicle\n\ @param\ company\ The\ taxi\ company.\ Must\ not\ be\ null.\n\ @param\ location\ The\ vehicle's\ starting\ point.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ company\ or\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ notifyPickupArrival()
|
||||
comment1.text=\n\ Notify\ the\ company\ of\ our\ arrival\ at\ a\ pickup\ location.\n
|
||||
comment10.params=location
|
||||
comment10.target=void\ setTargetLocation(Location)
|
||||
comment10.text=\n\ Set\ the\ required\ target\ location.\n\ @param\ location\ Where\ to\ go.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ location\ is\ null.\n
|
||||
comment11.params=
|
||||
comment11.target=void\ clearTargetLocation()
|
||||
comment11.text=\n\ Clear\ the\ target\ location.\n
|
||||
comment2.params=passenger
|
||||
comment2.target=void\ notifyPassengerArrival(Passenger)
|
||||
comment2.text=\n\ Notify\ the\ company\ of\ our\ arrival\ at\ a\n\ passenger's\ destination.\n\ @param\ passenger\ The\ passenger\ who\ has\ arrived.\n
|
||||
comment3.params=location
|
||||
comment3.target=void\ setPickupLocation(Location)
|
||||
comment3.text=\n\ Receive\ a\ pickup\ location.\n\ How\ this\ is\ handled\ depends\ on\ the\ type\ of\ vehicle.\n\ @param\ location\ The\ pickup\ location.\n
|
||||
comment4.params=passenger
|
||||
comment4.target=void\ pickup(Passenger)
|
||||
comment4.text=\n\ Receive\ a\ passenger.\n\ How\ this\ is\ handled\ depends\ on\ the\ type\ of\ vehicle.\n\ @param\ passenger\ The\ passenger\ being\ picked\ up.\n
|
||||
comment5.params=
|
||||
comment5.target=boolean\ isFree()
|
||||
comment5.text=\n\ @return\ Whether\ or\ not\ this\ vehicle\ is\ free.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ offloadPassenger()
|
||||
comment6.text=\n\ Offload\ any\ passengers\ whose\ destination\ is\ the\n\ current\ location.\n
|
||||
comment7.params=
|
||||
comment7.target=Location\ getLocation()
|
||||
comment7.text=\n\ Get\ the\ location.\n\ @return\ Where\ this\ vehicle\ is\ currently\ located.\n
|
||||
comment8.params=location
|
||||
comment8.target=void\ setLocation(Location)
|
||||
comment8.text=\n\ Set\ the\ current\ location.\n\ @param\ location\ Where\ it\ is.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ location\ is\ null.\n
|
||||
comment9.params=
|
||||
comment9.target=Location\ getTargetLocation()
|
||||
comment9.text=\n\ Get\ the\ target\ location.\n\ @return\ Where\ this\ vehicle\ is\ currently\ headed,\ or\ null\n\ \ \ \ \ \ \ \ \ if\ it\ is\ idle.\n
|
||||
numComments=12
|
||||
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* Capture outline details of a vehicle.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public abstract class Vehicle
|
||||
{
|
||||
private TaxiCompany company;
|
||||
// Where the vehicle is.
|
||||
private Location location;
|
||||
// Where the vehicle is headed.
|
||||
private Location targetLocation;
|
||||
|
||||
/**
|
||||
* Constructor of class Vehicle
|
||||
* @param company The taxi company. Must not be null.
|
||||
* @param location The vehicle's starting point. Must not be null.
|
||||
* @throws NullPointerException If company or location is null.
|
||||
*/
|
||||
public Vehicle(TaxiCompany company, Location location)
|
||||
{
|
||||
if(company == null) {
|
||||
throw new NullPointerException("company");
|
||||
}
|
||||
if(location == null) {
|
||||
throw new NullPointerException("location");
|
||||
}
|
||||
this.company = company;
|
||||
this.location = location;
|
||||
targetLocation = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the company of our arrival at a pickup location.
|
||||
*/
|
||||
public void notifyPickupArrival()
|
||||
{
|
||||
company.arrivedAtPickup(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the company of our arrival at a
|
||||
* passenger's destination.
|
||||
* @param passenger The passenger who has arrived.
|
||||
*/
|
||||
public void notifyPassengerArrival(Passenger passenger)
|
||||
{
|
||||
company.arrivedAtDestination(this, passenger);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a pickup location.
|
||||
* How this is handled depends on the type of vehicle.
|
||||
* @param location The pickup location.
|
||||
*/
|
||||
public abstract void setPickupLocation(Location location);
|
||||
|
||||
/**
|
||||
* Receive a passenger.
|
||||
* How this is handled depends on the type of vehicle.
|
||||
* @param passenger The passenger being picked up.
|
||||
*/
|
||||
public abstract void pickup(Passenger passenger);
|
||||
|
||||
/**
|
||||
* @return Whether or not this vehicle is free.
|
||||
*/
|
||||
public abstract boolean isFree();
|
||||
|
||||
/**
|
||||
* Offload any passengers whose destination is the
|
||||
* current location.
|
||||
*/
|
||||
public abstract void offloadPassenger();
|
||||
|
||||
/**
|
||||
* Get the location.
|
||||
* @return Where this vehicle is currently located.
|
||||
*/
|
||||
public Location getLocation()
|
||||
{
|
||||
return location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current location.
|
||||
* @param location Where it is. Must not be null.
|
||||
* @throws NullPointerException If location is null.
|
||||
*/
|
||||
public void setLocation(Location location)
|
||||
{
|
||||
if(location != null) {
|
||||
this.location = location;
|
||||
}
|
||||
else {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the target location.
|
||||
* @return Where this vehicle is currently headed, or null
|
||||
* if it is idle.
|
||||
*/
|
||||
public Location getTargetLocation()
|
||||
{
|
||||
return targetLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the required target location.
|
||||
* @param location Where to go. Must not be null.
|
||||
* @throws NullPointerException If location is null.
|
||||
*/
|
||||
public void setTargetLocation(Location location)
|
||||
{
|
||||
if(location != null) {
|
||||
targetLocation = location;
|
||||
}
|
||||
else {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the target location.
|
||||
*/
|
||||
public void clearTargetLocation()
|
||||
{
|
||||
targetLocation = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=PassengerSourceTest
|
||||
dependency1.to=TaxiCompany
|
||||
dependency1.type=UsesDependency
|
||||
dependency10.from=TaxiTest
|
||||
dependency10.to=Passenger
|
||||
dependency10.type=UsesDependency
|
||||
dependency11.from=TaxiCompany
|
||||
dependency11.to=Passenger
|
||||
dependency11.type=UsesDependency
|
||||
dependency12.from=TaxiCompany
|
||||
dependency12.to=Vehicle
|
||||
dependency12.type=UsesDependency
|
||||
dependency13.from=Passenger
|
||||
dependency13.to=Location
|
||||
dependency13.type=UsesDependency
|
||||
dependency14.from=PassengerSource
|
||||
dependency14.to=TaxiCompany
|
||||
dependency14.type=UsesDependency
|
||||
dependency15.from=PassengerSource
|
||||
dependency15.to=Passenger
|
||||
dependency15.type=UsesDependency
|
||||
dependency16.from=PassengerSource
|
||||
dependency16.to=Location
|
||||
dependency16.type=UsesDependency
|
||||
dependency17.from=PassengerTest
|
||||
dependency17.to=Location
|
||||
dependency17.type=UsesDependency
|
||||
dependency18.from=Vehicle
|
||||
dependency18.to=TaxiCompany
|
||||
dependency18.type=UsesDependency
|
||||
dependency19.from=Vehicle
|
||||
dependency19.to=Location
|
||||
dependency19.type=UsesDependency
|
||||
dependency2.from=Taxi
|
||||
dependency2.to=TaxiCompany
|
||||
dependency2.type=UsesDependency
|
||||
dependency20.from=Vehicle
|
||||
dependency20.to=Passenger
|
||||
dependency20.type=UsesDependency
|
||||
dependency21.from=TaxiCompany
|
||||
dependency21.to=Taxi
|
||||
dependency21.type=UsesDependency
|
||||
dependency22.from=TaxiCompany
|
||||
dependency22.to=Location
|
||||
dependency22.type=UsesDependency
|
||||
dependency23.from=PassengerSourceTest
|
||||
dependency23.to=PassengerSource
|
||||
dependency23.type=UsesDependency
|
||||
dependency24.from=TaxiTest
|
||||
dependency24.to=Taxi
|
||||
dependency24.type=UsesDependency
|
||||
dependency25.from=PassengerTest
|
||||
dependency25.to=Passenger
|
||||
dependency25.type=UsesDependency
|
||||
dependency3.from=Taxi
|
||||
dependency3.to=Location
|
||||
dependency3.type=UsesDependency
|
||||
dependency4.from=Taxi
|
||||
dependency4.to=Passenger
|
||||
dependency4.type=UsesDependency
|
||||
dependency5.from=Shuttle
|
||||
dependency5.to=TaxiCompany
|
||||
dependency5.type=UsesDependency
|
||||
dependency6.from=Shuttle
|
||||
dependency6.to=Location
|
||||
dependency6.type=UsesDependency
|
||||
dependency7.from=Shuttle
|
||||
dependency7.to=Passenger
|
||||
dependency7.type=UsesDependency
|
||||
dependency8.from=TaxiTest
|
||||
dependency8.to=TaxiCompany
|
||||
dependency8.type=UsesDependency
|
||||
dependency9.from=TaxiTest
|
||||
dependency9.to=Location
|
||||
dependency9.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=806
|
||||
package.editor.height=548
|
||||
package.editor.width=698
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=25
|
||||
package.numTargets=10
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=571
|
||||
readme.editor.width=770
|
||||
readme.editor.x=53
|
||||
readme.editor.y=23
|
||||
target1.editor.height=700
|
||||
target1.editor.width=900
|
||||
target1.editor.x=53
|
||||
target1.editor.y=23
|
||||
target1.height=50
|
||||
target1.name=Vehicle
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=AbstractTarget
|
||||
target1.typeParameters=
|
||||
target1.width=80
|
||||
target1.x=490
|
||||
target1.y=100
|
||||
target10.editor.height=730
|
||||
target10.editor.width=748
|
||||
target10.editor.x=53
|
||||
target10.editor.y=60
|
||||
target10.height=50
|
||||
target10.name=Location
|
||||
target10.naviview.expanded=true
|
||||
target10.showInterface=false
|
||||
target10.type=ClassTarget
|
||||
target10.typeParameters=
|
||||
target10.width=80
|
||||
target10.x=110
|
||||
target10.y=430
|
||||
target2.editor.height=700
|
||||
target2.editor.width=900
|
||||
target2.editor.x=53
|
||||
target2.editor.y=23
|
||||
target2.height=50
|
||||
target2.name=TaxiCompany
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=100
|
||||
target2.x=190
|
||||
target2.y=30
|
||||
target3.editor.height=735
|
||||
target3.editor.width=784
|
||||
target3.editor.x=117
|
||||
target3.editor.y=75
|
||||
target3.height=50
|
||||
target3.name=TaxiTest
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=UnitTestTargetJunit4
|
||||
target3.typeParameters=
|
||||
target3.width=80
|
||||
target3.x=430
|
||||
target3.y=200
|
||||
target4.editor.height=720
|
||||
target4.editor.width=842
|
||||
target4.editor.x=53
|
||||
target4.editor.y=63
|
||||
target4.height=50
|
||||
target4.name=PassengerSourceTest
|
||||
target4.naviview.expanded=true
|
||||
target4.showInterface=false
|
||||
target4.type=UnitTestTargetJunit4
|
||||
target4.typeParameters=
|
||||
target4.width=150
|
||||
target4.x=50
|
||||
target4.y=100
|
||||
target5.association=TaxiTest
|
||||
target5.editor.height=700
|
||||
target5.editor.width=900
|
||||
target5.editor.x=53
|
||||
target5.editor.y=23
|
||||
target5.height=50
|
||||
target5.name=Taxi
|
||||
target5.naviview.expanded=true
|
||||
target5.showInterface=false
|
||||
target5.type=ClassTarget
|
||||
target5.typeParameters=
|
||||
target5.width=80
|
||||
target5.x=400
|
||||
target5.y=230
|
||||
target6.editor.height=726
|
||||
target6.editor.width=907
|
||||
target6.editor.x=158
|
||||
target6.editor.y=83
|
||||
target6.height=50
|
||||
target6.name=PassengerTest
|
||||
target6.naviview.expanded=true
|
||||
target6.showInterface=false
|
||||
target6.type=UnitTestTargetJunit4
|
||||
target6.typeParameters=
|
||||
target6.width=110
|
||||
target6.x=300
|
||||
target6.y=310
|
||||
target7.association=PassengerTest
|
||||
target7.editor.height=700
|
||||
target7.editor.width=900
|
||||
target7.editor.x=53
|
||||
target7.editor.y=23
|
||||
target7.height=50
|
||||
target7.name=Passenger
|
||||
target7.naviview.expanded=true
|
||||
target7.showInterface=false
|
||||
target7.type=ClassTarget
|
||||
target7.typeParameters=
|
||||
target7.width=90
|
||||
target7.x=270
|
||||
target7.y=340
|
||||
target8.association=PassengerSourceTest
|
||||
target8.editor.height=700
|
||||
target8.editor.width=900
|
||||
target8.editor.x=53
|
||||
target8.editor.y=23
|
||||
target8.height=50
|
||||
target8.name=PassengerSource
|
||||
target8.naviview.expanded=true
|
||||
target8.showInterface=false
|
||||
target8.type=ClassTarget
|
||||
target8.typeParameters=
|
||||
target8.width=130
|
||||
target8.x=20
|
||||
target8.y=130
|
||||
target9.editor.height=707
|
||||
target9.editor.width=811
|
||||
target9.editor.x=53
|
||||
target9.editor.y=60
|
||||
target9.height=50
|
||||
target9.name=Shuttle
|
||||
target9.naviview.expanded=false
|
||||
target9.showInterface=false
|
||||
target9.type=ClassTarget
|
||||
target9.typeParameters=
|
||||
target9.width=80
|
||||
target9.x=570
|
||||
target9.y=190
|
||||
@@ -0,0 +1,8 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Demo()
|
||||
comment0.text=\n\ Create\ some\ sample\ objects\ for\ the\ demo.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ pickupTest()
|
||||
comment1.text=\n\ Request\ a\ pickup\ for\ a\ new\ passenger.\n\ The\ request\ will\ fail\ in\ this\ version.\n
|
||||
numComments=2
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Set up a passenger-source and taxi-company, to illustrate
|
||||
* the scenario of requesting a pickup.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Demo
|
||||
{
|
||||
private PassengerSource source;
|
||||
private TaxiCompany company;
|
||||
|
||||
/**
|
||||
* Create some sample objects for the demo.
|
||||
*/
|
||||
public Demo()
|
||||
{
|
||||
company = new TaxiCompany();
|
||||
source = new PassengerSource(company);
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a pickup for a new passenger.
|
||||
* The request will fail in this version.
|
||||
*/
|
||||
public void pickupTest()
|
||||
{
|
||||
if(source.requestPickup()) {
|
||||
System.out.println("Pickup request succeeded.");
|
||||
}
|
||||
else {
|
||||
System.out.println("Pickup request failed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Location()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Location\n
|
||||
numComments=1
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Model a location in a city.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Location
|
||||
{
|
||||
/**
|
||||
* Constructor for objects of class Location
|
||||
*/
|
||||
public Location()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#BlueJ class context
|
||||
comment0.params=pickup\ destination
|
||||
comment0.target=Passenger(Location,\ Location)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Passenger\n\ @param\ pickup\ The\ pickup\ location,\ must\ not\ be\ null.\n\ @param\ destination\ The\ destination\ location,\ must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ either\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=Location\ getPickupLocation()
|
||||
comment1.text=\n\ @return\ The\ pickup\ location.\n
|
||||
comment2.params=
|
||||
comment2.target=Location\ getDestination()
|
||||
comment2.text=\n\ @return\ The\ destination\ location.\n
|
||||
numComments=3
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Model a passenger wishing to get from one
|
||||
* location to another.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Passenger
|
||||
{
|
||||
private Location pickup;
|
||||
private Location destination;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Passenger
|
||||
* @param pickup The pickup location, must not be null.
|
||||
* @param destination The destination location, must not be null.
|
||||
* @throws NullPointerException If either location is null.
|
||||
*/
|
||||
public Passenger(Location pickup, Location destination)
|
||||
{
|
||||
if(pickup == null) {
|
||||
throw new NullPointerException("Pickup location");
|
||||
}
|
||||
if(destination == null) {
|
||||
throw new NullPointerException("Destination location");
|
||||
}
|
||||
this.pickup = pickup;
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The pickup location.
|
||||
*/
|
||||
public Location getPickupLocation()
|
||||
{
|
||||
return pickup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The destination location.
|
||||
*/
|
||||
public Location getDestination()
|
||||
{
|
||||
return destination;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company
|
||||
comment0.target=PassengerSource(TaxiCompany)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ PassengerSource.\n\ @param\ company\ The\ company\ to\ be\ used.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ if\ company\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=boolean\ requestPickup()
|
||||
comment1.text=\n\ Have\ the\ source\ generate\ a\ new\ passenger\ and\n\ request\ a\ pickup\ from\ the\ company.\n\ @return\ true\ If\ the\ request\ succeeds,\ false\ otherwise.\n
|
||||
comment2.params=
|
||||
comment2.target=Passenger\ createPassenger()
|
||||
comment2.text=\n\ Create\ a\ new\ passenger.\n\ @return\ The\ created\ passenger.\n
|
||||
numComments=3
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Simulate passengers requesting rides from a taxi company.
|
||||
* Passengers should be generated at random intervals.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class PassengerSource
|
||||
{
|
||||
private TaxiCompany company;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class PassengerSource.
|
||||
* @param company The company to be used. Must not be null.
|
||||
* @throws NullPointerException if company is null.
|
||||
*/
|
||||
public PassengerSource(TaxiCompany company)
|
||||
{
|
||||
if(company == null) {
|
||||
throw new NullPointerException("company");
|
||||
}
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
/**
|
||||
* Have the source generate a new passenger and
|
||||
* request a pickup from the company.
|
||||
* @return true If the request succeeds, false otherwise.
|
||||
*/
|
||||
public boolean requestPickup()
|
||||
{
|
||||
Passenger passenger = createPassenger();
|
||||
return company.requestPickup(passenger);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new passenger.
|
||||
* @return The created passenger.
|
||||
*/
|
||||
private Passenger createPassenger()
|
||||
{
|
||||
return new Passenger(new Location(), new Location());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
Project: taxi-company-outline
|
||||
Authors: David J. Barnes and Michael Kölling
|
||||
|
||||
This project is part of the material for chapter 14 of the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
This project provides a partial implementation of a simulation of
|
||||
taxis operating on a city grid to pick up and transport passengers.
|
||||
|
||||
This is the first stage of this project. It illustrates an outline
|
||||
implementation form derived from CRC interaction.
|
||||
|
||||
How to start this project:
|
||||
Create a TaxiCompany and a PassengerSource. Call the requestPickup
|
||||
method of the source. However, as there are no vehicles created,
|
||||
no passengers will ever be picked up. (This functionality is also
|
||||
capture in the Demo class.)
|
||||
|
||||
Create at least one Taxi in the constructor of TaxiCompany and
|
||||
add it to the vehicles collection. Then a passenger will have
|
||||
a taxi scheduled to pick them up. However, as vehicles do not
|
||||
yet move, they never will be picked up.
|
||||
@@ -0,0 +1,20 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company\ location
|
||||
comment0.target=Shuttle(TaxiCompany,\ Location)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Shuttle\n\ @param\ company\ The\ taxi\ company.\ Must\ not\ be\ null.\n\ @param\ location\ The\ vehicle's\ starting\ point.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ company\ or\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=boolean\ isFree()
|
||||
comment1.text=\n\ Is\ the\ shuttle\ free?\n\ @return\ Whether\ or\ not\ this\ vehicle\ is\ free.\n
|
||||
comment2.params=location
|
||||
comment2.target=void\ setPickupLocation(Location)
|
||||
comment2.text=\n\ Receive\ a\ pickup\ location.\n\ @param\ location\ The\ pickup\ location.\n
|
||||
comment3.params=passenger
|
||||
comment3.target=void\ pickup(Passenger)
|
||||
comment3.text=\n\ Receive\ a\ passenger.\n\ Add\ their\ destination\ to\ the\ list.\n\ @param\ passenger\ The\ passenger\ being\ picked\ up.\n
|
||||
comment4.params=
|
||||
comment4.target=void\ chooseTargetLocation()
|
||||
comment4.text=\n\ Decide\ where\ to\ go\ next,\ based\ on\ the\ list\ of\n\ possible\ destinations.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ offloadPassenger()
|
||||
comment5.text=\n\ Offload\ a\ passenger\ whose\ destination\ is\ the\n\ current\ location.\n
|
||||
numComments=6
|
||||
@@ -0,0 +1,76 @@
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* A shuttle is able to carry multiple passengers.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Shuttle extends Vehicle
|
||||
{
|
||||
// The list of destinations for the shuttle.
|
||||
private List<Location> destinations;
|
||||
// The list of passengers on the shuttle.
|
||||
private List<Passenger> passengers;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Shuttle
|
||||
* @param company The taxi company. Must not be null.
|
||||
* @param location The vehicle's starting point. Must not be null.
|
||||
* @throws NullPointerException If company or location is null.
|
||||
*/
|
||||
public Shuttle(TaxiCompany company, Location location)
|
||||
{
|
||||
super(company, location);
|
||||
destinations = new LinkedList<>();
|
||||
passengers = new LinkedList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the shuttle free?
|
||||
* @return Whether or not this vehicle is free.
|
||||
*/
|
||||
public boolean isFree()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a pickup location.
|
||||
* @param location The pickup location.
|
||||
*/
|
||||
public void setPickupLocation(Location location)
|
||||
{
|
||||
destinations.add(location);
|
||||
chooseTargetLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a passenger.
|
||||
* Add their destination to the list.
|
||||
* @param passenger The passenger being picked up.
|
||||
*/
|
||||
public void pickup(Passenger passenger)
|
||||
{
|
||||
passengers.add(passenger);
|
||||
destinations.add(passenger.getDestination());
|
||||
chooseTargetLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide where to go next, based on the list of
|
||||
* possible destinations.
|
||||
*/
|
||||
private void chooseTargetLocation()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Offload a passenger whose destination is the
|
||||
* current location.
|
||||
*/
|
||||
public void offloadPassenger()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company\ location
|
||||
comment0.target=Taxi(TaxiCompany,\ Location)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Taxi\n\ @param\ company\ The\ taxi\ company.\ Must\ not\ be\ null.\n\ @param\ location\ The\ vehicle's\ starting\ point.\n\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ company\ or\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=boolean\ isFree()
|
||||
comment1.text=\n\ Is\ the\ taxi\ free?\n\ @return\ Whether\ or\ not\ this\ taxi\ is\ free.\n
|
||||
comment2.params=location
|
||||
comment2.target=void\ setPickupLocation(Location)
|
||||
comment2.text=\n\ Receive\ a\ pickup\ location.\ This\ becomes\ the\n\ target\ location.\n\ @param\ location\ The\ pickup\ location.\n
|
||||
comment3.params=passenger
|
||||
comment3.target=void\ pickup(Passenger)
|
||||
comment3.text=\n\ Receive\ a\ passenger.\n\ Set\ their\ destination\ as\ the\ target\ location.\n\ @param\ passenger\ The\ passenger.\n
|
||||
comment4.params=
|
||||
comment4.target=void\ offloadPassenger()
|
||||
comment4.text=\n\ Offload\ the\ passenger.\n
|
||||
numComments=5
|
||||
@@ -0,0 +1,58 @@
|
||||
|
||||
/**
|
||||
* A taxi is able to carry a single passenger.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Taxi extends Vehicle
|
||||
{
|
||||
/**
|
||||
* Constructor for objects of class Taxi
|
||||
* @param company The taxi company. Must not be null.
|
||||
* @param location The vehicle's starting point.
|
||||
* Must not be null.
|
||||
* @throws NullPointerException If company or location is null.
|
||||
*/
|
||||
public Taxi(TaxiCompany company, Location location)
|
||||
{
|
||||
super(company, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the taxi free?
|
||||
* @return Whether or not this taxi is free.
|
||||
*/
|
||||
public boolean isFree()
|
||||
{
|
||||
return getTargetLocation() == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a pickup location. This becomes the
|
||||
* target location.
|
||||
* @param location The pickup location.
|
||||
*/
|
||||
public void setPickupLocation(Location location)
|
||||
{
|
||||
setTargetLocation(location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a passenger.
|
||||
* Set their destination as the target location.
|
||||
* @param passenger The passenger.
|
||||
*/
|
||||
public void pickup(Passenger passenger)
|
||||
{
|
||||
setTargetLocation(passenger.getDestination());
|
||||
}
|
||||
|
||||
/**
|
||||
* Offload the passenger.
|
||||
*/
|
||||
public void offloadPassenger()
|
||||
{
|
||||
clearTargetLocation();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=TaxiCompany()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ TaxiCompany\n
|
||||
comment1.params=passenger
|
||||
comment1.target=boolean\ requestPickup(Passenger)
|
||||
comment1.text=\n\ Request\ a\ pickup\ for\ the\ given\ passenger.\n\ @param\ passenger\ The\ passenger\ requesting\ a\ pickup.\n\ @return\ Whether\ a\ free\ vehicle\ is\ available.\n
|
||||
comment2.params=vehicle
|
||||
comment2.target=void\ arrivedAtPickup(Vehicle)
|
||||
comment2.text=\n\ A\ vehicle\ has\ arrived\ at\ a\ pickup\ point.\n\ @param\ vehicle\ The\ vehicle\ at\ the\ pickup\ point.\n
|
||||
comment3.params=vehicle\ passenger
|
||||
comment3.target=void\ arrivedAtDestination(Vehicle,\ Passenger)
|
||||
comment3.text=\n\ A\ vehicle\ has\ arrived\ at\ a\ passenger's\ destination.\n\ @param\ vehicle\ The\ vehicle\ at\ the\ destination.\n\ @param\ passenger\ The\ passenger\ being\ dropped\ off.\n
|
||||
comment4.params=
|
||||
comment4.target=Vehicle\ scheduleVehicle()
|
||||
comment4.text=\n\ Find\ a\ free\ vehicle,\ if\ any.\n\ @return\ A\ free\ vehicle,\ or\ null\ if\ there\ is\ none.\n
|
||||
numComments=5
|
||||
@@ -0,0 +1,74 @@
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* Model the operation of a taxi company, operating different
|
||||
* types of vehicle.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class TaxiCompany
|
||||
{
|
||||
private List<Vehicle> vehicles;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class TaxiCompany
|
||||
*/
|
||||
public TaxiCompany()
|
||||
{
|
||||
vehicles = new LinkedList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a pickup for the given passenger.
|
||||
* @param passenger The passenger requesting a pickup.
|
||||
* @return Whether a free vehicle is available.
|
||||
*/
|
||||
public boolean requestPickup(Passenger passenger)
|
||||
{
|
||||
Vehicle vehicle = scheduleVehicle();
|
||||
if(vehicle != null) {
|
||||
vehicle.setPickupLocation(passenger.getPickupLocation());
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A vehicle has arrived at a pickup point.
|
||||
* @param vehicle The vehicle at the pickup point.
|
||||
*/
|
||||
public void arrivedAtPickup(Vehicle vehicle)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* A vehicle has arrived at a passenger's destination.
|
||||
* @param vehicle The vehicle at the destination.
|
||||
* @param passenger The passenger being dropped off.
|
||||
*/
|
||||
public void arrivedAtDestination(Vehicle vehicle,
|
||||
Passenger passenger)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a free vehicle, if any.
|
||||
* @return A free vehicle, or null if there is none.
|
||||
*/
|
||||
private Vehicle scheduleVehicle()
|
||||
{
|
||||
Iterator<Vehicle> it = vehicles.iterator();
|
||||
while(it.hasNext()) {
|
||||
Vehicle vehicle = it.next();
|
||||
if(vehicle.isFree()) {
|
||||
return vehicle;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company\ location
|
||||
comment0.target=Vehicle(TaxiCompany,\ Location)
|
||||
comment0.text=\n\ Constructor\ of\ class\ Vehicle\n\ @param\ company\ The\ taxi\ company.\ Must\ not\ be\ null.\n\ @param\ location\ The\ vehicle's\ starting\ point.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ company\ or\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ notifyPickupArrival()
|
||||
comment1.text=\n\ Notify\ the\ company\ of\ our\ arrival\ at\ a\ pickup\ location.\n
|
||||
comment10.params=location
|
||||
comment10.target=void\ setTargetLocation(Location)
|
||||
comment10.text=\n\ Set\ the\ required\ target\ location.\n\ @param\ location\ Where\ to\ go.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ location\ is\ null.\n
|
||||
comment11.params=
|
||||
comment11.target=void\ clearTargetLocation()
|
||||
comment11.text=\n\ Clear\ the\ target\ location.\n
|
||||
comment2.params=passenger
|
||||
comment2.target=void\ notifyPassengerArrival(Passenger)
|
||||
comment2.text=\n\ Notify\ the\ company\ of\ our\ arrival\ at\ a\n\ passenger's\ destination.\n\ @param\ passenger\ The\ passenger\ who\ has\ arrived.\n
|
||||
comment3.params=location
|
||||
comment3.target=void\ setPickupLocation(Location)
|
||||
comment3.text=\n\ Receive\ a\ pickup\ location.\n\ How\ this\ is\ handled\ depends\ on\ the\ type\ of\ vehicle.\n\ @param\ location\ The\ pickup\ location.\n
|
||||
comment4.params=passenger
|
||||
comment4.target=void\ pickup(Passenger)
|
||||
comment4.text=\n\ Receive\ a\ passenger.\n\ How\ this\ is\ handled\ depends\ on\ the\ type\ of\ vehicle.\n\ @param\ passenger\ The\ passenger\ being\ picked\ up.\n
|
||||
comment5.params=
|
||||
comment5.target=boolean\ isFree()
|
||||
comment5.text=\n\ Is\ the\ vehicle\ free?\n\ @return\ Whether\ or\ not\ this\ vehicle\ is\ free.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ offloadPassenger()
|
||||
comment6.text=\n\ Offload\ any\ passengers\ whose\ destination\ is\ the\n\ current\ location.\n
|
||||
comment7.params=
|
||||
comment7.target=Location\ getLocation()
|
||||
comment7.text=\n\ Get\ the\ location.\n\ @return\ Where\ this\ vehicle\ is\ currently\ located.\n
|
||||
comment8.params=location
|
||||
comment8.target=void\ setLocation(Location)
|
||||
comment8.text=\n\ Set\ the\ current\ location.\n\ @param\ location\ Where\ it\ is.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ location\ is\ null.\n
|
||||
comment9.params=
|
||||
comment9.target=Location\ getTargetLocation()
|
||||
comment9.text=\n\ Get\ the\ target\ location.\n\ @return\ Where\ this\ vehicle\ is\ currently\ headed,\ or\ null\n\ \ \ \ \ \ \ \ \ if\ it\ is\ idle.\n
|
||||
numComments=12
|
||||
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* Capture outline details of a vehicle.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public abstract class Vehicle
|
||||
{
|
||||
private TaxiCompany company;
|
||||
// Where the vehicle is.
|
||||
private Location location;
|
||||
// Where the vehicle is headed.
|
||||
private Location targetLocation;
|
||||
|
||||
/**
|
||||
* Constructor of class Vehicle
|
||||
* @param company The taxi company. Must not be null.
|
||||
* @param location The vehicle's starting point. Must not be null.
|
||||
* @throws NullPointerException If company or location is null.
|
||||
*/
|
||||
public Vehicle(TaxiCompany company, Location location)
|
||||
{
|
||||
if(company == null) {
|
||||
throw new NullPointerException("company");
|
||||
}
|
||||
if(location == null) {
|
||||
throw new NullPointerException("location");
|
||||
}
|
||||
this.company = company;
|
||||
this.location = location;
|
||||
targetLocation = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the company of our arrival at a pickup location.
|
||||
*/
|
||||
public void notifyPickupArrival()
|
||||
{
|
||||
company.arrivedAtPickup(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the company of our arrival at a
|
||||
* passenger's destination.
|
||||
* @param passenger The passenger who has arrived.
|
||||
*/
|
||||
public void notifyPassengerArrival(Passenger passenger)
|
||||
{
|
||||
company.arrivedAtDestination(this, passenger);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a pickup location.
|
||||
* How this is handled depends on the type of vehicle.
|
||||
* @param location The pickup location.
|
||||
*/
|
||||
public abstract void setPickupLocation(Location location);
|
||||
|
||||
/**
|
||||
* Receive a passenger.
|
||||
* How this is handled depends on the type of vehicle.
|
||||
* @param passenger The passenger being picked up.
|
||||
*/
|
||||
public abstract void pickup(Passenger passenger);
|
||||
|
||||
/**
|
||||
* Is the vehicle free?
|
||||
* @return Whether or not this vehicle is free.
|
||||
*/
|
||||
public abstract boolean isFree();
|
||||
|
||||
/**
|
||||
* Offload any passengers whose destination is the
|
||||
* current location.
|
||||
*/
|
||||
public abstract void offloadPassenger();
|
||||
|
||||
/**
|
||||
* Get the location.
|
||||
* @return Where this vehicle is currently located.
|
||||
*/
|
||||
public Location getLocation()
|
||||
{
|
||||
return location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current location.
|
||||
* @param location Where it is. Must not be null.
|
||||
* @throws NullPointerException If location is null.
|
||||
*/
|
||||
public void setLocation(Location location)
|
||||
{
|
||||
if(location != null) {
|
||||
this.location = location;
|
||||
}
|
||||
else {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the target location.
|
||||
* @return Where this vehicle is currently headed, or null
|
||||
* if it is idle.
|
||||
*/
|
||||
public Location getTargetLocation()
|
||||
{
|
||||
return targetLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the required target location.
|
||||
* @param location Where to go. Must not be null.
|
||||
* @throws NullPointerException If location is null.
|
||||
*/
|
||||
public void setTargetLocation(Location location)
|
||||
{
|
||||
if(location != null) {
|
||||
targetLocation = location;
|
||||
}
|
||||
else {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the target location.
|
||||
*/
|
||||
public void clearTargetLocation()
|
||||
{
|
||||
targetLocation = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=Taxi
|
||||
dependency1.to=TaxiCompany
|
||||
dependency1.type=UsesDependency
|
||||
dependency10.from=TaxiCompany
|
||||
dependency10.to=Vehicle
|
||||
dependency10.type=UsesDependency
|
||||
dependency11.from=Passenger
|
||||
dependency11.to=Location
|
||||
dependency11.type=UsesDependency
|
||||
dependency12.from=PassengerSource
|
||||
dependency12.to=TaxiCompany
|
||||
dependency12.type=UsesDependency
|
||||
dependency13.from=PassengerSource
|
||||
dependency13.to=Passenger
|
||||
dependency13.type=UsesDependency
|
||||
dependency14.from=PassengerSource
|
||||
dependency14.to=Location
|
||||
dependency14.type=UsesDependency
|
||||
dependency15.from=Vehicle
|
||||
dependency15.to=TaxiCompany
|
||||
dependency15.type=UsesDependency
|
||||
dependency16.from=Vehicle
|
||||
dependency16.to=Location
|
||||
dependency16.type=UsesDependency
|
||||
dependency17.from=Vehicle
|
||||
dependency17.to=Passenger
|
||||
dependency17.type=UsesDependency
|
||||
dependency18.from=TaxiCompany
|
||||
dependency18.to=Taxi
|
||||
dependency18.type=UsesDependency
|
||||
dependency19.from=TaxiCompany
|
||||
dependency19.to=Location
|
||||
dependency19.type=UsesDependency
|
||||
dependency2.from=Taxi
|
||||
dependency2.to=Location
|
||||
dependency2.type=UsesDependency
|
||||
dependency3.from=Taxi
|
||||
dependency3.to=Passenger
|
||||
dependency3.type=UsesDependency
|
||||
dependency4.from=Shuttle
|
||||
dependency4.to=TaxiCompany
|
||||
dependency4.type=UsesDependency
|
||||
dependency5.from=Shuttle
|
||||
dependency5.to=Location
|
||||
dependency5.type=UsesDependency
|
||||
dependency6.from=Shuttle
|
||||
dependency6.to=Passenger
|
||||
dependency6.type=UsesDependency
|
||||
dependency7.from=Demo
|
||||
dependency7.to=PassengerSource
|
||||
dependency7.type=UsesDependency
|
||||
dependency8.from=Demo
|
||||
dependency8.to=TaxiCompany
|
||||
dependency8.type=UsesDependency
|
||||
dependency9.from=TaxiCompany
|
||||
dependency9.to=Passenger
|
||||
dependency9.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=855
|
||||
package.editor.height=513
|
||||
package.editor.width=747
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=19
|
||||
package.numTargets=8
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=587
|
||||
readme.editor.width=825
|
||||
readme.editor.x=53
|
||||
readme.editor.y=23
|
||||
target1.editor.height=739
|
||||
target1.editor.width=851
|
||||
target1.editor.x=53
|
||||
target1.editor.y=60
|
||||
target1.height=50
|
||||
target1.name=Vehicle
|
||||
target1.naviview.expanded=false
|
||||
target1.showInterface=false
|
||||
target1.type=AbstractTarget
|
||||
target1.typeParameters=
|
||||
target1.width=80
|
||||
target1.x=510
|
||||
target1.y=110
|
||||
target2.editor.height=738
|
||||
target2.editor.width=834
|
||||
target2.editor.x=53
|
||||
target2.editor.y=60
|
||||
target2.height=50
|
||||
target2.name=TaxiCompany
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=100
|
||||
target2.x=270
|
||||
target2.y=60
|
||||
target3.editor.height=730
|
||||
target3.editor.width=792
|
||||
target3.editor.x=53
|
||||
target3.editor.y=60
|
||||
target3.height=50
|
||||
target3.name=Demo
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=80
|
||||
target3.x=50
|
||||
target3.y=130
|
||||
target4.editor.height=810
|
||||
target4.editor.width=976
|
||||
target4.editor.x=53
|
||||
target4.editor.y=23
|
||||
target4.height=50
|
||||
target4.name=Taxi
|
||||
target4.naviview.expanded=false
|
||||
target4.showInterface=false
|
||||
target4.type=ClassTarget
|
||||
target4.typeParameters=
|
||||
target4.width=80
|
||||
target4.x=440
|
||||
target4.y=210
|
||||
target5.editor.height=737
|
||||
target5.editor.width=839
|
||||
target5.editor.x=53
|
||||
target5.editor.y=60
|
||||
target5.height=50
|
||||
target5.name=Passenger
|
||||
target5.naviview.expanded=true
|
||||
target5.showInterface=false
|
||||
target5.type=ClassTarget
|
||||
target5.typeParameters=
|
||||
target5.width=90
|
||||
target5.x=360
|
||||
target5.y=310
|
||||
target6.editor.height=737
|
||||
target6.editor.width=837
|
||||
target6.editor.x=53
|
||||
target6.editor.y=60
|
||||
target6.height=50
|
||||
target6.name=PassengerSource
|
||||
target6.naviview.expanded=true
|
||||
target6.showInterface=false
|
||||
target6.type=ClassTarget
|
||||
target6.typeParameters=
|
||||
target6.width=130
|
||||
target6.x=120
|
||||
target6.y=230
|
||||
target7.editor.height=733
|
||||
target7.editor.width=838
|
||||
target7.editor.x=53
|
||||
target7.editor.y=60
|
||||
target7.height=50
|
||||
target7.name=Shuttle
|
||||
target7.naviview.expanded=false
|
||||
target7.showInterface=false
|
||||
target7.type=ClassTarget
|
||||
target7.typeParameters=
|
||||
target7.width=80
|
||||
target7.x=570
|
||||
target7.y=210
|
||||
target8.editor.height=737
|
||||
target8.editor.width=794
|
||||
target8.editor.x=53
|
||||
target8.editor.y=60
|
||||
target8.height=50
|
||||
target8.name=Location
|
||||
target8.naviview.expanded=true
|
||||
target8.showInterface=false
|
||||
target8.type=ClassTarget
|
||||
target8.typeParameters=
|
||||
target8.width=80
|
||||
target8.x=210
|
||||
target8.y=380
|
||||
@@ -0,0 +1,5 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=void\ act()
|
||||
comment0.text=\n\ Implement\ the\ actor's\ behavior.\n
|
||||
numComments=1
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* An actor in the taxi-company simulation.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public interface Actor
|
||||
{
|
||||
/**
|
||||
* Implement the actor's behavior.
|
||||
*/
|
||||
public void act();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Demo()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Demo\n
|
||||
comment1.params=
|
||||
comment1.target=void\ run()
|
||||
comment1.text=\n\ Run\ the\ demo\ for\ a\ fixed\ number\ of\ steps.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ step()
|
||||
comment2.text=\n\ Run\ the\ demo\ for\ one\ step\ by\ requesting\n\ all\ actors\ to\ act.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ reset()
|
||||
comment3.text=\n\ Reset\ the\ demo\ to\ a\ starting\ point.\n\ A\ single\ taxi\ is\ created,\ and\ a\ pickup\ is\n\ requested\ for\ a\ single\ passenger.\n\ @throws\ IllegalStateException\ If\ a\ pickup\ cannot\ be\ found\n
|
||||
numComments=4
|
||||
@@ -0,0 +1,69 @@
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* Provide a simple demonstration of running a stage-one
|
||||
* scenario. A single passenger is created, and a pickup
|
||||
* requested. As the simulation is run, the passenger
|
||||
* should be picked up and then taken to their destination.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Demo
|
||||
{
|
||||
private List<Actor> actors;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Demo
|
||||
*/
|
||||
public Demo()
|
||||
{
|
||||
actors = new LinkedList<>();
|
||||
reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the demo for a fixed number of steps.
|
||||
*/
|
||||
public void run()
|
||||
{
|
||||
for(int step = 0; step < 100; step++) {
|
||||
step();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the demo for one step by requesting
|
||||
* all actors to act.
|
||||
*/
|
||||
public void step()
|
||||
{
|
||||
for(Actor actor : actors) {
|
||||
actor.act();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the demo to a starting point.
|
||||
* A single taxi is created, and a pickup is
|
||||
* requested for a single passenger.
|
||||
* @throws IllegalStateException If a pickup cannot be found
|
||||
*/
|
||||
public void reset()
|
||||
{
|
||||
actors.clear();
|
||||
TaxiCompany company = new TaxiCompany();
|
||||
Taxi taxi = new Taxi(company, new Location(10, 10));
|
||||
List<Vehicle> vehicles = company.getVehicles();
|
||||
vehicles.add(taxi);
|
||||
actors.addAll(vehicles);
|
||||
|
||||
Passenger passenger = new Passenger(new Location(0, 0),
|
||||
new Location(10, 20));
|
||||
if(!company.requestPickup(passenger)) {
|
||||
throw new IllegalStateException("Failed to find a pickup.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#BlueJ class context
|
||||
comment0.params=x\ y
|
||||
comment0.target=Location(int,\ int)
|
||||
comment0.text=\n\ Model\ a\ location\ in\ the\ city.\n\ @param\ x\ The\ x\ coordinate.\ Must\ be\ positive.\n\ @param\ y\ The\ y\ coordinate.\ Must\ be\ positive.\n\ @throws\ IllegalArgumentException\ If\ a\ coordinate\ is\ negative.\n
|
||||
comment1.params=destination
|
||||
comment1.target=Location\ nextLocation(Location)
|
||||
comment1.text=\n\ Generate\ the\ next\ location\ to\ visit\ in\ order\ to\n\ reach\ the\ destination.\n\ @param\ destination\ Where\ we\ want\ to\ get\ to.\n\ @return\ A\ location\ in\ a\ direct\ line\ from\ this\ to\n\ \ \ \ \ \ \ \ \ destination.\n
|
||||
comment2.params=destination
|
||||
comment2.target=int\ distance(Location)
|
||||
comment2.text=\n\ Determine\ the\ number\ of\ movements\ required\ to\ get\n\ from\ here\ to\ the\ destination.\n\ @param\ destination\ The\ required\ destination.\n\ @return\ The\ number\ of\ movement\ steps.\n
|
||||
comment3.params=other
|
||||
comment3.target=boolean\ equals(java.lang.Object)
|
||||
comment3.text=\n\ Implement\ content\ equality\ for\ locations.\n\ @return\ true\ if\ this\ location\ matches\ the\ other,\n\ \ \ \ \ \ \ \ \ false\ otherwise.\n
|
||||
comment4.params=
|
||||
comment4.target=java.lang.String\ toString()
|
||||
comment4.text=\n\ @return\ A\ representation\ of\ the\ location.\n
|
||||
comment5.params=
|
||||
comment5.target=int\ hashCode()
|
||||
comment5.text=\n\ Use\ the\ top\ 16\ bits\ for\ the\ y\ value\ and\ the\ bottom\ for\ the\ x.\n\ Except\ for\ very\ big\ grids,\ this\ should\ give\ a\ unique\ hash\ code\n\ for\ each\ (x,\ y)\ pair.\n\ @return\ A\ hashcode\ for\ the\ location.\n
|
||||
comment6.params=
|
||||
comment6.target=int\ getX()
|
||||
comment6.text=\n\ @return\ The\ x\ coordinate.\n
|
||||
comment7.params=
|
||||
comment7.target=int\ getY()
|
||||
comment7.text=\n\ @return\ The\ y\ coordinate.\n
|
||||
numComments=8
|
||||
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* Model a location in a city.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Location
|
||||
{
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
/**
|
||||
* Model a location in the city.
|
||||
* @param x The x coordinate. Must be positive.
|
||||
* @param y The y coordinate. Must be positive.
|
||||
* @throws IllegalArgumentException If a coordinate is negative.
|
||||
*/
|
||||
public Location(int x, int y)
|
||||
{
|
||||
if(x < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Negative x-coordinate: " + x);
|
||||
}
|
||||
if(y < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Negative y-coordinate: " + y);
|
||||
}
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the next location to visit in order to
|
||||
* reach the destination.
|
||||
* @param destination Where we want to get to.
|
||||
* @return A location in a direct line from this to
|
||||
* destination.
|
||||
*/
|
||||
public Location nextLocation(Location destination)
|
||||
{
|
||||
int destX = destination.getX();
|
||||
int destY = destination.getY();
|
||||
int offsetX = x < destX ? 1 : x > destX ? -1 : 0;
|
||||
int offsetY = y < destY ? 1 : y > destY ? -1 : 0;
|
||||
if(offsetX != 0 || offsetY != 0) {
|
||||
return new Location(x + offsetX, y + offsetY);
|
||||
}
|
||||
else {
|
||||
return destination;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the number of movements required to get
|
||||
* from here to the destination.
|
||||
* @param destination The required destination.
|
||||
* @return The number of movement steps.
|
||||
*/
|
||||
public int distance(Location destination)
|
||||
{
|
||||
int xDist = Math.abs(destination.getX() - x);
|
||||
int yDist = Math.abs(destination.getY() - y);
|
||||
return Math.max(xDist, yDist);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement content equality for locations.
|
||||
* @return true if this location matches the other,
|
||||
* false otherwise.
|
||||
*/
|
||||
public boolean equals(Object other)
|
||||
{
|
||||
if(other instanceof Location) {
|
||||
Location otherLocation = (Location) other;
|
||||
return x == otherLocation.getX() &&
|
||||
y == otherLocation.getY();
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A representation of the location.
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return "location " + x + "," + y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the top 16 bits for the y value and the bottom for the x.
|
||||
* Except for very big grids, this should give a unique hash code
|
||||
* for each (x, y) pair.
|
||||
* @return A hashcode for the location.
|
||||
*/
|
||||
public int hashCode()
|
||||
{
|
||||
return (y << 16) + x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The x coordinate.
|
||||
*/
|
||||
public int getX()
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The y coordinate.
|
||||
*/
|
||||
public int getY()
|
||||
{
|
||||
return y;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=LocationTest()
|
||||
comment0.text=\n\ Default\ constructor\ for\ test\ class\ LocationTest\n
|
||||
comment1.params=
|
||||
comment1.target=void\ setUp()
|
||||
comment1.text=\n\ Sets\ up\ the\ test\ fixture.\n\n\ Called\ before\ every\ test\ case\ method.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ tearDown()
|
||||
comment2.text=\n\ Tears\ down\ the\ test\ fixture.\n\n\ Called\ after\ every\ test\ case\ method.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ testDistance()
|
||||
comment3.text=\n\ Test\ the\ distance\ method\ of\ the\ Location\ class.\n
|
||||
comment4.params=
|
||||
comment4.target=void\ testAdjacentLocations()
|
||||
comment4.text=\n\ Run\ tests\ of\ the\ nextLocation\ method\ of\ the\ Location\ class.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ testNonAdjacentLocations()
|
||||
numComments=6
|
||||
@@ -0,0 +1,120 @@
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test implementation of the Location class.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class LocationTest
|
||||
{
|
||||
/**
|
||||
* Default constructor for test class LocationTest
|
||||
*/
|
||||
public LocationTest()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the test fixture.
|
||||
*
|
||||
* Called before every test case method.
|
||||
*/
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the test fixture.
|
||||
*
|
||||
* Called after every test case method.
|
||||
*/
|
||||
@After
|
||||
public void tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the distance method of the Location class.
|
||||
*/
|
||||
@Test
|
||||
public void testDistance()
|
||||
{
|
||||
boolean ok = true;
|
||||
int startX = 10, startY = 10;
|
||||
Location startLocation = new Location(startX, startY);
|
||||
|
||||
// Calculate the distance from startLocation to other
|
||||
// locations around it. The distance should always
|
||||
// be equal to offset.
|
||||
int offset = 5;
|
||||
assertEquals(startLocation.distance(
|
||||
new Location(startX, startY + offset)), offset);
|
||||
assertEquals(startLocation.distance(
|
||||
new Location(startX + offset, startY)), offset);
|
||||
assertEquals(startLocation.distance(
|
||||
new Location(startX + 1, startY + offset)), offset);
|
||||
assertEquals(startLocation.distance(
|
||||
new Location(startX + offset, startY + 1)), offset);
|
||||
assertEquals(startLocation.distance(
|
||||
new Location(startX + offset, startY + offset)), offset);
|
||||
assertEquals(startLocation.distance(
|
||||
new Location(startX + offset - 1, startY + offset)), offset);
|
||||
assertEquals(startLocation.distance(
|
||||
new Location(startX + offset, startY + offset - 1)), offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run tests of the nextLocation method of the Location class.
|
||||
*/
|
||||
@Test
|
||||
public void testAdjacentLocations()
|
||||
{
|
||||
int startX = 10, startY = 10;
|
||||
Location startLocation = new Location(startX, startY);
|
||||
|
||||
// Test immediate adjacency.
|
||||
// (x, y) offsets for each direction from (startX, startY).
|
||||
int[][] offsets = {
|
||||
{ 0, 1, 0, 1, -1, 0, -1, 1, -1},
|
||||
{ 0, 0, 1, 1, 0, -1, -1, -1, 1},
|
||||
};
|
||||
|
||||
for(int i = 0; i < offsets[0].length; i++) {
|
||||
Location destination = new Location(startX + offsets[0][i],
|
||||
startY + offsets[1][i]);
|
||||
Location nextLocation = startLocation.nextLocation(destination);
|
||||
assertEquals(nextLocation.equals(destination), true);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonAdjacentLocations()
|
||||
{
|
||||
int startX = 10, startY = 10;
|
||||
Location startLocation = new Location(startX, startY);
|
||||
// (x, y) offsets for each direction from (startX, startY).
|
||||
int[][] offsets = {
|
||||
{ 0, 1, 0, 1, -1, 0, -1, 1, -1},
|
||||
{ 0, 0, 1, 1, 0, -1, -1, -1, 1},
|
||||
};
|
||||
// Test with destination that are not adjacent.
|
||||
// Use different values for xDist and yDist for more
|
||||
// varied tests.
|
||||
int xDist = 7;
|
||||
int yDist = 3;
|
||||
for(int i = 0; i < offsets[0].length; i++) {
|
||||
Location destination = new Location(startX + xDist * offsets[0][i],
|
||||
startY + yDist * offsets[1][i]);
|
||||
Location expectedNextLocation =
|
||||
new Location(startX + offsets[0][i],
|
||||
startY + offsets[1][i]);
|
||||
Location nextLocation = startLocation.nextLocation(destination);
|
||||
assertEquals(expectedNextLocation.equals(nextLocation), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#BlueJ class context
|
||||
comment0.params=vehicle
|
||||
comment0.target=MissingPassengerException(Vehicle)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ MissingPassengerException.\n\ @param\ vehicle\ The\ vehicle\ expecting\ a\ passenger.\n
|
||||
comment1.params=
|
||||
comment1.target=Vehicle\ getVehicle()
|
||||
comment1.text=\n\ @return\ The\ vehicle\ for\ which\ there\ was\ no\ passenger.\n
|
||||
numComments=2
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
/**
|
||||
* Indicate that there was no passenger at a pickup point.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class MissingPassengerException extends RuntimeException
|
||||
{
|
||||
private Vehicle vehicle;
|
||||
/**
|
||||
* Constructor for objects of class MissingPassengerException.
|
||||
* @param vehicle The vehicle expecting a passenger.
|
||||
*/
|
||||
public MissingPassengerException(Vehicle vehicle)
|
||||
{
|
||||
super("Missing passenger at pickup location.");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The vehicle for which there was no passenger.
|
||||
*/
|
||||
public Vehicle getVehicle()
|
||||
{
|
||||
return vehicle;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#BlueJ class context
|
||||
comment0.params=pickup\ destination
|
||||
comment0.target=Passenger(Location,\ Location)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Passenger\n\ @param\ pickup\ The\ pickup\ location,\ must\ not\ be\ null.\n\ @param\ destination\ The\ destination\ location,\ must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ either\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=java.lang.String\ toString()
|
||||
comment2.params=
|
||||
comment2.target=Location\ getPickupLocation()
|
||||
comment2.text=\n\ @return\ The\ pickup\ location.\n
|
||||
comment3.params=
|
||||
comment3.target=Location\ getDestination()
|
||||
comment3.text=\n\ @return\ The\ destination\ location.\n
|
||||
numComments=4
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Model a passenger wishing to get from one
|
||||
* location to another.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Passenger
|
||||
{
|
||||
private Location pickup;
|
||||
private Location destination;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Passenger
|
||||
* @param pickup The pickup location, must not be null.
|
||||
* @param destination The destination location, must not be null.
|
||||
* @throws NullPointerException If either location is null.
|
||||
*/
|
||||
public Passenger(Location pickup, Location destination)
|
||||
{
|
||||
if(pickup == null) {
|
||||
throw new NullPointerException("Pickup location");
|
||||
}
|
||||
if(destination == null) {
|
||||
throw new NullPointerException("Destination location");
|
||||
}
|
||||
this.pickup = pickup;
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return "Passenger travelling from " +
|
||||
pickup + " to " + destination;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The pickup location.
|
||||
*/
|
||||
public Location getPickupLocation()
|
||||
{
|
||||
return pickup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The destination location.
|
||||
*/
|
||||
public Location getDestination()
|
||||
{
|
||||
return destination;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company
|
||||
comment0.target=PassengerSource(TaxiCompany)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ PassengerSource.\n\ @param\ company\ The\ company\ to\ be\ used.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ if\ company\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=boolean\ requestPickup()
|
||||
comment1.text=\n\ Have\ the\ source\ generate\ a\ new\ passenger\ and\n\ request\ a\ pickup\ from\ the\ company.\n\ @return\ true\ If\ the\ request\ succeeds,\ false\ otherwise.\n
|
||||
comment2.params=
|
||||
comment2.target=Passenger\ createPassenger()
|
||||
comment2.text=\n\ Create\ a\ new\ passenger.\n\ @return\ The\ created\ passenger.\n
|
||||
numComments=3
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Simulate passengers requesting rides from a taxi company.
|
||||
* Passengers should be generated at random intervals.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class PassengerSource
|
||||
{
|
||||
private TaxiCompany company;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class PassengerSource.
|
||||
* @param company The company to be used. Must not be null.
|
||||
* @throws NullPointerException if company is null.
|
||||
*/
|
||||
public PassengerSource(TaxiCompany company)
|
||||
{
|
||||
if(company == null) {
|
||||
throw new NullPointerException("company");
|
||||
}
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
/**
|
||||
* Have the source generate a new passenger and
|
||||
* request a pickup from the company.
|
||||
* @return true If the request succeeds, false otherwise.
|
||||
*/
|
||||
public boolean requestPickup()
|
||||
{
|
||||
Passenger passenger = createPassenger();
|
||||
return company.requestPickup(passenger);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new passenger.
|
||||
* @return The created passenger.
|
||||
*/
|
||||
public Passenger createPassenger()
|
||||
{
|
||||
return new Passenger(new Location(0, 0), new Location(10, 20));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=PassengerSourceTest()
|
||||
comment0.text=\n\ Default\ constructor\ for\ test\ class\ PassengerSourceTest\n
|
||||
comment1.params=
|
||||
comment1.target=void\ setUp()
|
||||
comment1.text=\n\ Sets\ up\ the\ test\ fixture.\n\n\ Called\ before\ every\ test\ case\ method.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ tearDown()
|
||||
comment2.text=\n\ Tears\ down\ the\ test\ fixture.\n\n\ Called\ after\ every\ test\ case\ method.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ testPickup()
|
||||
comment3.text=\n\ Test\ for\ successful\ pickup\ of\ a\ passenger.\n
|
||||
numComments=4
|
||||
@@ -0,0 +1,59 @@
|
||||
import java.util.List;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* The test class PassengerSourceTest.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class PassengerSourceTest
|
||||
{
|
||||
private PassengerSource source;
|
||||
|
||||
/**
|
||||
* Default constructor for test class PassengerSourceTest
|
||||
*/
|
||||
public PassengerSourceTest()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the test fixture.
|
||||
*
|
||||
* Called before every test case method.
|
||||
*/
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
TaxiCompany company = new TaxiCompany();
|
||||
source = new PassengerSource(company);
|
||||
Location taxiLocation = new Location(0, 0);
|
||||
Taxi taxi = new Taxi(company, taxiLocation);
|
||||
List<Vehicle> vehicles = company.getVehicles();
|
||||
vehicles.add(taxi);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the test fixture.
|
||||
*
|
||||
* Called after every test case method.
|
||||
*/
|
||||
@After
|
||||
public void tearDown()
|
||||
{
|
||||
source = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for successful pickup of a passenger.
|
||||
*/
|
||||
@Test
|
||||
public void testPickup()
|
||||
{
|
||||
assertEquals(true, source.requestPickup());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=PassengerTest()
|
||||
comment0.text=\n\ Default\ constructor\ for\ test\ class\ PassengerTest\n
|
||||
comment1.params=
|
||||
comment1.target=void\ setUp()
|
||||
comment1.text=\n\ Sets\ up\ the\ test\ fixture.\n\n\ Called\ before\ every\ test\ case\ method.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ tearDown()
|
||||
comment2.text=\n\ Tears\ down\ the\ test\ fixture.\n\n\ Called\ after\ every\ test\ case\ method.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ testCreation()
|
||||
comment3.text=\n\ Test\ basic\ creation\ of\ a\ passenger.\n\ Ensure\ that\ the\ pickup\ and\ destination\ locations\n\ have\ been\ set.\n
|
||||
numComments=4
|
||||
@@ -0,0 +1,55 @@
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* The test class PassengerTest.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class PassengerTest
|
||||
{
|
||||
/**
|
||||
* Default constructor for test class PassengerTest
|
||||
*/
|
||||
public PassengerTest()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the test fixture.
|
||||
*
|
||||
* Called before every test case method.
|
||||
*/
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the test fixture.
|
||||
*
|
||||
* Called after every test case method.
|
||||
*/
|
||||
@After
|
||||
public void tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Test basic creation of a passenger.
|
||||
* Ensure that the pickup and destination locations
|
||||
* have been set.
|
||||
*/
|
||||
@Test
|
||||
public void testCreation()
|
||||
{
|
||||
Location pickup = new Location(0, 0);
|
||||
Location destination = new Location(1, 2);
|
||||
Passenger passenger1 = new Passenger(pickup, destination);
|
||||
assertEquals(destination, passenger1.getDestination());
|
||||
assertEquals(pickup, passenger1.getPickupLocation());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
Project: taxi-company-stage-one
|
||||
Authors: David J. Barnes and Michael Kölling
|
||||
|
||||
This project is part of the material for chapter 14 of the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
This project provides a partial implementation of a simulation of
|
||||
taxis operating on a city grid to pick up and transport passengers.
|
||||
|
||||
This is the third stage of this project. It illustrates the
|
||||
implementation of a first stage of the project.
|
||||
|
||||
How to start this project:
|
||||
Create a Demo object and invoke its demo method.
|
||||
A single taxi carries a single passenger to their destination.
|
||||
|
||||
The test classes provide a little testing of basic functionality.
|
||||
@@ -0,0 +1,23 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company\ location
|
||||
comment0.target=Shuttle(TaxiCompany,\ Location)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Shuttle\n\ @param\ company\ The\ taxi\ company.\ Must\ not\ be\ null.\n\ @param\ location\ The\ vehicle's\ starting\ point.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ company\ or\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ act()
|
||||
comment1.text=\n\ Carry\ out\ a\ shuttle's\ actions.\n
|
||||
comment2.params=
|
||||
comment2.target=boolean\ isFree()
|
||||
comment2.text=\n\ Is\ the\ shuttle\ free?\n\ @return\ Whether\ or\ not\ this\ vehicle\ is\ free.\n
|
||||
comment3.params=location
|
||||
comment3.target=void\ setPickupLocation(Location)
|
||||
comment3.text=\n\ Receive\ a\ pickup\ location.\n\ @param\ location\ The\ pickup\ location.\n
|
||||
comment4.params=passenger
|
||||
comment4.target=void\ pickup(Passenger)
|
||||
comment4.text=\n\ Receive\ a\ passenger.\n\ Add\ their\ destination\ to\ the\ list.\n\ @param\ passenger\ The\ passenger.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ chooseTargetLocation()
|
||||
comment5.text=\n\ Decide\ where\ to\ go\ next,\ based\ on\ the\ list\ of\n\ possible\ destinations.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ offloadPassenger()
|
||||
comment6.text=\n\ Offload\ a\ passenger\ whose\ destination\ is\ the\n\ current\ location.\n
|
||||
numComments=7
|
||||
@@ -0,0 +1,83 @@
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* A shuttle is able to carry multiple passengers.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Shuttle extends Vehicle
|
||||
{
|
||||
// The list of destinations for the shuttle.
|
||||
private List<Location> destinations;
|
||||
// The list of passengers on the shuttle.
|
||||
private List<Passenger> passengers;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Shuttle
|
||||
* @param company The taxi company. Must not be null.
|
||||
* @param location The vehicle's starting point. Must not be null.
|
||||
* @throws NullPointerException If company or location is null.
|
||||
*/
|
||||
public Shuttle(TaxiCompany company, Location location)
|
||||
{
|
||||
super(company, location);
|
||||
destinations = new LinkedList<>();
|
||||
passengers = new LinkedList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Carry out a shuttle's actions.
|
||||
*/
|
||||
public void act()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the shuttle free?
|
||||
* @return Whether or not this vehicle is free.
|
||||
*/
|
||||
public boolean isFree()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a pickup location.
|
||||
* @param location The pickup location.
|
||||
*/
|
||||
public void setPickupLocation(Location location)
|
||||
{
|
||||
destinations.add(location);
|
||||
chooseTargetLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a passenger.
|
||||
* Add their destination to the list.
|
||||
* @param passenger The passenger.
|
||||
*/
|
||||
public void pickup(Passenger passenger)
|
||||
{
|
||||
passengers.add(passenger);
|
||||
destinations.add(passenger.getDestination());
|
||||
chooseTargetLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide where to go next, based on the list of
|
||||
* possible destinations.
|
||||
*/
|
||||
private void chooseTargetLocation()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Offload a passenger whose destination is the
|
||||
* current location.
|
||||
*/
|
||||
public void offloadPassenger()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#BlueJ class context
|
||||
comment0.params=company\ location
|
||||
comment0.target=Taxi(TaxiCompany,\ Location)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Taxi\n\ @param\ company\ The\ taxi\ company.\ Must\ not\ be\ null.\n\ @param\ location\ The\ vehicle's\ starting\ point.\ Must\ not\ be\ null.\n\ @throws\ NullPointerException\ If\ company\ or\ location\ is\ null.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ act()
|
||||
comment1.text=\n\ Carry\ out\ a\ taxi's\ actions.\n
|
||||
comment2.params=
|
||||
comment2.target=boolean\ isFree()
|
||||
comment2.text=\n\ Is\ the\ taxi\ free?\n\ @return\ Whether\ or\ not\ this\ taxi\ is\ free.\n
|
||||
comment3.params=location
|
||||
comment3.target=void\ setPickupLocation(Location)
|
||||
comment3.text=\n\ Receive\ a\ pickup\ location.\ This\ becomes\ the\n\ target\ location.\n\ @param\ location\ The\ pickup\ location.\n
|
||||
comment4.params=passenger
|
||||
comment4.target=void\ pickup(Passenger)
|
||||
comment4.text=\n\ Receive\ a\ passenger.\n\ Set\ their\ destination\ as\ the\ target\ location.\n\ @param\ passenger\ The\ passenger.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ offloadPassenger()
|
||||
comment5.text=\n\ Offload\ the\ passenger.\n
|
||||
comment6.params=
|
||||
comment6.target=java.lang.String\ toString()
|
||||
comment6.text=\n\ Return\ details\ of\ the\ taxi,\ such\ as\ where\ it\ is.\n\ @return\ A\ string\ representation\ of\ the\ taxi.\n
|
||||
numComments=7
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* A taxi is able to carry a single passenger.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
|
||||
public class Taxi extends Vehicle
|
||||
{
|
||||
private Passenger passenger;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Taxi
|
||||
* @param company The taxi company. Must not be null.
|
||||
* @param location The vehicle's starting point. Must not be null.
|
||||
* @throws NullPointerException If company or location is null.
|
||||
*/
|
||||
public Taxi(TaxiCompany company, Location location)
|
||||
{
|
||||
super(company, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Carry out a taxi's actions.
|
||||
*/
|
||||
public void act()
|
||||
{
|
||||
Location target = getTargetLocation();
|
||||
if(target != null) {
|
||||
// Find where to move to next.
|
||||
Location next = getLocation().nextLocation(target);
|
||||
setLocation(next);
|
||||
if(next.equals(target)) {
|
||||
if(passenger != null) {
|
||||
notifyPassengerArrival(passenger);
|
||||
offloadPassenger();
|
||||
}
|
||||
else {
|
||||
notifyPickupArrival();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
incrementIdleCount();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the taxi free?
|
||||
* @return Whether or not this taxi is free.
|
||||
*/
|
||||
public boolean isFree()
|
||||
{
|
||||
return getTargetLocation() == null && passenger == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a pickup location. This becomes the
|
||||
* target location.
|
||||
* @param location The pickup location.
|
||||
*/
|
||||
public void setPickupLocation(Location location)
|
||||
{
|
||||
setTargetLocation(location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a passenger.
|
||||
* Set their destination as the target location.
|
||||
* @param passenger The passenger.
|
||||
*/
|
||||
public void pickup(Passenger passenger)
|
||||
{
|
||||
this.passenger = passenger;
|
||||
setTargetLocation(passenger.getDestination());
|
||||
}
|
||||
|
||||
/**
|
||||
* Offload the passenger.
|
||||
*/
|
||||
public void offloadPassenger()
|
||||
{
|
||||
passenger = null;
|
||||
clearTargetLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return details of the taxi, such as where it is.
|
||||
* @return A string representation of the taxi.
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return "Taxi at " + getLocation();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=TaxiCompany()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ TaxiCompany\n
|
||||
comment1.params=passenger
|
||||
comment1.target=boolean\ requestPickup(Passenger)
|
||||
comment1.text=\n\ Request\ a\ pickup\ for\ the\ given\ passenger.\n\ @param\ passenger\ The\ passenger\ requesting\ a\ pickup.\n\ @return\ Whether\ a\ free\ vehicle\ is\ available.\n
|
||||
comment2.params=vehicle
|
||||
comment2.target=void\ arrivedAtPickup(Vehicle)
|
||||
comment2.text=\n\ A\ vehicle\ has\ arrived\ at\ a\ pickup\ point.\n\ @param\ vehicle\ The\ vehicle\ at\ the\ pickup\ point.\n\ @throws\ MissingPassengerException\ If\ there\ is\ no\ passenger\ waiting.\n
|
||||
comment3.params=vehicle\ passenger
|
||||
comment3.target=void\ arrivedAtDestination(Vehicle,\ Passenger)
|
||||
comment3.text=\n\ A\ vehicle\ has\ arrived\ at\ a\ passenger's\ destination.\n\ @param\ vehicle\ The\ vehicle\ at\ the\ destination.\n\ @param\ passenger\ The\ passenger\ being\ dropped\ off.\n
|
||||
comment4.params=
|
||||
comment4.target=java.util.List\ getVehicles()
|
||||
comment4.text=\n\ @return\ The\ list\ of\ vehicles.\n
|
||||
comment5.params=
|
||||
comment5.target=Vehicle\ scheduleVehicle()
|
||||
comment5.text=\n\ Find\ a\ free\ vehicle,\ if\ any.\n\ @return\ A\ free\ vehicle,\ or\ null\ if\ there\ is\ none.\n
|
||||
numComments=6
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user