Skip to content

Commit b5c4305

Browse files
committed
[Typescript] Add BasketInformations
1 parent 63e625b commit b5c4305

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const K = "K";
2+
3+
/**
4+
* Created by thomas on 02/12/2019.
5+
* This class is a basket
6+
*/
7+
8+
export class BasketInformations {
9+
// The product of the basket
10+
static map: Map<string, number> = new Map<string, number>()
11+
12+
addProductToBasket(product: string, price: number) {
13+
BasketInformations.map.set(product, price)
14+
}
15+
16+
getBasketPrice(inCents: boolean): Number {
17+
var v = 0;
18+
for (let s of Array.from(BasketInformations.map.values())) {
19+
v += s;
20+
}
21+
return inCents ? new Number(v * 100) : Number(v)
22+
}
23+
24+
resetBasket() {
25+
this.buyBasket();
26+
}
27+
28+
buyBasket() {
29+
BasketInformations.map.clear();
30+
}
31+
32+
isBasketContains(produit: string): boolean {
33+
var found: boolean = false;
34+
for (let s of Array.from(BasketInformations.map.keys())) {
35+
if (s == produit) found = true;
36+
}
37+
return found;
38+
}
39+
40+
41+
mixWithBasket(b: BasketInformations) {
42+
for (let [s,z] of Array.from(BasketInformations.map)) {
43+
BasketInformations.map.set(s,z)
44+
}
45+
}
46+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {BasketInformations} from "../../src/world-company-remuneration/basket-informations";
2+
3+
describe("a basket should cost", () => {
4+
5+
test("0 when empty", () => {
6+
expect(new BasketInformations().getBasketPrice(false)).toBe(0);
7+
});
8+
9+
test("1000 otherwise", () => {
10+
let basketInformations = new BasketInformations();
11+
basketInformations.resetBasket()
12+
basketInformations.addProductToBasket("Toto", 1000)
13+
expect(basketInformations.getBasketPrice(false)).toBe(1000);
14+
});
15+
16+
});

0 commit comments

Comments
 (0)