我正在编写一个模拟停车票系统的程序。总共有4个不同的类别,分别是ParkedCar,ParkingMeter,ParkingTicket和PoliceOfficer。在我的PoliceOfficer类中,我有一个if语句,以确定警务人员是否应为停放的汽车开具罚单。
我要执行以下声明:返回新的ParkingTicket(this,car,meter)。我收到不兼容的类型错误。如果我需要将代码发布给其他班级,请告诉我。
停车票类别:
import java.text.DecimalFormat;
public class ParkingTicket {
private static int ticketCount;
private ParkedCar car;
private PoliceOfficer officer;
private ParkingMeter meter;
private double fine;
public final double BASE_FINE = 25.0;
public final double HOURLY_FINE = 10.0;
public ParkingTicket (ParkedCar aCar, PoliceOfficer anOfficer, ParkingMeter aMeter) {
this.car = aCar;
this.officer = anOfficer;
this.meter = aMeter;
calculateFine();
}
public void calculateFine() {
// Calculate fine & increment ticket count
++ticketCount;
int timeOver = (int)Math.ceil((car.getMinutesParked() - meter.getMinutesPurchased() / 60.0));
fine = BASE_FINE * HOURLY_FINE * (timeOver - 1);
}
public String toString() {
DecimalFormat newDecimal = new DecimalFormat("$###.00");
return "Ticket Number: " + ticketCount + "\n" +
car + "\n" + meter + "\n" + "Fine: " + newDecimal.format(fine) + "\n"
+ officer;
}
}
警务人员班:
public class PoliceOfficer {
// Declare data fields
private String name;
private String badgeNumber;
// Default constructor
public PoliceOfficer (String name, String badgeNumber) {
this.name = name;
this.badgeNumber = badgeNumber;
}
// Copy of PoliceOfficer
public PoliceOfficer (PoliceOfficer OfficerCopy) {
this.name = OfficerCopy.name;
this.badgeNumber = OfficerCopy.badgeNumber;
}
// Class to check a parked car and determine if the
// cars time has expired
public ParkingTicket check(ParkedCar car, ParkingMeter meter) {
if (car.getMinutesParked() > meter.getMinutesPurchased()) {
// Creating a new object of ParkingTicket object
return new ParkingTicket(this,car,meter);
} else {
return null;
}
}
// toString class
public String toString() {
return "Officer Name: " + name + "/n" + "Badge ID: " + badgeNumber;
}
}
最佳答案
您只是颠倒了争论。 ParkingTicket
构造函数要求其参数为:
(ParkedCar, PoliceOfficer, ParkingMeter)
但是你过去了
(PoliceOfficer, ParkedCar, ParkingMeter)
您需要做的就是将呼叫更改为
new ParkingTicket(car, this, meter)
。