priority queue comparator java

Solutions on MaxInterview for priority queue comparator java by the best coders in the world

showing results for - "priority queue comparator java"
Lilya
13 Jun 2019
1Problem Statement
2
3A shopping mall giving gift to highest purchased customer 
4as Independence day offer.
5Data stored is order_id, amount and name of customer.
6Implement this program using PriorityQueue 
7to display Details of customer who won the gift.
8
9Example
10Enter customer id, amount and name who purchased in mall(0 to stop):
111 275 ram
122 500 sham
133 350 monika
140
15Details of customer who won gift on highest purchase:
16orderId:2, orderAmount:500.0, customerName:sham
17
18//write code here
19import java.util.*;
20class Priority_Queue{
21    int id;
22    int amount;
23    String name;
24    Priority_Queue(int i,int a,String n){
25        id=i;
26        amount=a;
27        name=n;
28    }
29    public String toString(){
30        return "orderId:"+id+", orderAmount:"+amount+".0, customerName:"+name;
31    }
32}
33
34class sort_amount implements Comparator<Priority_Queue>{
35    @Override
36    public int compare(Priority_Queue o1, Priority_Queue o2){
37        return o1.amount-o2.amount;
38    }
39}
40public class Test{
41    public static void main(String[] args){
42        Scanner scan=new Scanner(System.in);
43        ArrayList<Priority_Queue> pq=new ArrayList<>();
44        System.out.println("Enter customer id, amount and name who purchased in mall(0 to stop):");
45        for(int i=0;i<10;i++){
46            int id=scan.nextInt();
47            if(id==0){
48                break;
49            }
50            int amount=scan.nextInt();
51            String name=scan.next();
52            pq.add(new Priority_Queue(id, amount, name));
53        }
54        Collections.sort(pq, new sort_amount());
55        System.out.println("Details of customer who won gift on highest purchase:");
56        System.out.println(pq.get(pq.size()-1));
57    }
58}