boris-ivanov

ts pitane

Mar 28th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import { Product } from '.';
  2. import { validate } from './validator';
  3.  
  4. export class Category {
  5. private readonly minNameLength: number = 2;
  6. private readonly maxNameLength: number = 15;
  7. private readonly _name: string;
  8. private _products: Product[];
  9.  
  10. public constructor(name: string) {
  11. if (name.length < this.minNameLength || name.length > this.maxNameLength){
  12. throw new Error(`Product name length must be between ${this.minNameLength} and ${this.maxNameLength}`);
  13. }
  14. this._name = name;
  15. this._products = [];
  16. }
  17.  
  18. public get products(): Product[] {
  19.  
  20. return this._products;
  21. }
  22. public addProduct(product: Product): void {
  23.  
  24. if (product === null) {
  25. throw new Error('Product to add cannot be null!');
  26. }
  27.  
  28. this.products.push(product);
  29. }
  30.  
  31.  
  32.  
  33. public removeProduct(product: Product): void {
  34.  
  35. if (product === null || product === undefined){
  36. throw new Error(`Error`);
  37. }
  38. const productFound: Product | undefined = this.products.find((x: Product) => x.name === product.name);
  39. }
  40.  
  41. public print(): string {
  42.  
  43. throw new Error('Not implemented!');
  44. }
  45.  
  46. // Do not remove this. It is used for the tests.
  47. protected set mockProductsData(value: Product[]) {
  48. this._products = value;
  49. }
  50. }
Add Comment
Please, Sign In to add comment