refactor user input

This commit is contained in:
agatha 2024-05-12 14:58:23 -04:00
parent e7a78a4d33
commit 61a4c0dee6

32
main.go
View File

@ -116,18 +116,31 @@ func (ctx *AppContext) AddNote(note Note) {
} }
func main() { func main() {
// TODO: Clean up menu prompt
// TODO: Write getNote method
// TODO: Write option handlers (handleNewNote, handleNewReading)
var appCtx AppContext var appCtx AppContext
err := appCtx.LoadRecords(recordsFilename) if err := appCtx.LoadRecords(recordsFilename); err != nil {
if err != nil { fmt.Printf("Error loading records from file: %s\n", err)
fmt.Printf("Error loading records: %s\n", err)
} }
// Display menu for {
choice, err := getUserInput("1. Enter new reading\n2. Enter new note\n3. Exit\nYour choice") choice, err := getUserInput("1. Enter new reading\n2. Enter new note\n3. Exit\nYour choice")
if err != nil {
fmt.Printf("Error reading choice: %s\n", err)
continue
}
if choice == "3" {
fmt.Println("Exiting...")
break
}
switch choice { switch choice {
case "1": case "1":
// Take a new reading from the user // Get a new Reading from the user
reading, err := getReading() reading, err := getReading()
if err != nil { if err != nil {
fmt.Printf("Error getting reading: %s\n", err) fmt.Printf("Error getting reading: %s\n", err)
@ -149,20 +162,21 @@ func main() {
appCtx.AddReading(reading) appCtx.AddReading(reading)
} }
case "2": case "2":
// Get a new Note from the user
noteText, err := getUserInput("Enter your note text: ") noteText, err := getUserInput("Enter your note text: ")
if err != nil { if err != nil {
fmt.Println("Error getting note: %s\n", err) fmt.Printf("Error getting note: %s\n", err)
} else { } else {
note := NewNote(noteText) note := NewNote(noteText)
appCtx.AddNote(note) appCtx.AddNote(note)
} }
default: default:
fmt.Println("Quitting...") fmt.Printf("Invalid choice, please try again.\n\n")
}
} }
// Save Records before exiting // Save Records before exiting
err = appCtx.SaveRecords(recordsFilename) if err := appCtx.SaveRecords(recordsFilename); err != nil {
if err != nil {
fmt.Printf("Error saving records to file: %s\n", err) fmt.Printf("Error saving records to file: %s\n", err)
} }
} }