Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Typescript] Add promotion to basket #4

Open
wants to merge 2 commits into
base: typescript/basket-informations
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions typescript/src/world-company-remuneration/basket-informations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,32 @@ export class BasketInformations {
// The product of the basket
static map: Map<string, number> = new Map<string, number>()

addProductToBasket(product: string, price: number) {
BasketInformations.map.set(product, price)
// The fact that the basket has promo code
static codeDePromotion: boolean = false


addProductToBasket(product: string, price: number, promoCode: boolean = false) {
if (promoCode) {
BasketInformations.codeDePromotion = true
} else {
BasketInformations.map.set(product, price)
}
}

getBasketPrice(inCents: boolean): Number {
var v = 0;
for (let s of Array.from(BasketInformations.map.values())) {
v += s;
}
if (BasketInformations.codeDePromotion) {
v -= 100;
}
return inCents ? new Number(v * 100) : Number(v)
}

resetBasket() {
this.buyBasket();
BasketInformations.codeDePromotion = false;
}

buyBasket() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("a basket should cost", () => {
test("1000 otherwise", () => {
let basketInformations = new BasketInformations();
basketInformations.resetBasket()
basketInformations.addProductToBasket("Toto", 1000)
basketInformations.addProductToBasket("Toto", 1000) // Promo = false
expect(basketInformations.getBasketPrice(false)).toBe(1000);
});

Expand Down