Skip to content

Commit 7b81bab

Browse files
committed
shopping cart LLD
1 parent 0628336 commit 7b81bab

21 files changed

+455
-0
lines changed

.vscode/launch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "ts-node",
6+
"type": "node",
7+
"request": "launch",
8+
"args": [
9+
"${relativeFile}"
10+
],
11+
"runtimeArgs": [
12+
"-r",
13+
"ts-node/register"
14+
],
15+
"cwd": "${workspaceFolder}",
16+
"protocol": "inspector",
17+
"internalConsoleOptions": "openOnSessionStart",
18+
"skipFiles": ["<node_internals>/**"]
19+
}
20+
]
21+
}

shopping-cart/Cart.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Cart = void 0;
4+
var Cart = /** @class */ (function () {
5+
function Cart() {
6+
this.products = [];
7+
}
8+
Cart.prototype.addProduct = function (product) {
9+
this.products.push(product);
10+
};
11+
Cart.prototype.getTotalPrice = function () {
12+
var sum = 0;
13+
this.products.map(function (x) {
14+
sum += x.getPrice();
15+
});
16+
return sum;
17+
};
18+
return Cart;
19+
}());
20+
exports.Cart = Cart;

shopping-cart/Cart.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Product } from './Product';
2+
3+
export class Cart{
4+
private products: Product[];
5+
6+
constructor() {
7+
this.products = [];
8+
}
9+
10+
public addProduct(product: Product): void {
11+
this.products.push(product);
12+
}
13+
14+
public getTotalPrice(): number {
15+
let sum = 0;
16+
this.products.map((x) => {
17+
sum+= x.getPrice();
18+
});
19+
return sum;
20+
}
21+
}

shopping-cart/DiscountDecorator.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = function (d, b) {
4+
extendStatics = Object.setPrototypeOf ||
5+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7+
return extendStatics(d, b);
8+
};
9+
return function (d, b) {
10+
if (typeof b !== "function" && b !== null)
11+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12+
extendStatics(d, b);
13+
function __() { this.constructor = d; }
14+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15+
};
16+
})();
17+
Object.defineProperty(exports, "__esModule", { value: true });
18+
exports.DiscountDecorator = void 0;
19+
var Product_1 = require("./Product");
20+
var DiscountDecorator = /** @class */ (function (_super) {
21+
__extends(DiscountDecorator, _super);
22+
function DiscountDecorator(product) {
23+
var _this = _super.call(this, product.getName(), product.getPrice(), product.getTypeEnum()) || this;
24+
_this.product = product;
25+
return _this;
26+
}
27+
return DiscountDecorator;
28+
}(Product_1.Product));
29+
exports.DiscountDecorator = DiscountDecorator;

shopping-cart/DiscountDecorator.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Product } from './Product';
2+
3+
export abstract class DiscountDecorator extends Product {
4+
protected product: Product;
5+
constructor(product: Product) {
6+
super(product.getName(), product.getPrice(), product.getTypeEnum());
7+
this.product = product;
8+
}
9+
public abstract getPrice(): number;
10+
}

shopping-cart/FixedDiscount.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = function (d, b) {
4+
extendStatics = Object.setPrototypeOf ||
5+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7+
return extendStatics(d, b);
8+
};
9+
return function (d, b) {
10+
if (typeof b !== "function" && b !== null)
11+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12+
extendStatics(d, b);
13+
function __() { this.constructor = d; }
14+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15+
};
16+
})();
17+
Object.defineProperty(exports, "__esModule", { value: true });
18+
exports.FixedDiscount = void 0;
19+
var DiscountDecorator_1 = require("./DiscountDecorator");
20+
var FixedDiscount = /** @class */ (function (_super) {
21+
__extends(FixedDiscount, _super);
22+
function FixedDiscount(product, discount) {
23+
var _this = _super.call(this, product) || this;
24+
_this.discount = discount;
25+
return _this;
26+
}
27+
FixedDiscount.prototype.getPrice = function () {
28+
return this.product.getPrice() - this.discount;
29+
};
30+
return FixedDiscount;
31+
}(DiscountDecorator_1.DiscountDecorator));
32+
exports.FixedDiscount = FixedDiscount;

shopping-cart/FixedDiscount.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { DiscountDecorator } from './DiscountDecorator';
2+
import { Product } from './Product';
3+
4+
export class FixedDiscount extends DiscountDecorator {
5+
private discount: number;
6+
constructor(product: Product, discount: number) {
7+
super(product);
8+
this.discount = discount;
9+
}
10+
11+
public getPrice(): number {
12+
return this.product.getPrice() - this.discount;
13+
}
14+
}

shopping-cart/Item1.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = function (d, b) {
4+
extendStatics = Object.setPrototypeOf ||
5+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7+
return extendStatics(d, b);
8+
};
9+
return function (d, b) {
10+
if (typeof b !== "function" && b !== null)
11+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12+
extendStatics(d, b);
13+
function __() { this.constructor = d; }
14+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15+
};
16+
})();
17+
Object.defineProperty(exports, "__esModule", { value: true });
18+
exports.Item1 = void 0;
19+
var Product_1 = require("./Product");
20+
var Item1 = /** @class */ (function (_super) {
21+
__extends(Item1, _super);
22+
function Item1(name, price, type) {
23+
return _super.call(this, name, price, type) || this;
24+
}
25+
Item1.prototype.getPrice = function () {
26+
return this.price;
27+
};
28+
return Item1;
29+
}(Product_1.Product));
30+
exports.Item1 = Item1;

shopping-cart/Item1.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Product } from './Product';
2+
import { ProductType } from './ProductType';
3+
4+
export class Item1 extends Product {
5+
constructor(name: string, price: number, type: ProductType) {
6+
super(name, price, type);
7+
}
8+
9+
public getPrice(): number {
10+
return this.price;
11+
}
12+
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = function (d, b) {
4+
extendStatics = Object.setPrototypeOf ||
5+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7+
return extendStatics(d, b);
8+
};
9+
return function (d, b) {
10+
if (typeof b !== "function" && b !== null)
11+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12+
extendStatics(d, b);
13+
function __() { this.constructor = d; }
14+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15+
};
16+
})();
17+
Object.defineProperty(exports, "__esModule", { value: true });
18+
exports.PercentageDiscount = void 0;
19+
var DiscountDecorator_1 = require("./DiscountDecorator");
20+
var PercentageDiscount = /** @class */ (function (_super) {
21+
__extends(PercentageDiscount, _super);
22+
function PercentageDiscount(product, discountPercentage) {
23+
var _this = _super.call(this, product) || this;
24+
_this.discountPercentage = discountPercentage;
25+
return _this;
26+
}
27+
PercentageDiscount.prototype.getPrice = function () {
28+
return this.product.getPrice() * (1 - this.discountPercentage / 100);
29+
};
30+
return PercentageDiscount;
31+
}(DiscountDecorator_1.DiscountDecorator));
32+
exports.PercentageDiscount = PercentageDiscount;

0 commit comments

Comments
 (0)