gpt4 book ai didi

java - 如何调用 parking 票(parker, parker)?

转载 作者:太空宇宙 更新时间:2023-11-04 13:18:26 26 4
gpt4 key购买 nike

这是我的问题,我似乎无法弄清楚如何调用 ParkingTicket 对象 if (carMinutesPaid>meterMinutesPaid)?任何人都可以帮忙,下面是该问题的详细信息。

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

}

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

最佳答案

这个要求:

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 分配给对象 - 这称为类型不匹配。
  • 第 2 行:变量 Parkee 未在任何地方声明(问题标题除外)。

你看,只有Car对象保存了 parking 时长的信息,并且你需要对象来创建 parking 票。 ParkingMeter

也是如此

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

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.

建议该方法将返回 null,而不是等于“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;
}

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

关于java - 如何调用 parking 票(parker, parker)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33325163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com