Answers for "priority queue comparator java"

1

priority queue comparator java

Problem Statement

A shopping mall giving gift to highest purchased customer 
as Independence day offer.
Data stored is order_id, amount and name of customer.
Implement this program using PriorityQueue 
to display Details of customer who won the gift.

Example
Enter customer id, amount and name who purchased in mall(0 to stop):
1 275 ram
2 500 sham
3 350 monika
0
Details of customer who won gift on highest purchase:
orderId:2, orderAmount:500.0, customerName:sham

//write code here
import java.util.*;
class Priority_Queue{
    int id;
    int amount;
    String name;
    Priority_Queue(int i,int a,String n){
        id=i;
        amount=a;
        name=n;
    }
    public String toString(){
        return "orderId:"+id+", orderAmount:"+amount+".0, customerName:"+name;
    }
}

class sort_amount implements Comparator<Priority_Queue>{
    @Override
    public int compare(Priority_Queue o1, Priority_Queue o2){
        return o1.amount-o2.amount;
    }
}
public class Test{
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        ArrayList<Priority_Queue> pq=new ArrayList<>();
        System.out.println("Enter customer id, amount and name who purchased in mall(0 to stop):");
        for(int i=0;i<10;i++){
            int id=scan.nextInt();
            if(id==0){
                break;
            }
            int amount=scan.nextInt();
            String name=scan.next();
            pq.add(new Priority_Queue(id, amount, name));
        }
        Collections.sort(pq, new sort_amount());
        System.out.println("Details of customer who won gift on highest purchase:");
        System.out.println(pq.get(pq.size()-1));
    }
}
Posted by: Guest on August-23-2021

Code answers related to "priority queue comparator java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language