0

I guess it's a simple question, but I've just started to learn! :P

I need to invoke the ParkingTicket(parker, parkee) constructor in the checkParking() gateway method (if the "if" statement is true) but I'm getting the following error: incompatible types: int cannot be converted to Car

public class ParkingTicket
{
  private static int count = 1000;
  private final String referenceNumber;
  private final String carLicensePlate;
  private int carMinutesParked;
  private int meterMinutesPaid;
  private static final String prefix = "V";

  private ParkingTicket(String recordedLicense, int carParkedMinutes, int meterPaidMinutes)
  {
      count = (count + 1);
      referenceNumber = prefix + count;
      carLicensePlate = recordedLicense;
      carMinutesParked = carParkedMinutes;
      meterMinutesPaid = meterPaidMinutes;
  }

  private ParkingTicket(Car parker, ParkingMeter parkee)
  {
      this(parker.getLicensePlate(),
           parker.getMinutesParked(),
           parkee.getMinutesPaid());
  }

  public static int checkParking(Car minutesParked, ParkingMeter minutesPaid)
  {
    if (minutesParked.getMinutesParked() > minutesPaid.getMinutesPaid())
    {
       new ParkingTicket(minutesParked.getMinutesParked(), minutesPaid.getMinutesPaid());       
    }
    else
    {
       ParkingTicket pt1 = null;
    }
  }
} 

Thanks in advance for all the help!

2 Answers 2

3

On the line

new ParkingTicket(minutesParked.getMinutesParked(), minutesPaid.getMinutesPaid());

You are trying to use the constructor

 private ParkingTicket(Car parker, ParkingMeter parkee)

As you can see this takes two arguments, a Car and and ParkingMeter

The arguments you are passing in are int

I'm guessing you want to call the first constructor? You just need to change the line to include the license

 new ParkingTicket(minutesParked.getLicensePlate(), minutesParked.getMinutesParked(), minutesPaid.getMinutesPaid());       
Sign up to request clarification or add additional context in comments.

4 Comments

It wasn't working because of the "minutesParked.getRecordedLicense()" argument, but when I changed to "minutesParked.getLicensePlate()" it worked! But now I'm getting the following error in the method: missing return statement =\
@RodrigoSilva Your method checkParking specifies an int return type so it expects you to return an int value at some point. You can change the type of the method to void if you don't want the method to return anything to its' caller.
That's because your method checkParking expects you to return an int - see the method signature public static int checkParking
I changed to 'void' and it compiled! However, when I try to create an object they ask for the values and all I get is int cannot be converted to Car
1

You should use pass the parameters that your constructor is expecting :

ParkingTicket ticket = new ParkingTicket(minutesParked, minutesPaid); 

I'd change the names of those variables though. minutesParked is not a good name for a Car and minutesPaid is not a good name for a ParkingMeter.

P.S. you probably want to do something with the instance you created.

5 Comments

But this way I won't be invoking the constructor, right?
@RodrigoSilva You will be invoking the private ParkingTicket(Car parker, ParkingMeter parkee) constructor which will call the private ParkingTicket(String recordedLicense, int carParkedMinutes, int meterPaidMinutes) constructor.
Thanks for the tips about the names of the variables @eran! About the other instances... yeah, I'm gonna use then in the future! But I still don't get it, how this will call the private ParkingTicket(Car parker, ParkingMeter parkee) constructor? And how this constructor will call the private ParkingTicket(String recordedLicense, int carParkedMinutes, int meterPaidMinutes) constructor too? I don't get it.
@RodrigoSilva You should read some Java tutorial about constructors. The new keywords invokes a constructor (which looks like a method having the same name as the class name but no return type) that matches the parameters you are passing. Inside the constructor, if the first line contains this(...) is invokes a different constructor of the same class that matches the passed parameters.
I was thinking the new keywords invokes a method from another class (the same as the ParkingTicket(Car parker, ParkingMeter parkee) constructor). About the this(,,,) I didn't know that, thanks!

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.