Skip to content

Instantly share code, notes, and snippets.

@kingRayhan
Created January 10, 2021 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingRayhan/41cf09b5a2186534eb046d28669aaadd to your computer and use it in GitHub Desktop.
Save kingRayhan/41cf09b5a2186534eb046d28669aaadd to your computer and use it in GitHub Desktop.
package com.company;
public interface Discountable {
public double discountedPrice(double price);
}
package com.company;
class Main {
public static void main(String[] args) {
PercentageDiscount pd = new PercentageDiscount(20);
ThresholdDiscount td = new ThresholdDiscount(20, 10);
double perDiscount = pd.discountedPrice(200);
double threshold = td.discountedPrice(200);
System.out.println(perDiscount);
System.out.println(threshold);
}
}
package com.company;
public 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 - (price*percentage)/100);
}
}
package com.company;
public 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 void setThreshold(double threshold) {
this.threshold = threshold;
}
public double getDiscount() {
return discount;
}
public void setDiscount(double discount) {
this.discount = discount;
}
@Override
public double discountedPrice(double price) {
return this.discount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment