From 4dc70a974459f594f220390e4c1a1137c6471945 Mon Sep 17 00:00:00 2001 From: Thomas Carpaye Date: Mon, 24 Jan 2022 21:56:37 +0100 Subject: [PATCH] [Typescript] Add BasketInformations --- .../basket-informations.ts | 46 +++++++++++++++++++ .../basket-informations.test.ts | 16 +++++++ 2 files changed, 62 insertions(+) create mode 100644 typescript/src/world-company-remuneration/basket-informations.ts create mode 100644 typescript/test/world-company-remuneration/basket-informations.test.ts diff --git a/typescript/src/world-company-remuneration/basket-informations.ts b/typescript/src/world-company-remuneration/basket-informations.ts new file mode 100644 index 0000000..cbe333d --- /dev/null +++ b/typescript/src/world-company-remuneration/basket-informations.ts @@ -0,0 +1,46 @@ +const K = "K"; + +/** + * Created by thomas on 02/12/2019. + * This class is a basket + */ + +export class BasketInformations { + // The product of the basket + static map: Map = new Map() + + addProductToBasket(product: string, price: number) { + BasketInformations.map.set(product, price) + } + + getBasketPrice(inCents: boolean): Number { + var v = 0; + for (let s of Array.from(BasketInformations.map.values())) { + v += s; + } + return inCents ? new Number(v * 100) : Number(v) + } + + resetBasket() { + this.buyBasket(); + } + + buyBasket() { + BasketInformations.map.clear(); + } + + isBasketContains(produit: string): boolean { + var found: boolean = false; + for (let s of Array.from(BasketInformations.map.keys())) { + if (s == produit) found = true; + } + return found; + } + + + mixWithBasket(b: BasketInformations) { + for (let [s,z] of Array.from(BasketInformations.map)) { + BasketInformations.map.set(s,z) + } + } +} diff --git a/typescript/test/world-company-remuneration/basket-informations.test.ts b/typescript/test/world-company-remuneration/basket-informations.test.ts new file mode 100644 index 0000000..0a18345 --- /dev/null +++ b/typescript/test/world-company-remuneration/basket-informations.test.ts @@ -0,0 +1,16 @@ +import {BasketInformations} from "../../src/world-company-remuneration/basket-informations"; + +describe("a basket should cost", () => { + + test("0 when empty", () => { + expect(new BasketInformations().getBasketPrice(false)).toBe(0); + }); + + test("1000 otherwise", () => { + let basketInformations = new BasketInformations(); + basketInformations.resetBasket() + basketInformations.addProductToBasket("Toto", 1000) + expect(basketInformations.getBasketPrice(false)).toBe(1000); + }); + +});