Please solve this problem in java.
import java.util.Arrays;
public class PriorityQueue {
/* This class is finished for you.
*/
private static class Customer implements Comparable {
private double donation;
public Customer(double donation) {
this.donation = donation;
}
public double getDonation() {
return donation;
}
public void donate(double amount) {
donation += amount;
}
public int compareTo(Customer other) {
double diff = donation - other.donation;
if (diff < 0) {
return -1;
} else if (diff > 0) {
return 1;
} else {
return 0;
}
}
public String toString() {
return String.format("$%.2f", donation);
}
}
private Customer[] data;
private int size;
public PriorityQueue(int capacity) {
data = new Customer[capacity];
size = 0;
}
public PriorityQueue() {
this(10);
}
/* Add a customer to the queue.
* Remember to do so in a way that keeps the queue in sorted
order!
*/
public void push(Customer customer) {
}
/* Remove and return the highest priority customer from the
queue.
*/
public Customer pop() {
return null;
}
/* Return, but don't remove, the highest priority item from the
queue.
*/
public Customer peek() {
return null;
}
/* Given the index of a customer, increase their donation
amount, letting
* them cut in line if necessary.
*
* Refer to the Customer class to remind yourself the operations you
can do
* on a customer.
*/
public void bump(int customerIndex, double amount) {
}
public String toString() {
return Arrays.toString(data);
}
public static void main(String[] args) {
PriorityQueue line = new PriorityQueue(6);
line.push(new Customer(5.00));
line.push(new Customer(10.00));
line.push(new Customer(1.00));
System.out.println(line.pop());
System.out.println(line.pop());
line.push(new Customer(20.00));
line.push(new Customer(15.00));
line.push(new Customer(2.00));
line.bump(1, 30.00);
line.bump(3, 60.00);
System.out.println(line.pop());
System.out.println(line.peek());
System.out.println(line.pop());
System.out.println(line.pop());
System.out.println(line.pop());
}
}
RESULT
$10.00 $5.00 $61.00 $45.00 $45.00 $20.00 $2.00
import java.util.Arrays;
public class PriorityQueue {
/* This class is finished for you.
*/
private static class Customer implements Comparable<Customer>
{
private double donation;
public Customer(double donation) {
this.donation = donation;
}
public double getDonation() {
return donation;
}
public void donate(double amount) {
donation += amount;
}
public int compareTo(Customer other) {
double diff = donation - other.donation;
if (diff < 0) {
return -1;
} else if (diff > 0) {
return 1;
} else {
return 0;
}
}
// If following changes can be made in compareTo() function then reversing of an array is not required
// public int compareTo(Customer other) {
// double diff = donation - other.donation;
// if (diff < 0) {
// return 1;
// } else if (diff > 0) {
// return -1;
// } else {
// return 0;
// }
// }
public String toString() {
return String.format("$%.2f", donation);
}
}
private Customer[] data;
private int size;
public PriorityQueue (int capacity) {
data = new Customer[capacity];
size = 0;
}
public PriorityQueue () {
this(10);
}
public void push(Customer customer) {
// Pushed data in queue and sorted the queue
data[size]=customer;
// After pushing each element the size is increased
size++;
// Arrays sorted in ascending order due to the given compareTo()
function and
// so the array is reversed
Arrays.sort(data,0,size);
int i; Customer t;
for (i = 0; i < size / 2; i++) {
t = data[i];
data[i] = data[size - i - 1];
data[size - i - 1] = t;
}
}
public Customer pop() {
Customer cust=data[0];
// Shifting all the elements after popping the top element and
decreasing the size
for(int i=0;i<size-1;i++)
data[i]=data[i+1];
size--;
return cust;
}
public Customer peek() {
return data[0];
}
public void bump(int customerIndex, double amount) {
// Called function donate in customer class to further
// add the amount
data[customerIndex].donate(amount);
// Now the value has changed of an element hence, may not be
sorted
// So sorted again and then reversed
Arrays.sort(data,0,size);
int i; Customer t;
for (i = 0; i < size / 2; i++) {
t = data[i];
data[i] = data[size - i - 1];
data[size - i - 1] = t;
}
}
public String toString() {
return Arrays.toString(data);
}
public static void main(String[] args) {
PriorityQueue line = new PriorityQueue (6);
line.push(new Customer(5.00));
line.push(new Customer(10.00));
line.push(new Customer(1.00));
System.out.println(line.pop());
System.out.println(line.pop());
line.push(new Customer(20.00));
line.push(new Customer(15.00));
line.push(new Customer(2.00));
line.bump(1, 30.00);
line.bump(3, 60.00);
System.out.println(line.pop());
System.out.println(line.peek());
System.out.println(line.pop());
System.out.println(line.pop());
System.out.println(line.pop());
}
}
Get Answers For Free
Most questions answered within 1 hours.