1

I am coding to get a parking ticket simulator working. I have many classes, the ones relevant are a ParkedCar class, ParkingTicket class and a policeOfficer class. Now i wrote a method where I take the licensePlateNumber variable from ParkedCar as a parameter, and I have to get the ParkingTicket count for a car. I am getting an error in the test class which i have below, it wont compile because of this. The method is as follows

PoliceOfficer class

public int getParkingTicketsCountForACar(ParkedCar licensePlateNumber){
int numberOfTickets = 0;
String carPlateNumber = licensePlateNumber.getLicensePlateNumber();
for(ParkingTicket ticket: ticketList){
if(carPlateNumber.equalsIgnoreCase(ticket.getCarLicensePlateNumber())){
numberOfTickets++
}
return numberOfTickets;
}

Test class

public void testGetParkingTicketsCountForACar() {

    PoliceOfficer p = new PoliceOfficer("Adam White","RCMP5225");
    ParkedCar car = new ParkedCar("Bob Smith", "Porsche", 2015, "1A2B3C", 65);
    ParkingMeter meter = new ParkingMeter("Burnaby",false,10.5,60);
    p.issueParkingTicket(car, meter);
    ParkedCar car2 = new ParkedCar("Scott Marks", "Ford", 2010, "MYCAR", 30);
    meter.setNumberOfPurchasedMinutes(1);
    p.issueParkingTicket(car2, meter);
    car.setNumberOfMinutesParked(125);
    meter.setNumberOfPurchasedMinutes(20);
    p.issueParkingTicket(car, meter);
    assertEquals(2,p.getParkingTicketsCountForACar("1a2B3c"));

I am getting an error at the very end "1a2b3c" saying java.lang.String cannot be converted to ParkedCar. Is there a way to fix this without using the String override method?

1
  • getParkingTicketsCountForACar expects an argument of type "ParkedCar" but you're passing a String, which is why your code doesn't compile. Seem like in this case you'd want to call p.getParkingTicketsCountForACar(car) Commented Apr 2, 2020 at 7:14

3 Answers 3

2

you are passing String value of licensePlateNumber, and in the method

public int getParkingTicketsCountForACar(ParkedCar licensePlateNumber){}

you expect input of type ParkedCar, and then u you are extracting the licensePlateNumber. you can shrink the method just to:

public int getParkingTicketsCountForACar(String licensePlateNumber){
    for(ParkingTicket ticket: ticketList){
    if(licensePlateNumber.equalsIgnoreCase(ticket.getCarLicensePlateNumber())){
        numberOfTickets++
    }
    return numberOfTickets;
}
Sign up to request clarification or add additional context in comments.

Comments

0

This method accepts ParkedCar type

public int getParkingTicketsCountForACar(ParkedCar licensePlateNumber){}

But you trying to pass String

assertEquals(2,p.getParkingTicketsCountForACar("1a2B3c"));

1 Comment

If i convert the parameter to a String then how would I access the licensePlateNumber input?
0

You either have to change your method

public int getParkingTicketsCountForACar(ParkedCar licensePlateNumber){ ...

to

public int getParkingTicketsCountForACar(String carPlateNumber){ ...

and just remove the second line in the method,
OR you'll have to pass the method the actual ParkedCar object reference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.