-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dd57c5
commit ef13b1d
Showing
5 changed files
with
61 additions
and
10 deletions.
There are no files selected for viewing
19 changes: 9 additions & 10 deletions
19
src/chain-of-responsability/discounts-calculator/Calculator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/chain-of-responsability/discounts-calculator/discounts/Discount.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/chain-of-responsability/discounts-calculator/discounts/DiscountHigherThan500.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/chain-of-responsability/discounts-calculator/discounts/DiscountMoreThanTwoItems.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/chain-of-responsability/discounts-calculator/discounts/NoDiscount.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |