Skip to content

Commit

Permalink
prepared to applly chain
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusbalbi committed Aug 25, 2020
1 parent 935bba7 commit 6dd57c5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
Empty file.
10 changes: 10 additions & 0 deletions src/chain-of-responsability/discounts-calculator/Budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
20 changes: 20 additions & 0 deletions src/chain-of-responsability/discounts-calculator/Calculator.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 4 additions & 1 deletion src/chain-of-responsability/discounts-calculator/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Budged from "./Budget";
import Calculator from "./Calculator";

const budget = new Budged();

Expand All @@ -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 });


const calculator = new Calculator(budget);

console.log(calculator.calculate())

0 comments on commit 6dd57c5

Please sign in to comment.