如何调用Parking Ticket(parker, parker)?

How to invoke ParkingTicket(parker, parkee)?

这是我的问题 我似乎无法弄清楚如何在 (carMinutesPaid>meterMinutesPaid) 的情况下调用 ParkingTicket 对象?任何人都可以帮助这里是问题的详细信息。

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);
}

这是我的项目的问题。

请记住,此方法必须能够在没有 ParkingTicket 对象存在的情况下使用。

这是我的车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;
}

}

这是我的停车计时器 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;
    }
}

}

这是我的停车票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);
}
}

这个要求:

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

建议您向checkParking方法提供两个参数,一个是Car类型,一个是ParkingMeter类型。所以它应该是这样的:

public static  ParkingTicket checkParking(Car car, ParkingMeter meter)

此代码:

   Car parker = carMinutesParked;
   ParkingMeter parkee = parkee;

甚至无法编译

  • 第 1 行:您正在尝试将 int 分配给 object - 这称为类型不匹配。
  • 第 2 行:没有在任何地方声明变量 parkee(问题的标题除外)。

你看,只有Carobject有停车时长的信息,创建停车需要object票。 ParkingMeter

也一样

反之亦然 - 您从 objects:

中获取值
int carMinutesParked = car.getMinutesParked();
int meterMinutesPaid = meter.getMinutesPaid();

然后从这里开始使用 if(或者甚至在 if 中使用它而不声明临时变量)。

这个:

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

你做的不错。

现在这个要求:

Return null if a ticket was not merited.

建议该方法将 return 为空,而不是等于 "null" 的字符串。

因此,根据这些要求,它应该是:

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;
}

但是请注意,我不知道您是否需要此代码中的任何其他逻辑,并且不建议这应该是您的最终版本,只是解释一般方法。