如何调用ParkingTicket(parker,parkee)?

时间:2015-10-25 01:23:53

标签: java methods constructor invoke bluej

这是我的问题,如果(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对象的情况下使用。

  • 使用Car参数和ParkingMeter参数,确定是否应创建ParkingTicket对象。
  • 如果票证是值得的,则调用ParkingTicket(parker, parkee),并返回结果。
  • 如果票证不合理,则返回null

这是我的车类:

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

}

这是我的ParkingMeter课程:

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

}

这是我的ParkingTicket课程:

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

1 个答案:

答案 0 :(得分:0)

此要求:

  

使用Car参数和ParkingMeter参数,决定是否   应该创建ParkingTicket对象。

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

public static  ParkingTicket checkParking(Car car, ParkingMeter meter)

此代码:

   Car parker = carMinutesParked;
   ParkingMeter parkee = parkee;

甚至没有编译

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

您会看到,只有Car对象包含有关停车时间的信息,您需要对象来创建停车票。 ParkingMeter

也是如此

反之亦然 - 您从对象中获取值:

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

并使用if从此处继续(或者甚至在if中使用它而不声明临时变量)。

这一个:

  

如果票证是值得的,则调用ParkingTicket(parker,parkee),和   返回结果。

你没事。

现在这个要求:

  

如果票证不值得,则返回null。

建议该方法将返回null,而不是等于&#34; null&#34;

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

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

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