Page 1 of 1

Instructions are in the code, all edits are in second code public class ShoppingItem implements Comparable

Posted: Sun May 15, 2022 12:43 pm
by answerhappygod
Instructions are in the code, all edits are in second code
public class ShoppingItem implements
Comparable<ShoppingItem> {
private int dollars;
private int cents;
private double rating;
private String category;
private String name;


// constructors directly set values for ease of
reading
// in general, don't emulate this style

public ShoppingItem(int dollars, int cents,
double rating, String category, String name) {
this.dollars = dollars;
this.cents = cents;
this.rating = rating;
this.category =
category;
this.name = name;
}

public ShoppingItem() {
this.dollars = 0;
this.cents = 0;
this.rating = 0.0;
this.category = "";
this.name = "";
}
// setters have no error checking for ease of
reading
// in general, don't emulate this style

public void setDollars(int dollars) {
this.dollars = dollars;
}
public void setCents(int cents) {
this.cents = cents;
}
public void setRating(double rating) {
this.rating = rating;
}
public void setCategory(String category)
{
this.category =
category;
}
public void setName(String name) {
this.name = name;
}
public int getDollars() {
return dollars;
}
public int getCents() {
return cents;
}
public double getRating() {
return rating;
}
public String getCategory() {
return category;
}
public String getName() {
return name;
}
// uses an abbreviated format to make it easier
to
// write code for this lab. Don't emulate
this style
// in general
public String toString() {
String fmt = "%3d.%02d (%.1f
rating) %-10s %s";
String str =
String.format(fmt, dollars, cents, rating, category, name);
return str;
}
// compareTo is implemented only to ensure the
other code
// files do not crash when run. Don't
emulate this style.

@Override
public int compareTo(ShoppingItem si) {
return 0;
}
}
import java.util.ArrayList;
import java.util.Comparator;
public class RatPerCat {
public static void main(String[] args) {
ArrayList<ShoppingItem> list = new
ArrayList<ShoppingItem>();
list.add(new ShoppingItem(0,
99, 4.1, "Games", "20-sided die"));
list.add(new ShoppingItem(29,
99, 4.8, "Games", "Baseball mitt"));
list.add(new
ShoppingItem(299, 0, 4.4, "Games", "Foosball Table"));
list.add(new
ShoppingItem(299, 0, 4.5, "Candy", "Trident Peppermint
1000-pack"));
list.add(new ShoppingItem(2,
99, 4.7, "Candy", "Trident Peppermint 10-pack"));
list.add(new ShoppingItem(1,
99, 4.1, "Candy", "Trident Peppermint 6-pack"));

print("Original List",
list);


Comparator<ShoppingItem> comp = categoryRating();
list.sort(comp);

print("Sorted List",
list);
}
// return a Comparator object that facilitates
sorting first by
// category (a-z), then by rating (high to
low)
public static Comparator<ShoppingItem>
categoryRating() {
return null; // delete this line
}
public static void print(String header,
ArrayList<ShoppingItem> list) {
System.out.println("\n" +
header);
for (ShoppingItem item :
list) {

System.out.println(item);
}
}
}