refactor user input

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

88
main.go
View File

@ -116,53 +116,67 @@ 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")
switch choice {
case "1":
// Take a new reading from the user
reading, err := getReading()
if err != nil { if err != nil {
fmt.Printf("Error getting reading: %s\n", err) fmt.Printf("Error reading choice: %s\n", err)
} else { continue
// Display GKI and level of ketosis }
fmt.Printf("\nYour GKI is: %0.2f\n", reading.GKI)
switch { if choice == "3" {
case reading.GKI <= 1: fmt.Println("Exiting...")
fmt.Println("You're in the highest level of ketosis.") break
case reading.GKI < 3: }
fmt.Println("You're in a high therapeutic level of ketosis.")
case reading.GKI < 6: switch choice {
fmt.Println("You're in a moderate level of ketosis.") case "1":
case reading.GKI <= 9: // Get a new Reading from the user
fmt.Println("You're in a low level of ketosis.") reading, err := getReading()
default: if err != nil {
fmt.Println("You are not in ketosis.") fmt.Printf("Error getting reading: %s\n", err)
} else {
// Display GKI and level of ketosis
fmt.Printf("\nYour GKI is: %0.2f\n", reading.GKI)
switch {
case reading.GKI <= 1:
fmt.Println("You're in the highest level of ketosis.")
case reading.GKI < 3:
fmt.Println("You're in a high therapeutic level of ketosis.")
case reading.GKI < 6:
fmt.Println("You're in a moderate level of ketosis.")
case reading.GKI <= 9:
fmt.Println("You're in a low level of ketosis.")
default:
fmt.Println("You are not in ketosis.")
}
appCtx.AddReading(reading)
} }
appCtx.AddReading(reading) case "2":
// Get a new Note from the user
noteText, err := getUserInput("Enter your note text: ")
if err != nil {
fmt.Printf("Error getting note: %s\n", err)
} else {
note := NewNote(noteText)
appCtx.AddNote(note)
}
default:
fmt.Printf("Invalid choice, please try again.\n\n")
} }
case "2":
noteText, err := getUserInput("Enter your note text: ")
if err != nil {
fmt.Println("Error getting note: %s\n", err)
} else {
note := NewNote(noteText)
appCtx.AddNote(note)
}
default:
fmt.Println("Quitting...")
} }
// 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)
} }
} }