2017-12-19 4 views
0

Event 클래스와 Venue 클래스를 만들어야합니다.Java : 메서드 인수로 큐를 전달 하시겠습니까?

Venue 클래스에서 우선 순위 대기열을 넣어야합니다. 큐에서 이벤트를 제거하고 표시하는 방법을 작성하고 간단한 통계를 표시하는 방법을 작성해야합니다. 모든 이벤트에 대한 사람들의 평균 등.

나는 첫 번째 점에 붙어 있습니다. 이 이벤트를 보여줍니다. 전체 큐를 메소드의 인수로 전달할 수 있습니까? - 해보려고했지만 작동하지 않는 것 같습니다. - (표시 방법은 Event 클래스).

public class Event { 

    private String name; 
    private int time; 
    private int numberOfParticipants; 

    public Event(String name, int time, int numberOfParticipants) { 
     this.name = name; 
     this.time = time; 
     this.numberOfParticipants = numberOfParticipants; 
    } 

    /**Getters and setters omitted**/ 

    @Override 
    public String toString() { 
     return "Wydarzenie{" + 
       "name='" + name + '\'' + 
       ", time=" + time + 
       ", numberOfParticipants=" + numberOfParticipants + 
       '}'; 
    } 

    public void display(PriorityQueue<Event> e){ 
     while (!e.isEmpty()){ 
      System.out.println(e.remove()); 
     } 
    } 
} 

장소 클래스 : 여기

public class Venue { 
    public static void main(String[] args) { 
     PriorityQueue<Event> pq = new PriorityQueue<>(Comparator.comparing(Event::getTime)); 
     pq.add(new Event("stand up", 90, 200)); 
     pq.add(new Event("rock concert", 120, 150)); 
     pq.add(new Event("theatre play", 60, 120)); 
     pq.add(new Event("street performance", 70, 80)); 
     pq.add(new Event("movie", 100, 55)); 
    } 
} 
+0

예, 컬렉션에 인수를 전달할 수 있습니다. 시도 할 때 어떤 문제가 보이나요? – sprinter

+0

예. 메서드에서 컬렉션을 전달할 수 있습니다. 다른 것들은 ... Venue 클래스에서만 큐에 있어야한다고 생각합니다. Venue 클래스에 메소드를 추가하여 이벤트를 표시하고 이벤트 및 기타 사항을 추가 할 수 있습니다. 메인에서는 display (pq)를 호출 할 수 있습니다. 또한 .. 나는 표시 할 각 항목을 제거하는 것이 적절한 지 확신하지 못합니다. – clinomaniac

답변

0

는 장소 클래스에 약간의 변화이다.

class Venue { 
    PriorityQueue<Event> pq = new PriorityQueue<Event>(Comparator.comparing(Event::getTime)); 

    public static void main(String[] args) { 
     Venue v = new Venue(); 
     v.addEvents(); 
     v.display(v.pq); 
    } 

    private void addEvents() { 
     pq.add(new Event("stand up", 90, 200)); 
     pq.add(new Event("rock concert", 120, 150)); 
     pq.add(new Event("theatre play", 60, 120)); 
     pq.add(new Event("street performance", 70, 80)); 
     pq.add(new Event("movie", 100, 55)); 
    } 

    private void display(PriorityQueue<Event> e) { 
     while (!e.isEmpty()) { 
      System.out.println(e.remove()); 
     } 
    } 
} 

대기열은 클래스 수준이므로 각 Venue는 자체 대기열을 가질 수 있습니다. main 메서드는 다른 메서드를 호출하지만 이상적으로는 다른 클래스에 배치해야합니다. 디스플레이는 Venue 인스턴스에서 호출되며 큐에서 각 항목을 제거하는 동안 해당 메소드에서 통계를 수행 할 수 있습니다.

관련 문제