Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arisupriatna14/74fb55f71e862161acff91ba0b0fb742 to your computer and use it in GitHub Desktop.
Save arisupriatna14/74fb55f71e862161acff91ba0b0fb742 to your computer and use it in GitHub Desktop.
// Author: Ari Supriatna
// Date: Sun, 26 March 2023 23:38
import java.util.ArrayList;
public class Product {
private String name = "";
private double price = 0.0;
private double shippingCost = 0.0;
private int quantity = 0;
private static final int MAX_QUANTITY_FOR_DISCOUNT = 50;
private static final int MAX_TOTAL_PRICE_FOR_DISCOUNT = 500;
private static final int MIN_QUANTITY_FOR_DISCOUNT = 10;
private static final int MIN_TOTAL_PRICE_FOR_DISCOUNT = 50;
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public double getShippingCost() {
return shippingCost;
}
public int getQuantity() {
return quantity;
}
public 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 = getQuantityDiscount();
double discount = calculateDiscount(quantityDiscount);
return calculateTotalCost(discount);
}
public String printCost() {
String output = "";
output += "Total cost for " + quantity + " " + name + "s is $" + getTotalCost() + "\n";
output += "Cost per product " + getTotalCost() / quantity + "\n";
output += "Savings per product " + ((price + shippingCost) - (getTotalCost() / quantity)) + "\n";
return output;
}
private boolean isEligibleForDiscount(int quantity, double price, int maxQuantity, int maxPrice) {
return (quantity > maxQuantity) || ((quantity * price) > maxPrice);
}
private double getQuantityDiscount() {
double quantityDiscount = 0.0;
if (isEligibleForDiscount(quantity, price, MAX_QUANTITY_FOR_DISCOUNT, MAX_TOTAL_PRICE_FOR_DISCOUNT)) {
quantityDiscount = .10;
} else if (isEligibleForDiscount(quantity, price, MAX_QUANTITY_FOR_DISCOUNT / 2,
MAX_TOTAL_PRICE_FOR_DISCOUNT / 5)) {
quantityDiscount = .07;
} else if (isEligibleForDiscount(quantity, price, MIN_QUANTITY_FOR_DISCOUNT, MIN_TOTAL_PRICE_FOR_DISCOUNT)) {
quantityDiscount = .05;
}
return quantityDiscount;
}
private double calculateDiscount(double quantityDiscount) {
return ((quantity - 1) * quantityDiscount) * price;
}
private double calculateTotalCost(double discount) {
return (quantity * price) + (quantity * shippingCost) - discount;
}
}
public class Store {
private ArrayList<Product> products = new ArrayList<Product>();
public void addProduct(Product newProduct) {
products.add(newProduct);
}
public void showProductDetails() {
for (Product product : products) {
System.out.println(product.printCost());
}
}
public static void main(String[] args) {
Store cornerStore = new Store();
cornerStore.addProduct(new Product("Pizza", 10.00, 1.00, 52));
cornerStore.addProduct(new Product("Pizza", 10.00, 1.00, 26));
cornerStore.addProduct(new Product("Pizza", 10.00, 1.00, 10));
cornerStore.showProductDetails();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment