From 0d878efb8a53f6ac91f80497d433d717821a0564 Mon Sep 17 00:00:00 2001 From: KA1SER-06 Date: Sat, 28 Dec 2024 13:59:08 +0300 Subject: [PATCH 1/2] lab9 --- golang/Lab9/lab9.go | 51 +++++++++++++++++++++++++++++++++++++++++ golang/Lab9/task.go | 19 +++++++++++++++ golang/Lab9/tasklist.go | 34 +++++++++++++++++++++++++++ golang/go.mod | 2 +- golang/main.go | 9 ++++++-- 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 golang/Lab9/lab9.go create mode 100644 golang/Lab9/task.go create mode 100644 golang/Lab9/tasklist.go diff --git a/golang/Lab9/lab9.go b/golang/Lab9/lab9.go new file mode 100644 index 00000000..fb7961a0 --- /dev/null +++ b/golang/Lab9/lab9.go @@ -0,0 +1,51 @@ +package lab9 + +import ( + "fmt" +) + +type Task struct { + ID int + Description string + Completed bool +} + +type TaskList struct { + Tasks []Task +} + +func (t *Tasklist) AddTask(description string) { + id := len(t.Tasks) + 1 + t.Tasks = append(t.Tasks, Task{ID: id, Description: description, Completed: false}) + fmt.Printf("Task added: %d - %s", id, description) +} + +func (t *Tasklist) CompleteTask(id int) { + for i := range t.Tasks { + if t.Tasks[i].ID == id { + t.Tasks[i].Completed = true + fmt.Printf("Task %d completed", id) + return + } + } + fmt.Printf("Task %d not found", id) +} + +func (t *Tasklist) ShowTasks() { + for _, task := range t.Tasks { + status := "incomplete" + if task.Completed { + status = "completed" + } + fmt.Printf("%d: %s [%s]", task.ID, task.Description, status) + } +} + +func RunLab9() { + taskList := &Tasklist{} + taskList.AddTask("Write a Go program") + taskList.AddTask("Test the program") + taskList.ShowTasks() + taskList.CompleteTask(1) + taskList.ShowTasks() +} diff --git a/golang/Lab9/task.go b/golang/Lab9/task.go new file mode 100644 index 00000000..981993a9 --- /dev/null +++ b/golang/Lab9/task.go @@ -0,0 +1,19 @@ +package lab9 + +type task struct { + ID int + Description string + Completed bool +} + +func NewTask(id int, description string) Task { + return Task{ + ID: id, + Description: description, + Completed: false, + } +} + +func (t *Task) MarkAsComplete() { + t.Completed = true +} diff --git a/golang/Lab9/tasklist.go b/golang/Lab9/tasklist.go new file mode 100644 index 00000000..1a16392d --- /dev/null +++ b/golang/Lab9/tasklist.go @@ -0,0 +1,34 @@ +package lab9 + +import "fmt" + +type Tasklist struct { + Tasks []Task +} + +func (tl *Tasklist) Add(description string) { + id := len(tl.Tasks) + 1 + tl.Tasks = append(tl.Tasks, NewTask(id, description)) + fmt.Printf("Task added: %d - %s", id, description) +} + +func (tl *Tasklist) Complete(id int) { + for i := range tl.Tasks { + if tl.Tasks[i].ID == id { + tl.Tasks[i].MarkAsComplete() + fmt.Printf("Task %d marked as complete", id) + return + } + } + fmt.Printf("Task %d not found", id) +} + +func (tl *Tasklist) Display() { + for _, task := range tl.Tasks { + status := "Incomplete" + if task.Completed { + status = "Complete" + } + fmt.Printf("%d: %s [%s]", task.ID, task.Description, status) + } +} diff --git a/golang/go.mod b/golang/go.mod index d1cc943c..c1326067 100644 --- a/golang/go.mod +++ b/golang/go.mod @@ -2,4 +2,4 @@ module isuct.ru/informatics2022 go 1.16 -require github.com/stretchr/testify v1.8.1 +require github.com/stretchr/testify v1.8.1 \ No newline at end of file diff --git a/golang/main.go b/golang/main.go index d2c4e91e..8ffbbdb1 100644 --- a/golang/main.go +++ b/golang/main.go @@ -1,7 +1,12 @@ package main -import "fmt" +import ( + "fmt" + + lab9 "isuct.ru/informatics2022/lab9" +) func main() { - fmt.Println("Hello world") + fmt.Println("Maksimov Daniil Andreevich") + lab9.RunLab9() } From d68d7b9920bcd018d69b93175a5ca6a1f6d8c9e5 Mon Sep 17 00:00:00 2001 From: Maksimov Daniil Date: Sat, 28 Dec 2024 14:03:01 +0300 Subject: [PATCH 2/2] Update go.mod --- golang/go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/golang/go.mod b/golang/go.mod index c1326067..5cecc1f3 100644 --- a/golang/go.mod +++ b/golang/go.mod @@ -1,5 +1,3 @@ module isuct.ru/informatics2022 go 1.16 - -require github.com/stretchr/testify v1.8.1 \ No newline at end of file