ParkingTicket

Here are the relevant attributes:

  • ticket number of type String
  • the name of the police officer who issued the ticket
  • the police officer's badge number
  • the car's license number the car's make
  • the car's model
  • amount of the fine

Note that the ticket number must be unique, created by a private method createTicketNumber() that is called from the constructor. The ticketNumber must be set when the object is created and not allowed to change. For example the first ticket will have the number “V1001”, the second ticket will have the number “V1002” etc. (Hint: use a static variable to hold the number part of the ticket number and increment it in the method that creates the ticket number).

1

There are 1 answers

3
Anand S Kumar On BEST ANSWER

Let me give a java example in your case, assuming your program will run forever (that is you do not need to remember the latest ticket number across restarts) , you can use the static variable to store the last ticket number issued and then declare ticketNumber as a private property of the class and then only create the getTicketNumber() function (getter of that property) so that no one can set it from outside the class, and you can set it in your constructor.

Example -

class Ticket {
    private static long lastTicketNumber = 0L;
    private String ticketNumber;
    public String getTicketNumber() {
        return ticketNumber;
    }

    public Ticket() {
        ticketNumber = createTicketNumber();
    }

    private String createTicketNumber() {
        lastTicketNumber = lastTicketNumber + 1;
        return "V" + (lastTicketNumber);
    }

}