Skip to content

Commit

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

export default interface Discount {
calculate(budget: Budget): number;
setNext(discount: Discount): void;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit ef13b1d

Please sign in to comment.