Menu
Coddy logo textTech

Putting It All Together

Part of the Logic & Flow section of Coddy's GO journey. Lesson 25 of 68.

challenge icon

Challenge

Easy

Time to finish your task list application. In this last step you bring together everything from the previous lessons: the Task struct, addTask, viewAllTasks, completeTask and removeTask, driven by a session of menu actions.

You will receive three inputs:

  • A string with the application name and the user name separated by a comma (e.g., "MyTasks,Alice")
  • A string with the tasks the list starts with, in the format "name1:status1,name2:status2" where status is "true" or "false" (e.g., "Write report:false,Call dentist:true"). This line is empty when the list starts out empty.
  • A string with the actions of the session, separated by commas. An action is either its name on its own or its name and one parameter joined by | (e.g., "add|Buy groceries,view,complete|2,remove|0,exit"). The action list always ends with "exit".

Task names contain spaces, so read all three lines with bufio.Scanner, as in the previous lessons.

Your task is to:

  1. Define the same Task struct with Name (string) and Completed (bool) fields
  2. Bring back the four functions you already wrote, with the same signatures:
    • addTask(tasks []Task, taskName string) []Task
    • viewAllTasks(tasks []Task), which prints "[x] [task_name]" for a completed task and "[ ] [task_name]" for one that is still open
    • completeTask(tasks *[]Task, index int)
    • removeTask(tasks []Task, index int) []Task
  3. Build the starting list from the second input, remembering that an empty line means an empty list
  4. Print the welcome message and the menu, exactly as in the first step of the project:
    • "Welcome to [app_name], [user_name]!"
    • "1. Add Task"
    • "2. View Tasks"
    • "3. Complete Task"
    • "4. Remove Task"
    • "5. Exit"
    • "Current tasks: [count]"
  5. Run the actions in order. Every action prints its header first:
    • "add": print "--- ADD TASK ---", add the task named by the parameter, then print "Task '[task_name]' added!"
    • "view": print "--- VIEW TASKS ---", list every task with viewAllTasks, then print the summary line
    • "complete": print "--- COMPLETE TASK ---", mark the task at that index, then print "Task '[task_name]' marked as completed!"
    • "remove": print "--- REMOVE TASK ---", remove the task at that index, then print "Task '[task_name]' removed successfully!"
    • "exit": print "--- EXIT ---", then "Final list:", then the whole list, then the summary line, and finally "Goodbye, [user_name]!". Nothing runs after exit.
  6. Indexes are 0 based, as in the previous lessons. When a "complete" or "remove" index is outside the list, print "Invalid task number" instead and leave the list unchanged.
  7. The summary line is the one you already know: "Total: [total_count] tasks ([completed_count] completed, [incomplete_count] remaining)"

Use the strings package to split the input lines and the strconv package to turn an index into a number. This final step shows how the small functions you wrote one at a time add up to a working program.

Try it yourself

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

type Task struct {
	Name      string
	Completed bool
}

// TODO: Bring back addTask, viewAllTasks, completeTask and removeTask
// from the previous lessons of this project

func main() {
	// Create scanner for reading input lines
	scanner := bufio.NewScanner(os.Stdin)

	// Read the application name and the user name
	scanner.Scan()
	names := scanner.Text()

	// Read the tasks the list starts with
	scanner.Scan()
	taskData := scanner.Text()

	// Read the actions of the session
	scanner.Scan()
	actionData := scanner.Text()

	// TODO: Complete the implementation
	// 1. Split names on "," to get the app name and the user name
	// 2. Build the starting slice of tasks from taskData
	// 3. Print the welcome message, the menu and the current task count
	// 4. Run every action in actionData in order, printing its header first
}

All lessons in Logic & Flow

Practice on your own: Online Go compiler