-4

I'm currently working on an assignment where it's asking me to modify my previous method. My current method is:

public ParkingTicket issueParkingTicket(ParkedCar car,ParkingMeter meter){
    if(isParkingTimeExpired(car,meter) == true){
        ParkingTicket ticket = new ParkingTicket(getOfficerName(),getBadgeNumber(),car.getLicenseNumber(),car.getCarMake(),car.getCarModel(),calculateFine(car, meter));
        ticket.displayTicketDetails();
        return ticket;
    }
    return null;
}

The collection is a collection of parking tickets (which is also a class "public class ParkingTicket). The method I wrote above resides in a different class (public class ParkingOfficer).

This method will create an object. Additionally I'd like this method to create the object and add it to the collection as an ArrayList. How would I do that?

Sнаđошƒаӽ
  • 13,406
  • 11
  • 67
  • 83
Paul Kim
  • 17
  • 3
  • 1
    What is "_the_ collection"? I don't see a collection anywhere in your code. – Tim Biegeleisen Apr 07 '16 at 00:27
  • Apologies for being uninformative, The collection is a collection of parking tickets (which is also a class "public class ParkingTicket). The method I wrote above resides in a different class (public class ParkingOfficer). – Paul Kim Apr 07 '16 at 00:30
  • `ArrayList tickets =new ArrayList();` Then `tickets.add(` ticket here`)` – Laurel Apr 07 '16 at 00:32
  • What is the specific problem? What prevents you from invoking the existing collection's `add()` method to add an element? "How would I go about that?" rarely constitutes a good question here. – John Bollinger Apr 07 '16 at 00:35
  • @PaulKim please add this into your question via the 'edit' button so people don't have to look into comments to figure that useful bit out. – Bloodied Apr 07 '16 at 01:25

2 Answers2

0

In your other class, do something like this:

ParkingTicket blah;
ParkingTicket foo;
ParkingTicket bar;

ArrayList<ParkingTicket> allDaTickets = new ArrayList<>();
allDaTickets.add(blah);
allDaTickets.add(foo);
allDaTickets.add(bar);
ifly6
  • 2,517
  • 2
  • 19
  • 33
0

To Create an ArrayList of a certain class, you do: for example using the class 'Fruit' and calling the list 'fruits'.

List<Fruit> fruits = new ArrayList<>();

//to add an instance, to the above list, you use the add method
fruits.add(instance);

In your case, to create a list of ParkingTickets, it would be:

List<ParkingTicket> tickets = new ArrayList<>();
tickets.add(ticket); //ticket being an instance of ParkingTicket ofcourse

That being said, you code needs some refactoring. Your if condition should not be:

if(isParkingTimeExpired(car,meter) == true)

I'm assuming isParkingTimeExpired(car, meter) method already returns a boolean, so there is no need for the '==true' part in your condition checking. It would become something like this.

if(isParkingTimeExpired(car,meter)) { .... }

if the method returns true, it executes the block of the if statement.

nur-sh
  • 193
  • 10