onesnapthanos
onesnapthanos

Reputation: 121

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

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

Upvotes: 0

Views: 432

Answers (3)

ThallsEternal
ThallsEternal

Reputation: 152

Add an array or List to ParkedCar that keeps track of all the tickets for that vehicle.

public void issueParkingTicket(ParkedCar car, ParikingMeter meter)
{
   if(isParkingTimeExpired(car,meter) == true){
      ParkingTicket ticket = new ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));
  car.tickets.add(theTicket);

}

then in your main class you can access every ticket for any car object.

public static void main(String [] args)
{
  ParkedCar car = new ParkedCar(/*params*/);
  Officer officer1 = new Officer(/*params*/);
  officer1.issueTicket(/*params*/);

  //make a getter for the tickets in the ParkedCar class
  car.getTickets();
}

Upvotes: 1

Erik McKelvey
Erik McKelvey

Reputation: 1627

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.

Upvotes: 2

geffchang
geffchang

Reputation: 3340

You can pass in a ticket, and manipulate the ticket inside the issueParkingTicket method.

ParkingTicket ticket = new ParkingTicket();
officer1.issueParkingTicket(myCar1,myMeter1,ticket); 
ticket.displayDetails();

Upvotes: 0

Related Questions