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 BasketInformations #3

Open
wants to merge 1 commit into
base: master
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
46 changes: 46 additions & 0 deletions typescript/src/world-company-remuneration/basket-informations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const K = "K";
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved

/**
* Created by thomas on 02/12/2019.
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
* This class is a basket
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
*/

export class BasketInformations {
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
// The product of the basket
static map: Map<string, number> = new Map<string, number>()
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved

addProductToBasket(product: string, price: number) {
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
BasketInformations.map.set(product, price)
}

getBasketPrice(inCents: boolean): Number {
var v = 0;
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
for (let s of Array.from(BasketInformations.map.values())) {
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
v += s;
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
}
return inCents ? new Number(v * 100) : Number(v)
}

resetBasket() {
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
this.buyBasket();
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
}

buyBasket() {
BasketInformations.map.clear();
}

isBasketContains(produit: string): boolean {
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
var found: boolean = false;
for (let s of Array.from(BasketInformations.map.keys())) {
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
if (s == produit) found = true;
}
return found;
}


mixWithBasket(b: BasketInformations) {
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
for (let [s,z] of Array.from(BasketInformations.map)) {
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
BasketInformations.map.set(s,z)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {BasketInformations} from "../../src/world-company-remuneration/basket-informations";

describe("a basket should cost", () => {
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved

test("0 when empty", () => {
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
expect(new BasketInformations().getBasketPrice(false)).toBe(0);
});

test("1000 otherwise", () => {
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
let basketInformations = new BasketInformations();
basketInformations.resetBasket()
Tarcaye marked this conversation as resolved.
Show resolved Hide resolved
basketInformations.addProductToBasket("Toto", 1000)
expect(basketInformations.getBasketPrice(false)).toBe(1000);
});

});