/******************************** FILE HEADER ********************************** Main Class File: Main.java File: Taxi.java Author: Michael Schultz CS Login: mschultz Collaborators: Completion Date: 9/24/02 Course: CS 302 TA: Mr. Schultz Compiler: Code Warrior IDE 5.0 Platform: Windows 2000 *******************************************************************************/ /** * Defines the class of Taxi objects, which can pick up passengers and drive * around at certain rates accruing money. The taxi is sent out at the start * of a day and given a driver, and returns at the end of a day to deposit the * earnings. * Bugs: none known * * @author Michael Schultz * @version 1.0 * @see also Main.java */ public class Taxi { /** The standard fare, in dollars per miles */ public static final double STANDARD_RATE = 2.00; /** The discounted student fare, in dollars per miles */ public static final double STUDENT_RATE = 1.50; /** The zero fare rate, when no passengers are aboard */ private static final double NO_PASSENGERS = 0.00; /** A default value for the driver's name when there isn't any driver */ private static final String NO_DRIVER = ""; /** This Taxi's number */ private final int carNumber; /** This Taxi's driver's name */ private String driverName; /** This Taxi's current rate setting */ private double currentRate; /** This Taxi's total amount of money earned so far */ private double totalMoneyEarned; /** * Constructs a Taxi objects with the specified car number. DriverName, * totalMoneyEarned, and currentRate are alll given initial empty or * zero values. * * @param carNumber The number of the car */ public Taxi(int carNumber) { this.carNumber = carNumber; driverName = NO_DRIVER; resetTotalMoneyEarned(); setCurrentRate(NO_PASSENGERS); System.out.println("A new Taxi object, Taxi #" + getCarNumber() + " has been created\n"); } /** * An accessor for the car number * * @return The car number */ public int getCarNumber() { return carNumber; } /** * An accessor for the driver name * * @return The driver name */ public String getDriverName() { return driverName; } /** * An accessor for the current rate * * @return The current fare rate */ public double getCurrentRate() { return currentRate; } /** * An accessor for the total money earned thus far * * @return The total money earned thus far */ public double getTotalMoneyEarned() { return totalMoneyEarned; } /** * A mutator for the current rate * * @param rate The new fare rate for the Taxi */ private void setCurrentRate(double rate) { currentRate = rate; } /** * A mutator that resets the total money earned to zero */ private void resetTotalMoneyEarned() { totalMoneyEarned = 0.00; } /** * The public method that begins the Taxi's work day, given a specified * driver. Resets the total money earned * * @param driverName The name of the driver for that day */ public void startWorkDay(String driverName) { this.driverName = driverName; resetTotalMoneyEarned(); System.out.println("Taxi #" + getCarNumber() + " now starting day " + "with driver " + getDriverName() + "\n"); } /** * The public method that ends the Taxi's work day, reseting the driver * to the default value */ public void stopWorkDay() { driverName = NO_DRIVER; System.out.println("Taxi #" + getCarNumber() + " now ending day\n"); } /** * A public method to pick up passengers assigned at a specified rate, * and sets the current rate to it * * @param rate The new fare rate for the Taxi */ public void pickUpPeople(double rate) { setCurrentRate(rate); System.out.println("Taxi #" + getCarNumber() + " acquired passengers " + "at rate $" + getCurrentRate() + "/mile\n"); } /** * A public method to drop off passengers that it might have, and sets * the rate back to the zero rate */ public void dropOffPeople() { setCurrentRate(NO_PASSENGERS); System.out.println("Taxi #" + getCarNumber() + " released passengers" + "\n"); } /** * A public method to drive around a specified number of miles. The total * money earned will increase based upon the current rate per mile * * @param miles The number of miles to drive */ public void drive(int miles) { double newEarnings = miles * currentRate; double oldEarnings = getTotalMoneyEarned(); totalMoneyEarned = oldEarnings + newEarnings; System.out.println("Taxi #" + getCarNumber() + " drove " + miles + " miles." + " Now total earnings are $" + getTotalMoneyEarned() + "\n"); } /** * The overriden method inherited from the Object class. Provides a String * summary of this Taxi object */ public String toString() { return "This is a Taxi object:\n" + " car number = " + getCarNumber() + "\n" + " driver name = " + getDriverName() + "\n" + " current rate = $" + getCurrentRate() + "/mile\n" + " money earned = $" + getTotalMoneyEarned() + "\n" + "\n"; } }