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
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 cent each:
Drink 1. power-c", "Vitamin Water
Drink 2. vital-t", "Vitamin Water
Drink 3. XXX", "Vitamin Water
Drink 4. XXX", "Vitamin Water
Drink 5. XXX", "Vitamin Water
Drink 6. XXX", "Vitamin Water
Drink 7. XXX", "Vitamin Water
Drink 8. Original Coca-Cola
Drink 9. Vanilla Coca-Cola
Drink 10. Cherry Coca-Cola
Product code:
import java.time.LocalDate; | |
import java.time.ZoneId; | |
import java.util.Date; | |
public abstract class Product implements Comparable | |
//Shared Properties | |
private String name; | |
private String brand; | |
private String foodGroup; | |
private double price; | |
//Nutrition Facts | |
private double sugar; | |
private double protein; | |
private double sodium; | |
private double calories; | |
//Expiration Date - format mm/dd/yyyy | |
private String expirationDate; | |
//Default Constructor | |
public Product() { | |
this.name = null; | |
this.brand = null; | |
this.foodGroup = null; | |
this.price = 0.0; | |
this.sugar = 0.0; | |
this.protein = 0.0; | |
this.sodium = 0.0; | |
this.calories = 0.0; | |
this.setExpirationDate(calculateExpirationDate()); | |
} | |
//Parameterized Constructor | |
public Product(String name, String brand, String foodGroup, double price, double sugar, double protein, double sodium, double calories) { | |
this.name = name; | |
this.brand = brand; | |
this.foodGroup = foodGroup; | |
this.price = price; | |
this.sugar = sugar; | |
this.protein = protein; | |
this.sodium = sodium; | |
this.calories = calories; | |
this.setExpirationDate(calculateExpirationDate()); | |
} | |
//Copy Constructor | |
public Product(Product productToCopy) { | |
this.name = productToCopy.getName(); | |
this.brand = productToCopy.getBrand(); | |
this.foodGroup = productToCopy.getFoodGroup(); | |
this.price = productToCopy.getPrice(); | |
this.sugar = productToCopy.getSugar(); | |
this.protein = productToCopy.getProtein(); | |
this.sodium = productToCopy.getSodium(); | |
this.calories = productToCopy.getCalories(); | |
this.setExpirationDate(calculateExpirationDate()); | |
} | |
public abstract String toString(); | |
//Temporary compares the name of the products, returns 1 if equal or -1 if not. Will be needed later. | |
@Override | |
public int compareTo(Product other) { | |
int result = 0; | |
if(this.name.equals(other.name)) { | |
result = 1; | |
} | |
else { | |
result = -1; | |
} | |
return result; | |
} | |
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 String calculateExpirationDate() { | |
Date today = new Date(); | |
LocalDate localDate = today.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); | |
int year = localDate.getYear(); | |
int month = localDate.getMonthValue(); | |
int day = localDate.getDayOfMonth(); | |
//For Drinks, add 9 months | |
if(this.getClass().getSimpleName().equals("Drink")) | |
month += 9; | |
//For Chips, add 3 months | |
if(this.getClass().getSimpleName().equals("Chips")) | |
month += 3; | |
//For Candy, add 6 months | |
if(this.getClass().getSimpleName().equals("Candy")) | |
month += 6; | |
//For Gum, add 1 year | |
if(this.getClass().getSimpleName().equals("Drink")) | |
year += 1; | |
String expDate = month + "/" + day+ "/" + year; | |
return expDate; | |
} | |
} |
Snack code:
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; | |
} | |
} |
Drink code:
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; | |
} | |
} |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started