refactor user input
This commit is contained in:
parent
e7a78a4d33
commit
61a4c0dee6
88
main.go
88
main.go
@ -116,53 +116,67 @@ func (ctx *AppContext) AddNote(note Note) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// TODO: Clean up menu prompt
|
||||
// TODO: Write getNote method
|
||||
// TODO: Write option handlers (handleNewNote, handleNewReading)
|
||||
|
||||
var appCtx AppContext
|
||||
|
||||
err := appCtx.LoadRecords(recordsFilename)
|
||||
if err != nil {
|
||||
fmt.Printf("Error loading records: %s\n", err)
|
||||
if err := appCtx.LoadRecords(recordsFilename); err != nil {
|
||||
fmt.Printf("Error loading records from file: %s\n", err)
|
||||
}
|
||||
|
||||
// Display menu
|
||||
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()
|
||||
for {
|
||||
choice, err := getUserInput("1. Enter new reading\n2. Enter new note\n3. Exit\nYour choice")
|
||||
if err != nil {
|
||||
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.")
|
||||
fmt.Printf("Error reading choice: %s\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if choice == "3" {
|
||||
fmt.Println("Exiting...")
|
||||
break
|
||||
}
|
||||
|
||||
switch choice {
|
||||
case "1":
|
||||
// Get a new Reading from the user
|
||||
reading, err := getReading()
|
||||
if err != nil {
|
||||
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
|
||||
err = appCtx.SaveRecords(recordsFilename)
|
||||
if err != nil {
|
||||
if err := appCtx.SaveRecords(recordsFilename); err != nil {
|
||||
fmt.Printf("Error saving records to file: %s\n", err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user