Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please update the Snack and Drink classes so that they implement the Comparable

ID: 3699319 • Letter: P

Question

Please update the Snack and Drink classes so that they implement the Comparable interface. Comparison is based on the name of the item and follows alphabetical ordering rules, ignoring case. Items with the same name should be ordered on price in ascending order. If two items have the same name and price, they are considered equal. Write a main method that tests your compareTo() method. Submit your updated class files.

Here are the drinks each drink cost $ 0.99 :

power-c", "Vitamin Water

vital-t", "Vitamin Water

XXX", "Vitamin Water

Original Coca-Cola

Vanilla Coca-Cola

Cherry Coca-Cola

public abstract class Snack extends Product{,  

private String productType = "snack";

private boolean isSnack = true;

public Snack() {

super();

}

public String getProductType() {

return productType;

}

public boolean isSnack() {

return isSnack;

}

public Snack(String name, String brand, String foodGroup, double price, double sugar, double protein, double sodium, double calories) {

super(name, brand, foodGroup, price, sugar, protein, sodium, calories);

}

//Copy Constructor

public Snack(Product productToCopy) {

super();

this.setName(productToCopy.getName());

this.setBrand(productToCopy.getBrand());

this.setFoodGroup(productToCopy.getFoodGroup());

this.setPrice(productToCopy.getPrice());

this.setSugar(productToCopy.getSugar());

this.setProtein(productToCopy.getProtein());

this.setSodium(productToCopy.getSodium());

this.setExpirationDate(productToCopy.getExpirationDate());

}

@Override

public String toString() {

StringBuilder resultStringBuilder = new StringBuilder();

resultStringBuilder.append(this.getName() + " ");

resultStringBuilder.append(this.getBrand() + " ");

resultStringBuilder.append(this.getPrice() + " ");

resultStringBuilder.append(this.getSugar() + " ");

resultStringBuilder.append(this.getProtein() + " ");

resultStringBuilder.append(this.getSodium() + " ");

resultStringBuilder.append(this.getCalories() + " ");

resultStringBuilder.append(this.getExpirationDate() + " ");

String result = resultStringBuilder.toString();

return result;

}

}

public class Drink extends Product{,

private String productType = "drink";

private boolean isDrink = true;

public Drink() {

// TODO Auto-generated constructor stub

super();

}

public String getProductType() {

return productType;

}

public boolean isDrink() {

return isDrink;

}

public Drink(String name, String brand, String foodGroup, double price, double sugar, double protein, double sodium, double calories) {

super(name, brand, foodGroup, price, sugar, protein, sodium, calories);

}

//Copy Constructor

public Drink(Product productToCopy) {

super();

this.setName(productToCopy.getName());

this.setBrand(productToCopy.getBrand());

this.setFoodGroup(productToCopy.getFoodGroup());

this.setPrice(productToCopy.getPrice());

this.setSugar(productToCopy.getSugar());

this.setProtein(productToCopy.getProtein());

this.setSodium(productToCopy.getSodium());

this.setExpirationDate(productToCopy.getExpirationDate());

}

@Override

public String toString() {

StringBuilder resultStringBuilder = new StringBuilder();

resultStringBuilder.append("Drink: " + " ");

resultStringBuilder.append(this.getName() + " ");

resultStringBuilder.append(this.getBrand() + " ");

resultStringBuilder.append(this.getPrice() + " ");

resultStringBuilder.append(this.getSugar() + " ");

resultStringBuilder.append(this.getProtein() + " ");

resultStringBuilder.append(this.getSodium() + " ");

resultStringBuilder.append(this.getCalories() + " ");

resultStringBuilder.append(this.getExpirationDate() + " ");

String result = resultStringBuilder.toString();

return result;

}

}

Explanation / Answer

Since both Drink and Snack extends Product class the best way to implements Comparable interface is, implementing Comparable in Product class. So i created Product class which is not given in the question but needed for buliding the code.

=====================

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DrinkTester {

public static void main(String[] args) {
  // TODO Auto-generated method stub
  //String name, String brand, String foodGroup, double price, double sugar, double protein, double sodium,double calories
  //Product snack = new Snack();
  Product drink1 = new Drink("power-c","Cocacola","cocalcola",15,4.5,3.2,1.1,45);
  Product drink2 = new Drink("power-B","Cocacola","cocalcola",35,4.5,3.2,1.1,45);
  Product drink3 = new Drink("power-A","Cocacola","cocalcola",2,4.5,3.2,1.1,45);
  Product drink4 = new Drink("power-A","Cocacola","cocalcola",200,4.5,3.2,1.1,45);
  Product drink5 = new Drink("Original","Cocacola","cocalcola",35,4.5,3.2,1.1,45);
  Product drink6 = new Drink("XXX","Cocacola","Vitamin Water",75,4.5,3.2,1.1,45);
  Product drink7 = new Drink("Cherry","Cocacola","cocalcola",23,4.5,3.2,1.1,45);
  List<Product> drinkList = new ArrayList<>();
  drinkList.add(drink1);
  drinkList.add(drink2);
  drinkList.add(drink3);
  drinkList.add(drink4);
  drinkList.add(drink5);
  drinkList.add(drink6);
  drinkList.add(drink7);
  System.out.println("Unsorted");
  for(Product p : drinkList) {
   System.out.println(p.getName() + " " + p.getPrice());
  }
  System.out.println(" Sorted");
  Collections.sort(drinkList);
  
  for(Product p : drinkList) {
   System.out.println(p.getName() + " " + p.getPrice());
  }
  
  Product snack1 = new Snack("snack-a","Cocacola","cocalcola",15,4.5,3.2,1.1,45);
  Product snack2 = new Snack("snack-b","Cocacola","cocalcola",9,4.5,3.2,1.1,45);
  Product snack3 = new Snack("Snack","Cocacola","cocalcola",17,4.5,3.2,1.1,45);
  Product snack4 = new Snack("One-snack","Cocacola","cocalcola",15,4.5,3.2,1.1,45);
  Product snack5 = new Snack("One-snack","Cocacola","cocalcola",7,4.5,3.2,1.1,45);
  List<Product> snackList = new ArrayList<>();
  snackList.add(snack1);
  snackList.add(snack2);
  snackList.add(snack3);
  snackList.add(snack4);
  snackList.add(snack5);
  System.out.println(" Unsorted");
  for(Product p : snackList) {
   System.out.println(p.getName() + " " + p.getPrice());
  }
  System.out.println(" Sorted");
  Collections.sort(snackList);
  
  for(Product p : snackList) {
   System.out.println(p.getName() + " " + p.getPrice());
  }
}

}

=========================

public class Product implements Comparable<Product>{

String name;
String brand;
String foodGroup;
double price;
double sugar;
double protein;
double sodium;
double calories;
String expirationDate;
public String getName() {
  return name;
}

public void setName(String name) {
  this.name = name;
}

public String getBrand() {
  return brand;
}

public void setBrand(String brand) {
  this.brand = brand;
}

public String getFoodGroup() {
  return foodGroup;
}

public void setFoodGroup(String foodGroup) {
  this.foodGroup = foodGroup;
}

public double getPrice() {
  return price;
}

public void setPrice(double price) {
  this.price = price;
}

public double getSugar() {
  return sugar;
}

public void setSugar(double sugar) {
  this.sugar = sugar;
}

public double getProtein() {
  return protein;
}

public void setProtein(double protein) {
  this.protein = protein;
}

public double getSodium() {
  return sodium;
}

public void setSodium(double sodium) {
  this.sodium = sodium;
}

public double getCalories() {
  return calories;
}

public void setCalories(double calories) {
  this.calories = calories;
}

public String getExpirationDate() {
  return expirationDate;
}

public void setExpirationDate(String expirationDate) {
  this.expirationDate = expirationDate;
}

public Product() {
  
}

public Product(String name, String brand, String foodGroup, double price, double sugar, double protein,
   double sodium, double calories) {
  // TODO Auto-generated constructor stub
  
  this.name = name;
  this.brand = brand;
  this.foodGroup = foodGroup;
  this.price = price;
  this.sugar = sugar;
  this.protein = protein;
  this.sodium = sodium;
  this.calories = calories;
}

@Override
public int compareTo(Product o) {
  // TODO Auto-generated method stub
  
  String thisname = this.name.toLowerCase();
  String>   if(thisname.compareTo(oname)==0) {
   if(this.price == o.price) {
    return 0;
   }else if(this.price < o.price) {
    return -1;
   }else if(this.price < o.price) {
    return 1;
   }
  }else if(thisname.compareTo(oname)<0) {
   return -1;
  }else if(thisname.compareTo(oname)>0){
   return 1;
  }
  return 0;
}

}

=============================

public class Drink extends Product {

private String productType = "drink";
private boolean isDrink = true;

public Drink() {
  // TODO Auto-generated constructor stub
  super();
}

public String getProductType() {
  return productType;
}

public boolean isDrink() {
  return isDrink;
}

public Drink(String name, String brand, String foodGroup, double price, double sugar, double protein, double sodium,
   double calories) {
  super(name, brand, foodGroup, price, sugar, protein, sodium, calories);
}

// Copy Constructor
public Drink(Product productToCopy) {
  super();
  this.setName(productToCopy.getName());
  this.setBrand(productToCopy.getBrand());
  this.setFoodGroup(productToCopy.getFoodGroup());
  this.setPrice(productToCopy.getPrice());
  this.setSugar(productToCopy.getSugar());
  this.setProtein(productToCopy.getProtein());
  this.setSodium(productToCopy.getSodium());
  this.setExpirationDate(productToCopy.getExpirationDate());
}

@Override
public String toString() {
  StringBuilder resultStringBuilder = new StringBuilder();
  resultStringBuilder.append("Drink: " + " ");
  resultStringBuilder.append(this.getName() + " ");
  resultStringBuilder.append(this.getBrand() + " ");
  resultStringBuilder.append(this.getPrice() + " ");
  resultStringBuilder.append(this.getSugar() + " ");
  resultStringBuilder.append(this.getProtein() + " ");
  resultStringBuilder.append(this.getSodium() + " ");
  resultStringBuilder.append(this.getCalories() + " ");
  resultStringBuilder.append(this.getExpirationDate() + " ");
  String result = resultStringBuilder.toString();
  return result;
}

}

========================

public class Snack extends Product {

private String productType = "snack";
private boolean isSnack = true;

public Snack() {
  super();
}

public String getProductType() {
  return productType;
}

public boolean isSnack() {
  return isSnack;
}

public Snack(String name, String brand, String foodGroup, double price, double sugar, double protein, double sodium,
   double calories) {
  super(name, brand, foodGroup, price, sugar, protein, sodium, calories);
}

// Copy Constructor
public Snack(Product productToCopy) {
  super();
  this.setName(productToCopy.getName());
  this.setBrand(productToCopy.getBrand());
  this.setFoodGroup(productToCopy.getFoodGroup());
  this.setPrice(productToCopy.getPrice());
  this.setSugar(productToCopy.getSugar());
  this.setProtein(productToCopy.getProtein());
  this.setSodium(productToCopy.getSodium());
  this.setExpirationDate(productToCopy.getExpirationDate());
}

@Override
public String toString() {
  StringBuilder resultStringBuilder = new StringBuilder();
  resultStringBuilder.append(this.getName() + " ");
  resultStringBuilder.append(this.getBrand() + " ");
  resultStringBuilder.append(this.getPrice() + " ");
  resultStringBuilder.append(this.getSugar() + " ");
  resultStringBuilder.append(this.getProtein() + " ");
  resultStringBuilder.append(this.getSodium() + " ");
  resultStringBuilder.append(this.getCalories() + " ");
  resultStringBuilder.append(this.getExpirationDate() + " ");
  String result = resultStringBuilder.toString();
  return result;
}
}

============================================

Output

Unsorted
power-c 15.0
power-B 35.0
power-A 2.0
power-A 200.0
Original 35.0
XXX 75.0
Cherry 23.0

Sorted
Cherry 23.0
Original 35.0
power-A 2.0
power-A 200.0
power-B 35.0
power-c 15.0
XXX 75.0

Unsorted
snack-a 15.0
snack-b 9.0
Snack 17.0
One-snack 15.0
One-snack 7.0

Sorted
One-snack 7.0
One-snack 15.0
Snack 17.0
snack-a 15.0
snack-b 9.0