-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
141 lines (128 loc) · 3.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)
type Task struct {
ID int
Title string
Description string
DueDate time.Time
Completed bool
}
type TaskManager struct {
Tasks []Task
}
func (tm *TaskManager) AddTask(task Task) {
tm.Tasks = append(tm.Tasks, task)
}
func (tm *TaskManager) DeleteTask(taskID int) {
for i, task := range tm.Tasks {
if task.ID == taskID {
tm.Tasks = append(tm.Tasks[:i], tm.Tasks[i+1:]...)
break
}
}
}
func (tm *TaskManager) ListTasks() {
// make a dynamic formating based on the size of the longest string in each column
//
// ID Title Description Due Date Completed
// 1 Task 1 Description 1 2019-01-01 false
// 2 Task 2 Description 2 2019-01-02 true
fmt.Println("ID\tTitle\tDescription\tDue Date\tCompleted")
for _, task := range tm.Tasks {
fmt.Printf("%d\t%s\t%s\t%s\t%v\n", task.ID, task.Title, task.Description, task.DueDate.Format("2006-01-02"), task.Completed)
}
}
func main() {
fmt.Println("Welcome to the Task Manager!")
fmt.Println("--------------------------------------------")
fmt.Println("Commands: add, delete, list, quit (or exit)")
taskManager := TaskManager{}
// Read task data from file
file, err := os.Open("tasks.json")
if err == nil {
decoder := json.NewDecoder(file)
err = decoder.Decode(&taskManager)
if err != nil {
log.Fatal("Error decoding task data:", err)
}
file.Close()
} else if !os.IsNotExist(err) {
log.Fatal("Error opening task data file:", err)
}
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("> ")
if !scanner.Scan() {
break
}
line := scanner.Text()
if line == "" {
continue
}
args := strings.Split(line, " ")
command := args[0]
switch command {
case "add":
if len(args) < 4 {
fmt.Println("Usage: add <title> <description> <due-date>")
continue
}
dueDate, err := time.Parse("2006-01-02", args[3])
if err != nil {
fmt.Println("Invalid due date format. Please use yyyy-mm-dd.")
continue
}
task := Task{
ID: len(taskManager.Tasks) + 1,
Title: args[1],
Description: args[2],
DueDate: dueDate,
}
taskManager.AddTask(task)
fmt.Println("Task added successfully!")
case "delete":
if len(args) < 2 {
fmt.Println("Usage: delete <id>")
continue
}
taskID, err := strconv.Atoi(args[1])
if err != nil {
fmt.Println("Invalid task ID. Please enter a number.")
continue
}
taskManager.DeleteTask(taskID)
fmt.Println("Task deleted successfully!")
case "list":
taskManager.ListTasks()
case "quit", "exit":
fmt.Println("Goodbye!")
// Write task data to file
file, err := os.Create("tasks.json")
if err == nil {
encoder := json.NewEncoder(file)
err = encoder.Encode(&taskManager)
if err != nil {
log.Fatal("Error encoding task data:", err)
}
file.Close()
return // Exit program
} else {
log.Fatal("Error creating task data file:", err)
}
default:
fmt.Println("Unknown command. Please try again.")
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}