diff --git a/app.go b/investmentCalculator/app.go similarity index 100% rename from app.go rename to investmentCalculator/app.go diff --git a/investmentCalculator/go.mod b/investmentCalculator/go.mod new file mode 100644 index 0000000..8b5015c --- /dev/null +++ b/investmentCalculator/go.mod @@ -0,0 +1,3 @@ +module example.com/investment-calculator + +go 1.23.1 diff --git a/structs-practice/go.mod b/structs-practice/go.mod new file mode 100644 index 0000000..fc39288 --- /dev/null +++ b/structs-practice/go.mod @@ -0,0 +1,3 @@ +module example.com/note + +go 1.23.1 diff --git a/structs-practice/main.go b/structs-practice/main.go new file mode 100644 index 0000000..30144fa --- /dev/null +++ b/structs-practice/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "bufio" + "fmt" + "example.com/note/note" + "os" + "strings" +) + +func main() { + title, content := getNoteData() + + userNote, err := note.New(title, content) + + if err != nil { + fmt.Println(err) + return + } + + userNote.Display() + err = userNote.Save() + + if err != nil { + fmt.Println("Saving the note failed") + return + } + + fmt.Println("Saving the note succeeded!") +} + +func getNoteData() (string, string) { + title := getUserInput("Note title:") + + content := getUserInput("Note content:") + + return title, content +} + +func getUserInput(prompt string) (string) { + fmt.Printf("%v ", prompt) + + reader := bufio.NewReader(os.Stdin) + + text, err :=reader.ReadString('\n') + + if err != nil { + return "" + } + + text = strings.TrimSuffix(text, "\n") + text = strings.TrimSuffix(text, "\r") + + return text +} \ No newline at end of file diff --git a/structs-practice/note/note.go b/structs-practice/note/note.go new file mode 100644 index 0000000..7af9a9a --- /dev/null +++ b/structs-practice/note/note.go @@ -0,0 +1,45 @@ +package note + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "strings" + "time" +) + +type Note struct { + Title string `json:"title"` + Content string `json:"content"` + CreatedAt time.Time `json:"created_at"` +} + +func (note Note) Display() { + fmt.Printf("Your note title %v has the following content:\n\n%v", note.Title, note.Content) +} + +func (note Note) Save() error { + fileName := strings.ReplaceAll(note.Title, " ", "_") + fileName = strings.ToLower(fileName) + ".json" + + json, err := json.Marshal(note) + + if err != nil { + return err + } + + return os.WriteFile(fileName, json, 0644) +} + +func New(title, content string) (Note, error) { + if title == "" || content == "" { + return Note{}, errors.New("Invalid input") + } + + return Note{ + Title: title, + Content: content, + CreatedAt: time.Now(), + }, nil +} \ No newline at end of file diff --git a/structs.go b/structs.go deleted file mode 100644 index b9f5807..0000000 --- a/structs.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "fmt" -) - -func main() { - firstName := getUserData("Please enter your first name: ") - lastName := getUserData("Please enter your last name: ") - birthdate := getUserData("Please enter your birthdate (MM/DD/YYYY): ") - - // ... do something awesome with that gathered data! - - fmt.Println(firstName, lastName, birthdate) -} - -func getUserData(promptText string) string { - fmt.Print(promptText) - var value string - fmt.Scan(&value) - return value -} diff --git a/go.mod b/structs/go.mod similarity index 100% rename from go.mod rename to structs/go.mod diff --git a/structs/structs.go b/structs/structs.go new file mode 100644 index 0000000..4ab1484 --- /dev/null +++ b/structs/structs.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "example.com/structs/user" +) + +func main() { + userFirstName := getUserData("Please enter your first name: ") + userLastName := getUserData("Please enter your last name: ") + userBirthdate := getUserData("Please enter your birthdate (MM/DD/YYYY): ") + + var appUser *user.User + + appUser, err := user.New(userFirstName, userLastName, userBirthdate) + + if err != nil { + fmt.Println(err) + return + } + + admin := user.NewAdmin("test@example.com", "test123") + + admin.OutputUserDetails() + admin.ClearUserName() + admin.OutputUserDetails() + + // ... do something awesome with that gathered data! + + appUser.OutputUserDetails() + appUser.ClearUserName() + appUser.OutputUserDetails() +} + +func getUserData(promptText string) string { + fmt.Print(promptText) + var value string + fmt.Scanln(&value) + return value +} diff --git a/structs/user/user.go b/structs/user/user.go new file mode 100644 index 0000000..06a0325 --- /dev/null +++ b/structs/user/user.go @@ -0,0 +1,55 @@ +package user + +import ( + "errors" + "fmt" + "time" +) + +type User struct { + firstName string + lastName string + birthdate string + createdAt time.Time +} + +type Admin struct { + email string + password string + User +} + +func (u User) OutputUserDetails() { + fmt.Println(u.firstName, u.lastName, u.birthdate) +} + +func (u *User) ClearUserName() { + u.firstName = "" + u.lastName = "" +} + +func NewAdmin(email, password string) Admin { + return Admin{ + email: email, + password: password, + User: User{ + firstName: "ADMIN", + lastName: "ADMIN", + birthdate: "---", + createdAt: time.Now(), + }, + } +} + +func New(firstName, lastName, birthdate string) (*User, error) { + if firstName == "" || lastName == "" || birthdate == "" { + return nil, errors.New("First name, last name, and birthdate are required") + } + + return &User{ + firstName: firstName, + lastName: lastName, + birthdate: birthdate, + createdAt: time.Now(), + }, nil +} \ No newline at end of file