Add Elements from one class to one arraylist in another class

0

I have a class Ticket , which creates a ticket. and I have to make a CustomerService class with a ArrayList of Ticket type, which I called tickets and places all tickets created in this ArrayList.

Ticket class constructor:

   public Ticket(char attendanceType, int ticketNumber)
    {
     this.arriveTime = LocalDateTime.now();
     this.attendanceTime = null;
     this.attendanceType = validateAttendanceType(attendanceType);
     this.waitTime = 0;
     this.ticketNumber = validateTicketNumber(ticketNumber);
    }
    
asked by anonymous 12.12.2017 / 14:23

2 answers

0

If you need a CustomerService instance during the entire run of your application, I recommend following a Singleton pattern:

public class CustomerService {

    private static CustomerService INSTANCIA;

    private ArraList<Ticket> tickets;

    public static CustomerService getInstancia() {
        if (INSTANCIA == null) {
            INSTANCIA = new CustomerService();
            INSTANCIA.setTickets(new ArrayList<>());
        }

        return INSTANCIA;
    }

    public ArrayList<Ticket> getTickets() {
        return tickets;
    }

    public void setTickets(ArrayList<Ticket> tickets) {
        this.tickets = tickets;        
    }
    ...
}

From there, you need to run CustomerService.getInstancia().getTickets() to get the list of tickets and insert the object you want.

    
12.12.2017 / 14:53
0

According to what I noticed, your question is very simple, just create a method in the CustomerService class:

public class CustomerService {

private ArrayList<Tickets> tickets;

public CustomerService(){
    this.tickets = new ArrayList<>();
}

public void addTicket(Tickets t){
    tickets.add(t);
}
}
    
14.12.2017 / 00:36