interface Discountable {
double discountedPrice(double price);
}
class PercentageDiscount implements Discountable {
private double percentage;
public PercentageDiscount(double percentage) {
this.percentage = percentage;
}
public double getPercentage() {
return percentage;
}
public void setPercentage(double percentage) {
this.percentage = percentage;
}
@Override
public double discountedPrice(double price) {
return price * (1 - percentage / 100);
}
}
class ThresholdDiscount implements Discountable {
private double threshold;
private double discount;
public ThresholdDiscount(double threshold, double discount) {
this.threshold = threshold;
this.discount = discount;
}
public double getThreshold() {
return threshold;
}
public double getDiscount() {
return discount;
}
public void setThreshold(double threshold) {
this.threshold = threshold;
}
public void setDiscount(double discount) {
this.discount = discount;
}
@Override
public double discountedPrice(double price) {
if (price >= threshold) {
return price - discount;
} else {
return price;
}
}
}
class BestForCustomer implements Discountable {
private double percentage;
private double threshold;
private double discount;
public BestForCustomer(double percentage, double threshold, double discount) {
this.percentage = percentage;
this.threshold = threshold;
this.discount = discount;
}
public double getPercentage() {
return percentage;
}
public double getThreshold() {
return threshold;
}
public double getDiscount() {
return discount;
}
public void setPercentage(double percentage) {
this.percentage = percentage;
}
public void setThreshold(double threshold) {
this.threshold = threshold;
}
public void setDiscount(double discount) {
this.discount = discount;
}
@Override
public double discountedPrice(double price) {
double percentageDiscountedPrice = price * (1 - percentage / 100);
if (price >= threshold) {
return Math.min(percentageDiscountedPrice, price - discount);
} else {
return percentageDiscountedPrice;
}
}
}
public class Main {
public static void main(String[] args) {
PercentageDiscount percentageDiscount = new PercentageDiscount(10);
ThresholdDiscount thresholdDiscount = new ThresholdDiscount(50, 5);
BestForCustomer bestForCustomer = new BestForCustomer(10, 50, 5);
double price = 100;
System.out.println("Percentage Discounted Price: " +
percentageDiscount.discountedPrice(price));
System.out.println("Threshold Discounted Price: " +
thresholdDiscount.discountedPrice(price));
System.out.println("Best For Customer Price: " +
bestForCustomer.discountedPrice(price));
}
}