Skip to content

Instantly share code, notes, and snippets.

@ketankhairnar
Created July 5, 2020 19:13
Show Gist options
  • Save ketankhairnar/69296512572fef4fa026ce0a8a6a4ed3 to your computer and use it in GitHub Desktop.
Save ketankhairnar/69296512572fef4fa026ce0a8a6a4ed3 to your computer and use it in GitHub Desktop.
package co.in.shoonya.abc.kata;
public class CdkShoppingCart {
public static void main(String[] args) {
System.out.println(billAmount(CustomerType.Regular, 15000d));
System.out.println(billAmount(CustomerType.Premium, 20000d));
}
private static double billAmount(CustomerType type, double input) {
double diff = 0d;
double disc = 0d;
double curr = input;
for (CustomerType.Range range : type.ranges) {
diff = input >= range.end ? range.end - range.start : curr;
disc += range.discount == 0d ? 0d : diff * range.discount / 100d;
curr = curr - diff;
}
return input - disc;
}
public enum CustomerType {
Regular(new Range(0, 5000d, 0d),
new Range(5000d, 10000d, 10d),
new Range(10000d, Double.MAX_VALUE, 20d)),
Premium(new Range(0, 4000d, 10d),
new Range(4000d, 8000d, 15d),
new Range(8000d, 12000d, 20d),
new Range(12000d, Double.MAX_VALUE, 30d));
Range[] ranges;
CustomerType(Range... ranges) {
this.ranges = ranges;
}
private static class Range {
double start;
double end;
double discount;
Range(double start, double end, double discount) {
this.start = start;
this.end = end;
this.discount = discount;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment