diff --git a/src/chain-of-responsability/Readme.md b/src/chain-of-responsability/Readme.md new file mode 100644 index 0000000..e69de29 diff --git a/src/chain-of-responsability/discounts-calculator/Budget.ts b/src/chain-of-responsability/discounts-calculator/Budget.ts index 6ac23ca..18c478b 100644 --- a/src/chain-of-responsability/discounts-calculator/Budget.ts +++ b/src/chain-of-responsability/discounts-calculator/Budget.ts @@ -17,4 +17,14 @@ export default class Budget { return nItems; } + + getTotal(): number { + return this.items + .map((product: Product) => { + return product.price; + }) + .reduce((prev: number, current: number) => { + return prev + current; + }, 0); + } } diff --git a/src/chain-of-responsability/discounts-calculator/Calculator.ts b/src/chain-of-responsability/discounts-calculator/Calculator.ts new file mode 100644 index 0000000..0cc016c --- /dev/null +++ b/src/chain-of-responsability/discounts-calculator/Calculator.ts @@ -0,0 +1,20 @@ +import Budget from "./Budget"; + +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; + } + + if (total > 500) { + discount += total * 0.07; + } + + return total - discount; + } +} diff --git a/src/chain-of-responsability/discounts-calculator/index.ts b/src/chain-of-responsability/discounts-calculator/index.ts index a505f27..e673dd9 100644 --- a/src/chain-of-responsability/discounts-calculator/index.ts +++ b/src/chain-of-responsability/discounts-calculator/index.ts @@ -1,4 +1,5 @@ import Budged from "./Budget"; +import Calculator from "./Calculator"; const budget = new Budged(); @@ -7,4 +8,6 @@ budget .add({ code: "B335", name: "Monitor LG Ultrawide 29", price: 1500.0 }) .add({ code: "V887", name: "Suporte Monitor Top", price: 300.0 }); - \ No newline at end of file +const calculator = new Calculator(budget); + +console.log(calculator.calculate()) \ No newline at end of file