Java calling a void method in a different class to a main class?

onesnapthanos :

I have a method to issue a parkingTicket in my officer class,

public ParkingTicket issueParkingTicket(ParkedCar car,ParkingMeter meter){

        if(isParkingTimeExpired(car,meter) == true){
          ParkingTicket ticket = new ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));
          ticket.displayDetails();


          return ticket;
            } else
             { return null;
    }           
   }

I was asked to modify it in a way to have it not return anything so i made it void, so I did it this way

 public void issueParkingTicket(ParkedCar car,ParkingMeter meter){

        if(isParkingTimeExpired(car,meter) == true){
          ParkingTicket ticket = new ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));

          ticketList.add(ticket);
          ticket.displayDetails();
    }

Now in my main driver class, I have to created an officer object but since I had to make the method void to not return anything I am getting an error saying void cannot be converted to ParkingTicket, if i take away myCar1,myMeter1 from the parentheses, I get an error telling me there's arguments required ParkedCar , ParkingMeter. How can I make it so I don't get an error and the officer object created can issue a parking ticket?

 public class Driver
{
    main method
    public static void main(String [] args){

ParkedCar myCar1 = new ParkedCar("Fred","toyota",2013,"Z1234",25);
    myCar1.displayDetails();

    ParkingMeter myMeter1 = new ParkingMeter("SYDNEY",true,0.15,70); 
    myMeter1.displayDetails();


    PoliceOfficer officer1 = new PoliceOfficer("John doe","DMX1234"); 
    ParkingTicket ticket = officer1.issueParkingTicket(myCar1,myMeter1); 

This is the source of my error ParkingTicket ticket = officer1.issueParkingTicket(myCar1,myMeter1);

Erik McKelvey :

You can just remove the part that says:

ParkingTicket ticket = 

in your main method.

Since the ticket is created in the function there is no need to create a new ticket when you call the function.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=403764&siteId=1