-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Baldina Lab9 #509
Open
BaldinaDaria
wants to merge
1
commit into
ISUCT:Baldina_Daria
Choose a base branch
from
BaldinaDaria:Baldina_Lab9
base: Baldina_Daria
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Baldina Lab9 #509
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package Lab9 | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type Task struct { | ||
Description string | ||
Completed bool | ||
} | ||
|
||
type TaskManager interface { | ||
AddTask(description string) | ||
ShowTasks() | ||
CompleteTask(index int) | ||
DeleteTask(index int) | ||
SearchTask(keyword string) | ||
LoadTasks(filename string) error | ||
SaveTasks(filename string) error | ||
} | ||
|
||
type SimpleTaskManager struct { | ||
tasks []Task | ||
} | ||
|
||
func (tm *SimpleTaskManager) AddTask(description string) { | ||
tm.tasks = append(tm.tasks, Task{Description: description, Completed: false}) | ||
} | ||
|
||
func (tm *SimpleTaskManager) ShowTasks() { | ||
if len(tm.tasks) == 0 { | ||
fmt.Println("Список задач пуст.") | ||
return | ||
} | ||
for i, task := range tm.tasks { | ||
status := "не выполнена" | ||
if task.Completed { | ||
status = "выполнена" | ||
} | ||
fmt.Printf("%d: %s [%s]\n", i+1, task.Description, status) | ||
} | ||
} | ||
|
||
func (tm *SimpleTaskManager) CompleteTask(index int) { | ||
if index < 0 || index >= len(tm.tasks) { | ||
fmt.Println("Некорректный индекс.") | ||
return | ||
} | ||
tm.tasks[index].Completed = true | ||
} | ||
|
||
func (tm *SimpleTaskManager) DeleteTask(index int) { | ||
if index < 0 || index >= len(tm.tasks) { | ||
fmt.Println("Некорректный индекс.") | ||
return | ||
} | ||
tm.tasks = append(tm.tasks[:index], tm.tasks[index+1:]...) | ||
} | ||
|
||
func (tm *SimpleTaskManager) SearchTask(keyword string) { | ||
found := false | ||
for _, task := range tm.tasks { | ||
if strings.Contains(task.Description, keyword) { | ||
fmt.Println(task.Description) | ||
found = true | ||
} | ||
} | ||
if !found { | ||
fmt.Println("Задачи не найдены.") | ||
} | ||
} | ||
|
||
func (tm *SimpleTaskManager) LoadTasks(filename string) error { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return nil | ||
} | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
parts := strings.Split(line, "|") | ||
if len(parts) != 2 { | ||
continue | ||
} | ||
description := parts[0] | ||
completed := parts[1] == "1" | ||
tm.tasks = append(tm.tasks, Task{Description: description, Completed: completed}) | ||
} | ||
|
||
return scanner.Err() | ||
} | ||
|
||
func (tm *SimpleTaskManager) SaveTasks(filename string) error { | ||
file, err := os.Create(filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
writer := bufio.NewWriter(file) | ||
for _, task := range tm.tasks { | ||
status := "0" | ||
if task.Completed { | ||
status = "1" | ||
} | ||
_, err := writer.WriteString(fmt.Sprintf("%s|%s\n", task.Description, status)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return writer.Flush() | ||
} | ||
|
||
func RunLab9() { | ||
manager := &SimpleTaskManager{} | ||
const filename = "tasks.txt" | ||
|
||
if err := manager.LoadTasks(filename); err != nil { | ||
fmt.Printf("Ошибка загрузки задач: %v\n", err) | ||
return | ||
} | ||
|
||
scanner := bufio.NewScanner(os.Stdin) | ||
for { | ||
fmt.Println("\n=============================") | ||
fmt.Println(" МЕНЮ ЗАДАЧ ") | ||
fmt.Println("=============================") | ||
fmt.Println("1. Добавить задачу") | ||
fmt.Println("2. Показать все задачи") | ||
fmt.Println("3. Отметить задачу как выполненную") | ||
fmt.Println("4. Удалить задачу") | ||
fmt.Println("5. Поиск задачи") | ||
fmt.Println("6. Выйти") | ||
fmt.Print("Выберите действие (1-6): ") | ||
|
||
scanner.Scan() | ||
choice := scanner.Text() | ||
|
||
switch choice { | ||
case "1": | ||
fmt.Print("Введите описание задачи: ") | ||
scanner.Scan() | ||
description := scanner.Text() | ||
manager.AddTask(description) | ||
case "2": | ||
manager.ShowTasks() | ||
case "3": | ||
fmt.Print("Введите номер задачи для отметки как выполненной: ") | ||
scanner.Scan() | ||
var index int | ||
fmt.Sscanf(scanner.Text(), "%d", &index) | ||
manager.CompleteTask(index - 1) | ||
case "4": | ||
fmt.Print("Введите номер задачи для удаления: ") | ||
scanner.Scan() | ||
var index int | ||
fmt.Sscanf(scanner.Text(), "%d", &index) | ||
manager.DeleteTask(index - 1) | ||
case "5": | ||
fmt.Print("Введите ключевое слово для поиска: ") | ||
scanner.Scan() | ||
keyword := scanner.Text() | ||
manager.SearchTask(keyword) | ||
case "6": | ||
if err := manager.SaveTasks(filename); err != nil { | ||
fmt.Printf("Ошибка сохранения задач: %v\n", err) | ||
} | ||
fmt.Println("Выход из программы.") | ||
return | ||
default: | ||
fmt.Println("Некорректный выбор. Пожалуйста, попробуйте снова.") | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Разделите на файлы, сложно такое проверять, а в будущем, если бы это был настоящий проект, и поддерживать