onesnapthanos
onesnapthanos

Reputation: 121

Beginner Java converting an arraylist object to output String value?

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?

Upvotes: 1

Views: 61

Answers (3)

bkis
bkis

Reputation: 2587

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.

Upvotes: 0

Alufoil
Alufoil

Reputation: 36

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

Upvotes: 2

This method accepts ParkedCar type

public int getParkingTicketsCountForACar(ParkedCar licensePlateNumber){}

But you trying to pass String

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

Upvotes: 0

Related Questions