This commit is contained in:
2024-10-23 12:10:32 +00:00
parent c7e45139ec
commit a8148f2400
8 changed files with 230 additions and 5 deletions

View File

@@ -2,31 +2,72 @@ package main
import (
"bufio"
"fmt"
"example.com/note/note"
"example.com/note/todo"
"fmt"
"os"
"strings"
)
type saver interface {
Save() error
}
// type displayer interface {
// Display()
// }
type outputtable interface {
saver
Display()
}
// type outputtable interface {
// Save() error
// Display()
// }
func main() {
title, content := getNoteData()
todoText := getUserInput("Todo text: ")
userNote, err := note.New(title, content)
todo, err := todo.New(todoText)
if err != nil {
fmt.Println(err)
return
}
userNote.Display()
err = userNote.Save()
userNote, err := note.New(title, content)
if err != nil {
fmt.Println("Saving the note failed")
return
}
err = outputData(todo)
if err != nil {
return
}
outputData(userNote)
}
func outputData(data outputtable) error {
data.Display()
return saveData(data)
}
func saveData(data saver) error {
err := data.Save()
if err != nil {
fmt.Println("Saving the note failed")
return err
}
fmt.Println("Saving the note succeeded!")
return nil
}
func getNoteData() (string, string) {

View File

@@ -0,0 +1,38 @@
package todo
import (
"encoding/json"
"errors"
"fmt"
"os"
)
type Todo struct {
Text string `json:"text"`
}
func (todo Todo) Display() {
fmt.Printf(todo.Text)
}
func (todo Todo) Save() error {
fileName := "todo.json"
json, err := json.Marshal(todo)
if err != nil {
return err
}
return os.WriteFile(fileName, json, 0644)
}
func New(content string) (Todo, error) {
if content == "" || content == "" {
return Todo{}, errors.New("Invalid input")
}
return Todo{
Text: content,
}, nil
}