2014-12-04 187 views
1

我正在編寫一個程序來模擬停車票系統。我共有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; 
} 
} 

回答

1

你拿到你的論點相反。該ParkingTicket構造函數需要它的參數是:

(ParkedCar, PoliceOfficer, ParkingMeter) 

但你傳遞

(PoliceOfficer, ParkedCar, ParkingMeter) 

所有你需要做的是改變調用new ParkingTicket(car, this, meter)

+0

忘記我的ParkingTicket簽名需要匹配時,我打電話來創建一個新的ParkingTicket對象。感謝您的快速幫助:) – Chewy 2014-12-04 16:23:15

1

你提到實例中PoliceOfficerParkingTicket(所以thisPoliceOfficer

new ParkingTicket(this,car,meter) 

我認爲它應該是

new ParkingTicket(car,this,meter) 

因爲構造函數簽名是ParkingTicket(ParkedCar, PoliceOfficer, ParkingMeter)

相關問題