package pr01; public class GroceryProduct { private String name; private double pricePerPound; private double discountRate; private double startingDiscountWeight; public GroceryProduct(String name, double pricePerPound, double discountRate, double startingDiscountWeight) { this.name = name; this.pricePerPound = pricePerPound; this.discountRate = discountRate; this.startingDiscountWeight = startingDiscountWeight; } public String getName() { return name; } public double getPricePerPound() { return pricePerPound; } public double getDiscountRate() { return discountRate; } public double getStartingDiscountWeight() { return startingDiscountWeight; } public double getNewSellingPrice(double weight) { double price; double discount = 0; double nextDiscount = discountRate; double discountWeight = startingDiscountWeight; while (weight >= discountWeight) { discount += nextDiscount; nextDiscount = nextDiscount / 2; discountWeight *= 2; } price = pricePerPound * weight; price = price - discount * price; return price; } public double getSellingPrice(double weight) { double price = pricePerPound * weight; if (weight >= startingDiscountWeight) price = price - discountRate * price; return price; } public String toString() { return name + ", price per pound: $" + pricePerPound + ", discount: " + discountRate + " starting at " + startingDiscountWeight + " pounds"; } }