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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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);
}
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply