Created
March 6, 2025 17:49
-
-
Save Dhananjay-JSR/98d014387b2dc5355f75c11929fecd46 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"os/exec" | |
"strconv" | |
"runtime" | |
// "strings" | |
// "strings" | |
) | |
type Task struct { | |
ID int | |
Description string | |
Completed bool | |
} | |
type TaskManager struct { | |
tasks []Task | |
nextID int | |
} | |
func NewTaskManager() *TaskManager { | |
return &TaskManager{ | |
tasks: make([]Task, 0), | |
nextID: 1, | |
} | |
} | |
func (tm *TaskManager) addTask(description string) { | |
task := Task{ | |
ID: tm.nextID, | |
Description: description, | |
Completed: false, | |
} | |
tm.tasks = append(tm.tasks, task) | |
tm.nextID++ | |
fmt.Printf("Task added successfully with ID: %d\n", task.ID) | |
} | |
func (tm *TaskManager) listTasks() { | |
if len(tm.tasks) == 0 { | |
fmt.Println("No tasks found!") | |
return | |
} | |
fmt.Println("\nTask List:") | |
fmt.Println("------------------") | |
for _, task := range tm.tasks { | |
status := " " | |
if task.Completed { | |
status = "✓" | |
} | |
fmt.Printf("[%s] %d. %s\n", status, task.ID, task.Description) | |
} | |
fmt.Println("------------------") | |
} | |
func (tm *TaskManager) markComplete(id int) { | |
for i := range tm.tasks { | |
if tm.tasks[i].ID == id { | |
tm.tasks[i].Completed = true | |
fmt.Printf("Task %d marked as complete\n", id) | |
return | |
} | |
} | |
fmt.Printf("Task with ID %d not found\n", id) | |
} | |
func (tm *TaskManager) deleteTask(id int) { | |
for i := range tm.tasks { | |
if tm.tasks[i].ID == id { | |
tm.tasks = append(tm.tasks[:i], tm.tasks[i+1:]...) | |
fmt.Printf("Task %d deleted successfully\n", id) | |
return | |
} | |
} | |
fmt.Printf("Task with ID %d not found\n", id) | |
} | |
func printMenu() { | |
fmt.Println("\nTask Manager Menu:") | |
fmt.Println("1. Add Task") | |
fmt.Println("2. List Tasks") | |
fmt.Println("3. Mark Task as Complete") | |
fmt.Println("4. Delete Task") | |
fmt.Println("5. Exit") | |
fmt.Print("Enter your choice: ") | |
} | |
func clearScreen() { | |
switch runtime.GOOS { | |
case "windows": | |
cmd := exec.Command("cmd", "/c", "cls") | |
cmd.Stdout = os.Stdout | |
cmd.Run() | |
default: // For linux, mac, etc. | |
cmd := exec.Command("clear") | |
cmd.Stdout = os.Stdout | |
cmd.Run() | |
} | |
} | |
func main() { | |
taskManager := NewTaskManager() | |
scanner := bufio.NewScanner(os.Stdin) | |
for { | |
clearScreen() | |
printMenu() | |
scanner.Scan() | |
choice := scanner.Text() | |
clearScreen() // Clear screen after choice | |
switch choice { | |
case "1": | |
fmt.Print("Enter task description: ") | |
scanner.Scan() | |
description := scanner.Text() | |
taskManager.addTask(description) | |
fmt.Print("\nPress Enter to continue...") | |
scanner.Scan() | |
case "2": | |
taskManager.listTasks() | |
fmt.Print("\nPress Enter to continue...") | |
scanner.Scan() | |
case "3": | |
fmt.Print("Enter task ID to mark as complete: ") | |
scanner.Scan() | |
id, err := strconv.Atoi(scanner.Text()) | |
if err != nil { | |
fmt.Println("Invalid ID. Please enter a number.") | |
fmt.Print("\nPress Enter to continue...") | |
scanner.Scan() | |
continue | |
} | |
taskManager.markComplete(id) | |
fmt.Print("\nPress Enter to continue...") | |
scanner.Scan() | |
case "4": | |
fmt.Print("Enter task ID to delete: ") | |
scanner.Scan() | |
id, err := strconv.Atoi(scanner.Text()) | |
if err != nil { | |
fmt.Println("Invalid ID. Please enter a number.") | |
fmt.Print("\nPress Enter to continue...") | |
scanner.Scan() | |
continue | |
} | |
taskManager.deleteTask(id) | |
fmt.Print("\nPress Enter to continue...") | |
scanner.Scan() | |
case "5": | |
clearScreen() | |
fmt.Println("Goodbye!") | |
return | |
default: | |
fmt.Println("Invalid choice. Please try again.") | |
fmt.Print("\nPress Enter to continue...") | |
scanner.Scan() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment