-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
124 lines (100 loc) · 2.27 KB
/
Copy pathmain.go
File metadata and controls
124 lines (100 loc) · 2.27 KB
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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type Task struct {
name string
points int
}
func main() {
reader := bufio.NewReader(os.Stdin)
tasks := []Task{}
for {
displayMenu()
choice := readInput(reader, "Select option")
switch choice {
case "1":
tasks = addTask(reader, tasks)
case "2":
displayTasks(tasks)
case "3":
total := calculateTotalPoints(tasks)
displayTotalPoints(total)
case "4":
tasks = removeTask(reader, tasks)
case "5":
fmt.Println("Exiting program")
return
default:
fmt.Println("Invalid option")
}
}
}
func displayMenu() {
fmt.Println("\n1. Add Task")
fmt.Println("2. View Tasks")
fmt.Println("3. View Total Points")
fmt.Println("4. Remove Task")
fmt.Println("5. Exit")
separator()
}
func separator() {
fmt.Println("\n-----------------------------------\n\n")
}
func readInput(reader *bufio.Reader, prompt string) string {
fmt.Print(prompt + ": ")
input, _ := reader.ReadString('\n')
return strings.TrimSpace(input)
}
func addTask(reader *bufio.Reader, tasks []Task) []Task {
name := readInput(reader, "Enter task name")
pointsInput := readInput(reader, "Enter task points")
points, err := strconv.Atoi(pointsInput)
if err != nil {
fmt.Println("Points must be a number")
return tasks
}
newTask := Task{name: name, points: points}
tasks = append(tasks, newTask)
fmt.Println("Task added")
return tasks
}
func displayTasks(tasks []Task) {
if len(tasks) == 0 {
fmt.Println("No tasks available")
return
}
for index, task := range tasks {
fmt.Printf("%d. %s (%d points)\n", index+1, task.name, task.points)
}
}
func calculateTotalPoints(tasks []Task) int {
total := 0
for _, task := range tasks {
total += task.points
}
return total
}
func displayTotalPoints(total int) {
fmt.Println("Total Points:", total)
}
func removeTask(reader *bufio.Reader, tasks []Task) []Task {
if len(tasks) == 0 {
fmt.Println("No tasks to remove")
return tasks
}
displayTasks(tasks)
indexInput := readInput(reader, "Enter task number to remove")
index, err := strconv.Atoi(indexInput)
if err != nil || index < 1 || index > len(tasks) {
fmt.Println("Invalid selection")
return tasks
}
tasks = append(tasks[:index-1], tasks[index:]...)
fmt.Println("Task removed")
return tasks
}