Created
April 12, 2019 14:17
-
-
Save tyhonchik/1647b2157670d3f116dd8159c776f0c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Задача: | |
| Что вы здесь видите и как реализовать: | |
| let taskList = new TaskList<Task>(); | |
| taskList.add(new SimpleTask(title, content)); | |
| taskList.add(new FitureTask(title, content, fiture)); | |
| taskList.startAll(); | |
| */ | |
| enum TaskStatus { | |
| TO_DO, | |
| IN_PROGRESS, | |
| IN_TESTING, | |
| DONE | |
| } | |
| interface ITask { | |
| getTask: () => void; | |
| } | |
| interface ISimpleTask { | |
| title: string; | |
| content: string; | |
| } | |
| interface IFeatureTask extends ISimpleTask { | |
| feature: string; | |
| } | |
| function isFeatureTask(object: any): object is IFeatureTask { | |
| return "feature" in object; | |
| } | |
| class Task implements ITask { | |
| private title: string; | |
| private content: string; | |
| private feature?: string; | |
| status: TaskStatus; | |
| constructor(args: ISimpleTask | IFeatureTask) { | |
| this.title = args.title; | |
| this.content = args.content; | |
| this.status = TaskStatus.TO_DO; | |
| if (isFeatureTask(args)) { | |
| this.feature = args.feature; | |
| } | |
| this.getTask(); | |
| } | |
| getTask = () => { | |
| const { title, content, feature, status } = this; | |
| console.log( | |
| `Task is: ${title}, ${content}${ | |
| feature ? `, ${feature}` : "" | |
| } with status: ${TaskStatus[status]}` | |
| ); | |
| }; | |
| } | |
| class TaskList<T extends Task = Task> { | |
| private taskList: T[] = []; | |
| add(newElement: T): void { | |
| this.taskList.push(newElement); | |
| } | |
| startAll(): void { | |
| this.taskList.forEach(task => | |
| task.status === TaskStatus.TO_DO || !task.status | |
| ? (task.status = TaskStatus.IN_PROGRESS) | |
| : null | |
| ); | |
| } | |
| } | |
| class SimpleTask extends Task { | |
| constructor(title: string, content: string) { | |
| super({ title, content }); | |
| } | |
| } | |
| class FeatureTask extends Task { | |
| constructor(title: string, content: string, feature: string) { | |
| super({ title, content, feature }); | |
| } | |
| } | |
| // ======== main ======== | |
| // Создаем экземпляр списка задач | |
| let taskList = new TaskList<Task>(); | |
| // Добавляем задачу без фичи | |
| taskList.add(new SimpleTask("titleSimple", "simpleContent")); | |
| // Добавляем задачу с фичей | |
| taskList.add(new FeatureTask("featSimple", "featContent", "featFeature")); | |
| // Переводим все задачи в InProgress | |
| taskList.startAll(); | |
| console.log(taskList); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment