Commit 6c3709f9 authored by Nurasyl's avatar Nurasyl

Initial commit

parents
node_modules
\ No newline at end of file
import { TGetRandomNumber } from "./src/types";
export const getRandomNumber = ({min, max}: TGetRandomNumber): number => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
\ No newline at end of file
This diff is collapsed.
{
"name": "classwork",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon --ignore tests/ --watch src -e ts,tsx --exec ts-node src/index.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/cli-table": "^0.3.4",
"@types/nodemon": "^1.19.6",
"axios": "^1.6.7",
"cli-table": "^0.3.11",
"nodemon": "^3.1.0",
"ts-node": "^10.9.2"
},
"devDependencies": {
"@types/node": "^20.11.24"
}
}
import { TSubProduct } from "../types";
import Product from "./Product";
class Corn extends Product {
constructor(data: TSubProduct) {
super({
name: "Corn",
storageLifeDays: 150,
deliveryTimestamp: data.deliveryTimestamp,
storagePlace: data.storagePlace
});
};
override isFresh(): boolean {
const dateDifference: number = (Date.now() - this.deliveryTimestamp?.getTime()) / (1000 * 3600 * 24);
if(dateDifference > this.storageLifeDays) {
return false;
} else {
return true;
};
};
};
export default Corn;
\ No newline at end of file
import StoragePlace from "../enum";
import { TSubProduct } from "../types";
import Product from "./Product";
class Fish extends Product {
constructor(data: TSubProduct) {
super({
name: "Fish",
storageLifeDays: 20,
deliveryTimestamp: data.deliveryTimestamp,
storagePlace: data.storagePlace
});
};
override isFresh(): boolean {
if(this.storagePlace === StoragePlace.Showcase) {
this.storageLifeDays = this.storageLifeDays / 6;
};
const dateDifference: number = (Date.now() - this.deliveryTimestamp?.getTime()) / (1000 * 3600 * 24);
if(dateDifference > this.storageLifeDays) {
return false;
} else {
return true;
};
};
};
export default Fish;
\ No newline at end of file
import StoragePlace from "../enum";
import { TSubProduct } from "../types";
import Product from "./Product";
class Milk extends Product {
constructor(data: TSubProduct) {
super({
name: "Milk",
storageLifeDays: 60,
deliveryTimestamp: data.deliveryTimestamp,
storagePlace: data.storagePlace
});
};
override isFresh(): boolean {
if(this.storagePlace === StoragePlace.Showcase) {
this.storageLifeDays = this.storageLifeDays / 2;
};
const dateDifference: number = (Date.now() - this.deliveryTimestamp?.getTime()) / (1000 * 3600 * 24);
if(dateDifference > this.storageLifeDays) {
return false;
} else {
return true;
};
};
};
export default Milk;
\ No newline at end of file
import StoragePlace from "../enum";
import { TProduct } from "../types";
abstract class Product {
public deliveryTimestamp: Date;
public storagePlace: StoragePlace;
public storageLifeDays: number;
public name: string;
constructor(data: TProduct) {
this.deliveryTimestamp = data.deliveryTimestamp;
this.storagePlace = data.storagePlace;
this.storageLifeDays = data.storageLifeDays;
this.name = data.name;
};
public isFresh(): boolean {
return true;
};
};
export default Product;
\ No newline at end of file
import { TSubProduct } from "../types";
import Product from "./Product";
class Salt extends Product {
constructor(data: TSubProduct) {
super({
name: "Salt",
storageLifeDays: Number.POSITIVE_INFINITY,
deliveryTimestamp: data.deliveryTimestamp,
storagePlace: data.storagePlace
});
};
override isFresh(): boolean {
return true;
};
};
export default Salt;
\ No newline at end of file
import { TSubProduct } from "../types";
import Product from "./Product";
class Stew extends Product {
constructor(data: TSubProduct) {
super({
name: "Stew",
storageLifeDays: 180,
deliveryTimestamp: data.deliveryTimestamp,
storagePlace: data.storagePlace
});
};
override isFresh(): boolean {
const dateDifference: number = (Date.now() - this.deliveryTimestamp?.getTime()) / (1000 * 3600 * 24);
if(dateDifference > this.storageLifeDays) {
return false;
} else {
return true;
};
};
};
export default Stew;
\ No newline at end of file
import Table from "cli-table";
import StoragePlace from "./enum";
import Product from "./Products/Product";
import Corn from "./Products/Corn";
import Fish from "./Products/Fish";
import Milk from "./Products/Milk";
import Salt from "./Products/Salt";
import Stew from "./Products/Stew";
import { getRandomNumber } from "../helpers";
class Store {
public products: Product[] = [];
constructor() {
for(let i = 0; i < 100; i++) {
const products = [
new Corn({deliveryTimestamp: this.getRandomDeliveryTime(), storagePlace: this.getRandomStoragePlace()}),
new Fish({deliveryTimestamp: this.getRandomDeliveryTime(), storagePlace: this.getRandomStoragePlace()}),
new Milk({deliveryTimestamp: this.getRandomDeliveryTime(), storagePlace: this.getRandomStoragePlace()}),
new Salt({deliveryTimestamp: this.getRandomDeliveryTime(), storagePlace: this.getRandomStoragePlace()}),
new Stew({deliveryTimestamp: this.getRandomDeliveryTime(), storagePlace: this.getRandomStoragePlace()})
];
const randomIndex = getRandomNumber({min: 0, max: products.length - 1});
this.products.push(products[randomIndex]);
};
};
public doInspection(): void {
const table = new Table({
head: ['Product', 'Delivered at', 'Storage place', 'S. life days', 'Fresh'],
colWidths: [20, 20, 15, 15, 10]
});
this.products?.forEach(product => {
table.push([
product.name,
product.deliveryTimestamp?.toISOString(),
product.storagePlace,
product.storageLifeDays?.toString(),
product.isFresh() ? "Fresh" : "Not Fresh"
]);
});
console.log(table.toString());
};
public getRandomDeliveryTime(): Date {
const randomNum: number = getRandomNumber({min: 1, max: 200});
return new Date(Date.now() - 86400000 * randomNum);
};
public getRandomStoragePlace(): StoragePlace {
const enums: (string | StoragePlace)[] = Object.values(StoragePlace);
const randomIndex = Math.floor(Math.random() * enums.length);
const randomEnumKey = enums[randomIndex];
return randomEnumKey as StoragePlace;
};
};
export default Store;
\ No newline at end of file
enum StoragePlace {
Icebox = "Icebox",
Showcase = "Showcase"
};
export default StoragePlace;
\ No newline at end of file
import Store from "./Store";
const store = new Store();
store.doInspection();
\ No newline at end of file
import StoragePlace from "./enum";
export type TProduct = {
storageLifeDays: number;
name: string;
deliveryTimestamp: Date;
storagePlace: StoragePlace;
};
export type TSubProduct = {
deliveryTimestamp: Date;
storagePlace: StoragePlace;
};
export type TGetRandomNumber = {
min: number;
max: number;
};
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment