diff --git a/src/chain-of-responsability/discounts-calculator/Calculator.ts b/src/chain-of-responsability/discounts-calculator/Calculator.ts index 0cc016c..ae53a11 100644 --- a/src/chain-of-responsability/discounts-calculator/Calculator.ts +++ b/src/chain-of-responsability/discounts-calculator/Calculator.ts @@ -1,20 +1,19 @@ import Budget from "./Budget"; +import DiscountMoreThanTwoItems from "./discounts/DiscountMoreThanTwoItems"; +import DiscountHigherThan500 from "./discounts/DiscountHigherThan500"; +import NoDiscount from "./discounts/NoDiscount"; export default class Calculator { constructor(private budged: Budget) {} calculate(): number { - const total: number = this.budged.getTotal(); - let discount = 0; - // discount more than 2 items - if (this.budged.getItems().length > 2) { - discount += total * 0.1; - } + const moreThantwo = new DiscountMoreThanTwoItems(); + const higherThan500 = new DiscountHigherThan500(); + const nodiscount = new NoDiscount(); - if (total > 500) { - discount += total * 0.07; - } + moreThantwo.setNext(higherThan500); + higherThan500.setNext(nodiscount); - return total - discount; + return this.budged.getTotal() - moreThantwo.calculate(this.budged); } } diff --git a/src/chain-of-responsability/discounts-calculator/discounts/Discount.ts b/src/chain-of-responsability/discounts-calculator/discounts/Discount.ts new file mode 100644 index 0000000..90efa03 --- /dev/null +++ b/src/chain-of-responsability/discounts-calculator/discounts/Discount.ts @@ -0,0 +1,6 @@ +import Budget from "../Budget"; + +export default interface Discount { + calculate(budget: Budget): number; + setNext(discount: Discount): void; +} diff --git a/src/chain-of-responsability/discounts-calculator/discounts/DiscountHigherThan500.ts b/src/chain-of-responsability/discounts-calculator/discounts/DiscountHigherThan500.ts new file mode 100644 index 0000000..a743ce5 --- /dev/null +++ b/src/chain-of-responsability/discounts-calculator/discounts/DiscountHigherThan500.ts @@ -0,0 +1,17 @@ +import Discount from "./Discount"; +import Budget from "../Budget"; + +export default class DiscountHigherThan500 implements Discount { + private next!: Discount; + calculate(budget: Budget): number { + if (budget.getTotal() > 500) { + console.log("Disconto aplicado foi DiscountHigherThan500"); + return budget.getTotal() * 0.15; + } else { + return this.next.calculate(budget); + } + } + setNext(next: Discount): void { + this.next = next; + } +} diff --git a/src/chain-of-responsability/discounts-calculator/discounts/DiscountMoreThanTwoItems.ts b/src/chain-of-responsability/discounts-calculator/discounts/DiscountMoreThanTwoItems.ts new file mode 100644 index 0000000..0cf75af --- /dev/null +++ b/src/chain-of-responsability/discounts-calculator/discounts/DiscountMoreThanTwoItems.ts @@ -0,0 +1,17 @@ +import Discount from "./Discount"; +import Budget from "../Budget"; + +export default class DiscountMoreThanTwoItems implements Discount { + private next!: Discount; + calculate(budget: Budget): number { + if (budget.getItems().length > 2) { + console.log("Disconto aplicado foi DiscountMoreThanTwoItems"); + return budget.getTotal() * 0.1; + } else { + return this.next.calculate(budget); + } + } + setNext(next: Discount): void { + this.next = next; + } +} diff --git a/src/chain-of-responsability/discounts-calculator/discounts/NoDiscount.ts b/src/chain-of-responsability/discounts-calculator/discounts/NoDiscount.ts new file mode 100644 index 0000000..10d43e7 --- /dev/null +++ b/src/chain-of-responsability/discounts-calculator/discounts/NoDiscount.ts @@ -0,0 +1,12 @@ +import Discount from "./Discount"; +import Budget from "../Budget"; + +export default class NoDiscount implements Discount { + calculate(budget: Budget): number { + console.log("Disconto aplicado foi NoDiscount"); + return 0; + } + setNext(next: Discount): void { + throw new Error("No DIscount should be the last one implemented" + next); + } +}