Here are 2 interrelated classes, namely Product and Store, to calculate the discount and total cost of
Question:
Here are 2 interrelated classes, namely Product and Store, to calculate the discount and total cost of the pizza ordered:
a. There is a minimum of 4 smell code in the code below, please mention and explain your answer! b. D0 a refactor to fix the code smell!
Product.java public class Product { private String name = ""; private double price = 0.0; private double shippingCost = 0.0; private int quantity = 0; public String getName(){ return name; } public double getPrice(){ return price; } public double getShippingCost(){ return shippingCost; } public int getQuantity(){ return quantity; } Product(String name, double price, double shippingCost, int quantity){ this.name = name; this.price = price; this.shippingCost = shippingCost; this.quantity = quantity; } public double getTotalCost(){ double quantityDiscount = 0.0; if((quantity > 50) || ((quantity * price) > 500)) { quantityDiscount = .10; } else if((quantity > 25) || ((quantity * price) > 100)) { quantityDiscount = .07; } else if((quantity >= 10) || ((quantity * price) > 50)) { quantityDiscount = .05; } double discount = ((quantity - 1) * quantityDiscount) * price; return (quantity * price) + (quantity * shippingCost) - discount; } } |
Store.java import java.util.ArrayList; public class Store { public ArrayList theProducts = new ArrayList(); public void addAProduct(Product newProduct){ theProducts.add(newProduct); } public void getCostOfProducts(){ for(Product product : theProducts){ System.out.println("Total cost for " + product.getQuantity() + " " + product.getName() + "s is $" + product.getTotalCost()); System.out.println("Cost per product " + product.getTotalCost() / product.getQuantity()); System.out.println("Savings per product " + ((product.getPrice() + product.getShippingCost()) - (product.getTotalCost() / product.getQuantity())) + " "); } } public static void main(String[] args){ Store cornerStore = new Store(); cornerStore.addAProduct(new Product("Pizza", 10.00, 1.00, 52)); cornerStore.addAProduct(new Product("Pizza", 10.00, 1.00, 26)); cornerStore.addAProduct(new Product("Pizza", 10.00, 1.00, 10)); cornerStore.getCostOfProducts(); } } |
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill