anagreg

task

Aug 15th, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.36 KB | Source Code | 0 0
  1. import { status } from '../common/status.enum.js';
  2.  
  3. /** The Task class holds all relevant data and behavior a task might have. */
  4. export class Task {
  5.  
  6.   #name;
  7.   #dueDate;
  8.   #status;
  9.  
  10. constructor(initialName, initialDate){
  11.   this.name = initialName;
  12.   this.dueDate = initialDate;
  13.   this.status = status.TODO;
  14. }
  15.  
  16. get name(){
  17.  return this.#name;
  18. }
  19.  
  20. set name(value){
  21.   if (!value || value.length <= 6 || value.length > 20) {
  22.     throw new Error('Invalid person name!');
  23.   }
  24.  
  25.   this.#name = value;
  26. }
  27.  
  28. get dueDate(){
  29.   return this.#dueDate;
  30. }
  31.  
  32. set dueDate(value){
  33.   if(value.valueOf() <= Date.now().valueOf()){
  34.     throw new Error('Can\'t set due date to a date in the past!');
  35.   }
  36.   this.#dueDate = value;
  37. }
  38.  
  39. get status(){
  40.   return this.#status;
  41. }
  42.  
  43. set status(value){
  44.     this.#status = value;
  45. }
  46.  
  47.   /** Sets the task status to TODO. */
  48.   reset() {
  49.     this.status = status.TODO;
  50.   }
  51.  
  52.   /** Sets the task status to IN_PROGRESS. */
  53.   advance() {
  54.     this.status = status.IN_PROGRESS;
  55.   }
  56.  
  57.   /** Sets the task status to DONE. */
  58.   complete() {
  59.     this.status = status.DONE;
  60.   }
  61.  
  62.   /** Transforms the task data into a formatted string */
  63.   toString() {
  64.     return `Name: ${this.name}\n` +
  65.            `Status: ${this.status}\n` +
  66.            `Due: ${this.dueDate.toLocaleDateString()} ${this.dueDate.toLocaleTimeString()}`;
  67.   }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment