我正在編寫一個程序來模擬停車票系統。我共有4個不同的課程,分別是Parked Car,ParkingMeter,ParkingTicket和PoliceOfficer。在我的PoliceOfficer班上,我有一個if語句來確定警員是否應該爲停放的車輛發放車票。返回一個對象的新實例
我有這樣的聲明:return new 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簽名需要匹配時,我打電話來創建一個新的ParkingTicket對象。感謝您的快速幫助:) – Chewy 2014-12-04 16:23:15