0% found this document useful (0 votes)
30 views

Java Assignment

This document defines three classes - PercentageDiscount, ThresholdDiscount, and BestForCustomer - that implement a Discountable interface and calculate discounted prices differently. PercentageDiscount applies a percentage discount to the original price, ThresholdDiscount applies a fixed discount if the price exceeds a threshold, and BestForCustomer returns the better of a percentage discount or threshold discount on a given price. Main instantiates objects of each class and prints the discounted prices for a sample price of 100.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Java Assignment

This document defines three classes - PercentageDiscount, ThresholdDiscount, and BestForCustomer - that implement a Discountable interface and calculate discounted prices differently. PercentageDiscount applies a percentage discount to the original price, ThresholdDiscount applies a fixed discount if the price exceeds a threshold, and BestForCustomer returns the better of a percentage discount or threshold discount on a given price. Main instantiates objects of each class and prints the discounted prices for a sample price of 100.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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));
}
}

You might also like