Created
January 10, 2021 07:25
-
-
Save kingRayhan/41cf09b5a2186534eb046d28669aaadd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
public interface Discountable { | |
public double discountedPrice(double price); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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