diff --git a/bank/bank.go b/bank/bank.go index 5e481da..94ebdfe 100644 --- a/bank/bank.go +++ b/bank/bank.go @@ -1,36 +1,17 @@ package main -import "errors" -import "fmt" -import "os" -import "strconv" +import ( + "example.com/bank/fileops" + "fmt" + "github.com/Pallinder/go-randomdata" +) const accountBalanceFile = "balance.txt" -func getBalanceFromFile() (float64, error) { - data, err := os.ReadFile(accountBalanceFile, ) - if err != nil { - return 1000, errors.New("Failed to find balance file.") - } - - balanceText := string(data) - balance, err :=strconv.ParseFloat(balanceText, 64) - - if err != nil { - return 1000, errors.New("Failed to parse stored balance value.") - } - - return balance, nil -} - -func writeBalanceToFile(balance float64) { - balanceText := fmt.Sprint(balance) - os.WriteFile(accountBalanceFile, []byte(balanceText), 0644) -} func main() { - var accountBalance, err = getBalanceFromFile() + var accountBalance, err = fileops.GetFloatFromFile(accountBalanceFile) if err != nil { fmt.Println("ERROR") @@ -39,13 +20,10 @@ func main() { } fmt.Println("Welcome to Go Bank!") + fmt.Println(randomdata.PhoneNumber()) for { - fmt.Println("What do you want to do?") - fmt.Println("1. Check balance") - fmt.Println("2. Deposit money") - fmt.Println("3. Withdraw money") - fmt.Println("4. Exit") + presentOptions() var choice int fmt.Print("Your choice: ") @@ -66,7 +44,7 @@ func main() { accountBalance += accountBalance fmt.Println("Balance updated! New amount:", accountBalance) - writeBalanceToFile(accountBalance) + fileops.WriteFloatToFile(accountBalance, accountBalanceFile) case 3: fmt.Print("Your withdrawal") var withdrawalAmount float64 @@ -84,7 +62,7 @@ func main() { accountBalance -= withdrawalAmount fmt.Println("Balance updated! New amount:", accountBalance) - writeBalanceToFile(accountBalance) + fileops.WriteFloatToFile(accountBalance, accountBalanceFile) default: fmt.Println("Goodbye!") fmt.Println("Thanks for choosing our bank") diff --git a/bank/communication.go b/bank/communication.go new file mode 100644 index 0000000..402bb65 --- /dev/null +++ b/bank/communication.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func presentOptions() { + fmt.Println("What do you want to do?") + fmt.Println("1. Check balance") + fmt.Println("2. Deposit money") + fmt.Println("3. Withdraw money") + fmt.Println("4. Exit") +} \ No newline at end of file diff --git a/bank/fileops/fileops.go b/bank/fileops/fileops.go new file mode 100644 index 0000000..205bc22 --- /dev/null +++ b/bank/fileops/fileops.go @@ -0,0 +1,30 @@ +package fileops + +import ( + "errors" + "fmt" + "os" + "strconv" +) + +func GetFloatFromFile(fileName string) (float64, error) { + data, err := os.ReadFile(fileName) + + if err != nil { + return 1000, errors.New("Failed to find file.") + } + + valueText := string(data) + value, err :=strconv.ParseFloat(valueText, 64) + + if err != nil { + return 1000, errors.New("Failed to parse stored value.") + } + + return value, nil +} + +func WriteFloatToFile(value float64, fileName string) { + valueText := fmt.Sprint(value) + os.WriteFile(fileName, []byte(valueText), 0644) +} \ No newline at end of file diff --git a/bank/go.mod b/bank/go.mod index 401aede..02533b1 100644 --- a/bank/go.mod +++ b/bank/go.mod @@ -1,3 +1,5 @@ module example.com/bank go 1.23.1 + +require github.com/Pallinder/go-randomdata v1.2.0 // indirect diff --git a/bank/go.sum b/bank/go.sum new file mode 100644 index 0000000..b8cf38f --- /dev/null +++ b/bank/go.sum @@ -0,0 +1,2 @@ +github.com/Pallinder/go-randomdata v1.2.0 h1:DZ41wBchNRb/0GfsePLiSwb0PHZmT67XY00lCDlaYPg= +github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y= diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b8cf38f --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/Pallinder/go-randomdata v1.2.0 h1:DZ41wBchNRb/0GfsePLiSwb0PHZmT67XY00lCDlaYPg= +github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y= diff --git a/pointers/go.mod b/pointers/go.mod new file mode 100644 index 0000000..f5278a8 --- /dev/null +++ b/pointers/go.mod @@ -0,0 +1,3 @@ +module example.com/pointers + +go 1.23.1 diff --git a/pointers/pointers.go b/pointers/pointers.go new file mode 100644 index 0000000..c3661c4 --- /dev/null +++ b/pointers/pointers.go @@ -0,0 +1,22 @@ +package main + +import "fmt" + +func main() { + age := 32 + + var agePointer *int + + agePointer = &age + + fmt.Println("Age:", *agePointer) + + editAgeToAdultYears(agePointer) + fmt.Println(age) +} + +func editAgeToAdultYears(age *int) { + // return *age - 18 + + *age = *age - 18 +} \ No newline at end of file