I'm doing a Java comparator exercise which ask me to sort products by whether or not they are in stock, then in ascending order by their price. For more detail instruction, please read the comment in PriceStockComparator class.
import java.util.Comparator;
import java.util.Collections;
public class Product {
private String name;
private int priceCents;
private int stockLevel;
public Product(String name, int priceCents, int stockLevel) {
this.name = name;
this.priceCents = priceCents;
this.stockLevel = stockLevel;
}
@Override
public String toString() {
return this.name ": " this.stockLevel " @ $" this.priceCents / 100.0;
}
/**
* This comparator should sort products by whether or not they are in stock,
* then in ascending order by their price.
*
* After sorting, products that have a positive stock level should appear
* before products that have a stock level of 0. If two products being
* sorted are either 1) both in stock; or 2) both out of stock, they
* should then be sorted based on their price in ascending order.
*/
static class PriceStockComparator implements Comparator<Product> {
@Override
public int compare(Product p1, Product p2) {
// write your code here
return Integer.compare(p1.stockLevel, p2.stockLevel);
}
}
}
I got 4 error on my program
=> java.lang.AssertionError: expected:<[Apple: 5 @ $1.0, Bottled Water: 2 @ $2.0, Donut: 7 @ $4.0]> but was:<[Bottled Water: 2 @ $2.0, Apple: 5 @ $1.0, Donut: 7 @ $4.0]>
=> java.lang.AssertionError: expected:<[Apple: 5 @ $1.0, Bottled Water: 2 @ $2.0, Donut: 7 @ $4.0, Fish: 3 @ $6.0, Crackers: 0 @ $3.0, Eggs: 0 @ $5.0]> but was:<[Crackers: 0 @ $3.0, Eggs: 0 @ $5.0, Bottled Water: 2 @ $2.0, Fish: 3 @ $6.0, Apple: 5 @ $1.0, Donut: 7 @ $4.0]>
=> java.lang.AssertionError: expected:<[Crackers: 0 @ $3.0, Eggs: 0 @ $5.0]> but was:<[Eggs: 0 @ $5.0, Crackers: 0 @ $3.0]>
=> java.lang.AssertionError: expected:<[Donut: 7 @ $4.0, Fish: 3 @ $6.0, Eggs: 0 @ $5.0]> but was:<[Eggs: 0 @ $5.0, Fish: 3 @ $6.0, Donut: 7 @ $4.0]>
That's because I sort the in stock level but I can't make it positive first and I didn't sort the ascending order in price. I don't think I could use two compareTo() here and I have the intention to use Collection.sort
in sorting the ascending order in price. But I don't know how to do that. I would be nice if someone can help. Thank you!
CodePudding user response:
You can write your compareTo method as follows:
public int compare(Product p1, Product p2) {
//If both the products are in stock or are not in stock
// we can compare them on price
if(p1.getStockLevel()>0 && p2.getStockLevel()>0 ||
p1.getStockLevel()<=0 && p2.getStockLevel()<=0)
return Integer.compare(p1.getPriceCents(), p2.getPriceCents());
// If the first product is not in stock then we move it down the list
// by returning a -1
else if(p1.getStockLevel()>0 && p2.getStockLevel()<=0)
return -1;
// If second product is not in stock but first one
else
return 1;
}
CodePudding user response:
You could use Java 8 lambda expressions. The compiler is capable of inferring type definitions. If your object Product is in a list what you could do is as follows...
List<Product> products = new ArrayList<>();
Collections.sort(products, (a,b) -> a.priceCents - b.priceCents);
If you'd like a reference, Baeldung has a good page with some examples you could pull from.