Search code examples
javamethodsconstructorinvokebluej

How to invoke ParkingTicket(parker, parkee)?


Here is my problem I can't seem to figure out how to invoke a ParkingTicket object if (carMinutesPaid>meterMinutesPaid)? can any one help here are the details below to the question.

public static  ParkingTicket checkParking(int carMinutesParked, int meterMinutesPaid)
{


   Car parker = carMinutesParked;
   ParkingMeter parkee = parkee;


    if(carMinutesParked>meterMinutesPaid){
        return new ParkingTicket(parker, parkee);
    }
    else if(carMinutesParked<=meterMinutesPaid){
        System.out.println("null");
    }
    return new ParkingTicket(parker, parkee);
}

Here is the question for my project.

Remember, this method must be able to be used without a ParkingTicket object in existence.

  • Using a Car parameter and a ParkingMeter parameter, decide whether a ParkingTicket object should be created.
  • Invoke ParkingTicket(parker, parkee) if a ticket was merited, and return the result.
  • Return null if a ticket was not merited.

Here is my car class:

 /**
 * This is a Car class for Impark.
 * 
 * @author Tre
 * @version 2.0 15 October 2015
 */
public class Car
{
private  static final int    MINIMUM_PLATE_LENGTH=2;
private  static final int    MAXIMUM_PLATE_LENGTH=7;
public   static final char   MANUAL_TRANSMISSION='m';
public   static final char   AUTOMATIC_TRANSMISSION='a';

private static int defaultMinutesParked = 0;
private static double defaultOdometerInKm = 50000.5; 

private  String  licensePlate;
private  char    transmissionType;
private  double  odometerInKm; 
private  int     minutesParked;

/**
 * @param newProposedLicensePlate the license plate of the car can equal null 
 * but must be between MINIMUM_PLATE_LENGTH and MAXIMUM_PLATE_LENGTH
 */
public Car(String newProposedLicensePlate)
{
    setLicensePlate(newProposedLicensePlate);
    transmissionType = AUTOMATIC_TRANSMISSION;
    odometerInKm     = defaultOdometerInKm;
    minutesParked    = defaultMinutesParked;
}

/**
 * @return the license plate of the car can equal null 
 * but must be between MINIMUM_PLATE_LENGTH and MAXIMUM_PLATE_LENGTH
 */
public String getLicensePlate()
{
    return licensePlate;
}

/**
 * @return the transmission type MANUAL_TRANSMISSION or AUTOMATIC_TRANSMISSION
 */
public char getTransmissionType()
{
    return transmissionType;
}

/**
 * @return the odometer in kilometers
 */
public double getOdometerInKm()
{
    return odometerInKm;
}

/**
 * Recieve the license plate
 * Mutator.licensePlate.
 * @param proposedLicense String Conforming to ICBC *length* guidlines:
 * http://www.icbc.com/vehicle-registration/license-plates/Pages/Personalized-licence-plates.aspx
 * May also be null. The null represents a car without a plate
 * If validation fails, null will be set.
 */
public void setLicensePlate(String proposedLicense)
{
    if(proposedLicense==null){
        licensePlate = proposedLicense;
    }
    else if(proposedLicense.length()>=MINIMUM_PLATE_LENGTH && proposedLicense.length()<=MAXIMUM_PLATE_LENGTH){
        licensePlate = proposedLicense;
    }
    else{
        licensePlate = null;
    }
}

/**
 * @param mOrA recieve the transmission type MANUAL_TRANSMISSION or AUTOMATIC_TRANSMISSION 
 * if invalid type of transmission is entered then will return "Installation failure: 'mOrA' is not a vaild transmission type"
 */
public void setTransmissionType(char mOrA)
{
    if(mOrA==MANUAL_TRANSMISSION){
        transmissionType = mOrA;
    }
    else if(mOrA==AUTOMATIC_TRANSMISSION){
        transmissionType = mOrA;
    }
    else if (mOrA==mOrA){
        System.out.println("Installation failure:" + " " + ("'")+(mOrA)+("'") + " " + "is not a valid transmission type."); 
    }
    else{
        transmissionType = mOrA;
    }
} 

/**
 * @return the value of the odometer in with the String kilometers
 */
public String readOdometer()
{
    return odometerInKm + " " + "kilometers";  
}

/**
 * @return the false if the minutesParked equals zero; otherwise true
 */
public boolean isParked()
{
    if(minutesParked==defaultMinutesParked){
        return false;
    }
    else{
        return true;
    }
}

/**
 * @param duration replaces any existing value in minutesParked with the value from duration
 */        
public void park(int duration)
{
    if(duration>=defaultMinutesParked){
        minutesParked = duration;
    }
}

/**
 * @param aOdometerInKm recieve the odometer in kilometers
 */
public void setOdometerInKm(double aOdometerInKm)
{
    odometerInKm = aOdometerInKm;
}

/**
 * @param aMinutesParked recieve the minutes parked in the stall but can not be a negative number
 * if invalid number of minutes is entered then the number of minutes will not change.
 */
public void setMinutesParked(int aMinutesParked)
{   
    if(aMinutesParked>=defaultMinutesParked){
        minutesParked = aMinutesParked;
    }
    else{
        return; 
    }
}

/**
 * @return the minutes parked
 */
public int getMinutesParked()
{
    return minutesParked;
}

}

here is my ParkingMeter class:

/**
 * This is a ParkingMeter class for Impark.
 * 
 * @author Tre
 * @version 2.0 15 October 2015
 */
public class ParkingMeter
{
private int minutesPaid;
private String methodPaid;

/**
 * @param newMinutesPaid the minutes paid for parking meter
 */
public ParkingMeter()
{
}

/**
 * @return the minutes paid
 */
public int getMinutesPaid()
{
    return minutesPaid;
}

/**
 * @return the method paid
 */
public String getMethodPaid()
{
    return methodPaid;
}

/**
 * @param paidBy the payment method customer will paid by
 */
public void setMethodPaid(String paidBy) /* BONUS for creating method paid */
{
    if(methodPaid=="Visa"){
        methodPaid = paidBy;
    }
    else if(methodPaid=="Master Card"){
        methodPaid = paidBy;
    }
    else if(methodPaid=="American Express"){
        methodPaid = paidBy;
    }
    else if(methodPaid=="Cash"){
        methodPaid = paidBy;
    }
    else if(methodPaid=="Debit"){
        methodPaid = paidBy;
    }
    else{
        methodPaid = paidBy;
    }
}

/**
 * @param quantity the added minutes paid must not have a negative number
 */
public void addMinutesPaid(int quantity)
{
    if(quantity>=0){
        minutesPaid+=quantity;
    }
}

}

and here is my ParkingTicket class:

/**
 * This is a ParkingTicket class for Impark.
 * 
 * @author Tre 
* @version 1.0
*/
public class ParkingTicket
{
private final String  referenceNumber;

private static String  carLicensePlate;


private static int     carMinutesParked;
private static int     meterMinutesPaid;

private static int count = 1000;

private static String PREFIX = "V";

/**
 * @param recorededLicense the value of the tick number
 */
private ParkingTicket(String recordedLicense, int newCarMinutesParked,   int newMeterPaidMinutes)
{
    referenceNumber = (PREFIX+count++);
    carMinutesParked = newCarMinutesParked;
    meterMinutesPaid = newMeterPaidMinutes;

}

/**
 * @param 
 */
private ParkingTicket(Car parker, ParkingMeter parkee)
{
    this(parker.getLicensePlate(), parker.getMinutesParked(), parkee.getMinutesPaid());

}

/**
 * @return referenceNumber the reference number
 */
public String getReferenceNumber()
{
    return referenceNumber;
}

/**
 * @return carLicensePlate the car's license plate
 */
public String getCarLicensePlate()
{
    return carLicensePlate;
}

/**
 * @return carMinutesParked the minutes car was parked
 */
public int getCarMinutesParked()
{
    return carMinutesParked;
}

/**
 * @return meterMinutesPaid the minutes paid on meter
 */
public int getMeterMinutesPaid()
{
    return meterMinutesPaid;
}

/**
 * @return count the with initial value of 1000
 */
public int getCount()
{
    return count;
}


public static  ParkingTicket checkParking(int carMinutesParked, int meterMinutesPaid)
{


   Car parker = carMinutesParked;
   ParkingMeter parkee = parkee;


    if(carMinutesParked>meterMinutesPaid){
        return new ParkingTicket(parker, parkee);
    }
    else if(carMinutesParked<=meterMinutesPaid){
        return null;
    }
    return new ParkingTicket(parker, parkee);
}
}

Solution

  • This requirement:

    Using a Car parameter and a ParkingMeter parameter, decide whether a ParkingTicket object should be created.

    suggests that you provide two parameters to the checkParking method, one is of the type Car and one of the ParkingMeter. So it should be like so:

    public static  ParkingTicket checkParking(Car car, ParkingMeter meter)
    

    This code :

       Car parker = carMinutesParked;
       ParkingMeter parkee = parkee;
    

    won't even compile

    • line 1: you're trying to assign int to object - that's called type mismatch.
    • line 2: variable parkee is not declared anywhere (except for the headline of the question).

    You see, only the Car object holds the information about the parking duration, and you need the object for creating parking ticket. Same for the ParkingMeter

    It should be vice versa - you get the values from the objects:

    int carMinutesParked = car.getMinutesParked();
    int meterMinutesPaid = meter.getMinutesPaid();
    

    and proceed from here with if( or even use it in if without declaring temporary variables).

    This one:

    Invoke ParkingTicket(parker, parkee) if a ticket was merited, and return the result.

    you did OK.

    Now this requirement:

    Return null if a ticket was not merited.

    suggest the method will return null, not string that equals to "null" .

    So, based on these requirements it should rather be:

    public static  ParkingTicket checkParking(Car car, ParkingMeter meter)
    {
        //sanity check (bonus)
        if ((car == null) || (meter == null))
            return null;
    
        if(car.getMinutesParked() > meter.getMinutesPaid()){
            return new ParkingTicket(car, meter);
        }
    
        return null;
    }
    

    Note however, I don't know if you need any additional logic in this code and don't suggest this should be your final version, just explaining the general approach.