Question 01

Add Logging to Monitor Ticket Operations Question: Modify the Logger class to log all ticket operations (addTickets and removeTicket) in a standardized format that includes: • Ticket ID • Operation type (add/remove) • Operation timestampUpdate the TicketPool class to implement logging for all ticket operations, then write a test scenario to verify the logs.

Answer

1. Modify the Logger Class

public static void log(String operation, String ticketId) {
    String timeStampedMessage = ticketId + " : " + operation + " : " + LocalDateTime.now();
    System.out.println(timeStampedMessage);
}

2. Modify the TicketPool Class

@Override
public synchronized void addTickets(String ticket) {
    logger.log("add", ticket);
    tickets.add(ticket);
}
@Override
public synchronized String removeTicket() {
    if (tickets.isEmpty()) {
        return null;
    } else {
        String removedTicket = tickets.remove(0);
        logger.log("remove", removedTicket);
        return removedTicket;
    }
}

3. Test Case

Write the following test case to verify the changes:

public class TicketPoolTest {
    public static void main(String[] args) {
        TicketPool ticketPool = new TicketPool();

        // Adding tickets
        ticketPool.addTickets("Ticket1");
        ticketPool.addTickets("Ticket2");

        // Removing tickets
        ticketPool.removeTicket();
        ticketPool.removeTicket();

        // Attempting to remove from an empty pool
        ticketPool.removeTicket();
    }
}

Question 2

Refactor the system to allow customers to retrieve tickets either: