Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Update the snack and drink classes with a compareTo() method the items they need to implement the c omparable interface in alphabetical order by name

Update the snack and drink classes with a compareTo() method the items they need to implement the comparable interface in alphabetical order by name then by their price in the compareTo() method. 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.

Example:

if ((this.Cola).equals(other.Tea)) {

return - 0;

return = 0;

int compare = a.compareTo(b); if (compare < 0) { //a is smaller } else if (compare > 0) { //a is larger } else { //a is equal to b }

____________________

import java.time.LocalDate; import java.time.ZoneId; import java.util.Date; public 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 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; } @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; } }

public class Snack extends Product implements Comparable { private String productType = "snack"; private boolean isSnack = true; public Snack() { super(); } public static void main(String[] args) { Product p1 = new Product("Drink 1", "power-c", "Vitamin Water", 0.99, 12.32, 23, 34, 3); System.out.println(p1); Product p2 = new Product("Drink 2", "vital-t", "Vitamin water", 0.99, 12.32, 23, 34, 3); System.out.println(p2); System.out.println("p1.compareTo(p2) : " + p1.compareTo(p2)); System.out.println("p2.compareTo(p2) : " + p2.compareTo(p2)); Snack s1 = new Snack(p1); Snack s2 = new Snack(p2); System.out.println("s1.compareTo(s2) : " + s1.compareTo(s2)); System.out.println("s2.compareTo(s2) : " + s2.compareTo(s2)); } 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 implements Comparable { private String productType = "drink"; private boolean isDrink = true; public Drink() { // TODO Auto-generated constructor stub super(); } public static void main(String[] args) { Product p1 = new Product("Drink 1", "power-c", "Vitamin Water", 0.99, 12.32, 23, 34, 3); System.out.println(p1); Product p2 = new Product("Drink 2", "vital-t", "Vitamin water", 0.99, 12.32, 23, 34, 3); System.out.println(p2); System.out.println("p1.compareTo(p2) : " + p1.compareTo(p2)); System.out.println("p2.compareTo(p2) : " + p2.compareTo(p2)); Drink d1 = new Drink(p1); Drink d2 = new Drink(p2); System.out.println("d1.compareTo(d2) : " + d1.compareTo(d2)); System.out.println("d2.compareTo(d2) : " + d2.compareTo(d2)); } 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2017 For Windows Web And Database Applications

Authors: Corinne Hoisington

1st Edition

1337102113, 978-1337102117

More Books

Students also viewed these Databases questions