Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum Status {
- DONE = "Done", IN_PROGRESS = "In progress"
- }
- enum Priority {
- HIGH = "high", LOW = "low"
- }
- interface Task {
- name: string,
- status: Status,
- priority: Priority
- }
- class TaskRepository {
- private tasks: Array<Task> = [];
- public getAllTasks(): Array<Task> {
- return this.tasks.sort((t1, t2) => {
- if (t1.status === Status.DONE && t2.status === Status.DONE) {
- return 0;
- }
- if (t1.status === Status.DONE && t2.status !== Status.DONE) {
- return 1;
- }
- return -1;
- });
- }
- public addTask(name: string): void {
- this.tasks.push({name, status: Status.IN_PROGRESS, priority: Priority.HIGH});
- }
- public changeTask(name: string, status: Status, priority: Priority): void {
- const taskToChange: Array<Task> = this.tasks.filter(t => t.name === name);
- if (taskToChange.length < 1) {
- return;
- }
- taskToChange[0].status = status;
- taskToChange[0].priority = priority;
- }
- public deleteTaskByName(name: string): void {
- const newTasks = this.tasks.filter(t => t.name !== name);
- this.tasks = newTasks;
- }
- }
- const taskRepo = new TaskRepository();
- taskRepo.addTask("create a post");
- console.log("Add create a post task:")
- console.log(taskRepo.getAllTasks());
- taskRepo.changeTask("create a post", Status.DONE, Priority.LOW);
- console.log("Modify create a post task:")
- console.log(taskRepo.getAllTasks());
- taskRepo.addTask("have a walk");
- console.log("Add have a walk task:")
- console.log(taskRepo.getAllTasks());
- taskRepo.deleteTaskByName("create a post")
- console.log("Delete create a post task:")
- console.log(taskRepo.getAllTasks());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement