Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions 2-calc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
"unicode"
)

var operationsMap = map[string]func([]float64) float64{
"sum": calcSum,
"avg": calcAVG,
"med": calcMed,
}

func main() {
var operation string
var err error
Expand All @@ -35,13 +41,12 @@ func main() {
}

func requestOperationType() (string, error) {
fmt.Println("Enter the name of the operation")
fmt.Println("sum - Calculate the amount")
fmt.Println("avg - Calculate the average")
fmt.Println("med - Calculate the median")

var userInput string
fmt.Scan(&userInput)
userInput := promptData(
"sum - Calculate the amount",
"avg - Calculate the average",
"med - Calculate the median",
"Enter the name of the operation",
)

if userInput != "sum" && userInput != "avg" && userInput != "med" {
return "", errors.New("Invalid operation type")
Expand All @@ -51,16 +56,13 @@ func requestOperationType() (string, error) {
}

func calculate(operationType string, operations []float64) (float64, error) {
switch operationType {
case "sum":
return calcSum(operations), nil
case "avg":
return calcAVG(operations), nil
case "med":
return calcMed(operations), nil
default:
operationFn := operationsMap[operationType]

if operationFn == nil {
return 0.0, errors.New("I can't do it.")
}

return operationFn(operations), nil
}

func requestOperations() []float64 {
Expand Down Expand Up @@ -122,3 +124,16 @@ func calcMed(slice []float64) float64 {
}

}

func promptData(content ...any) string {
var res string
for idx := range content {
if idx != len(content)-1 {
fmt.Println(content[idx])
continue
}
fmt.Printf("%v: ", content[idx])
}
fmt.Scanln(&res)
return res
}
1 change: 1 addition & 0 deletions jsonBin/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.json
.env
go.sum

9 changes: 9 additions & 0 deletions jsonBin/api/api.go
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
package api

import (
"jsonBin/config"
"jsonBin/print"
)

func Init(config *config.Config) {
print.Success(config)
}
24 changes: 24 additions & 0 deletions jsonBin/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package config

import (
"errors"
"jsonBin/print"
"os"

"github.com/joho/godotenv"
)

type Config struct {
Key string
}

func NewConfig() *Config {
err := godotenv.Load()
if err != nil {
print.Error(errors.New("no .env file found"))
}

return &Config{
Key: os.Getenv("KEY"),
}
}
22 changes: 16 additions & 6 deletions jsonBin/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ import (
"os"
)

func WriteFile(content []byte, name string) {
file, err := os.Create(name)
type FileStorage struct {
Filename string
}

func NewFileStorage(filename string) *FileStorage {
return &FileStorage{
Filename: filename,
}
}

func (fileStorage *FileStorage) Write(content []byte) {
file, err := os.Create(fileStorage.Filename)

if err != nil {
print.Error(err)
Expand All @@ -23,8 +33,8 @@ func WriteFile(content []byte, name string) {
print.Success("Success save file")
}

func ReadFile(name string) ([]byte, error) {
data, err := os.ReadFile(name)
func (fileStorage *FileStorage) Read() ([]byte, error) {
data, err := os.ReadFile(fileStorage.Filename)

if err != nil {
return nil, errors.New("Can't read file")
Expand All @@ -33,8 +43,8 @@ func ReadFile(name string) ([]byte, error) {
return data, nil
}

func IsJson(name string) (bool, error) {
data, err := ReadFile(name)
func (fileStorage *FileStorage) IsJson(name string) (bool, error) {
data, err := fileStorage.Read()
if err != nil {
return false, err
}
Expand Down
6 changes: 5 additions & 1 deletion jsonBin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ module jsonBin
go 1.23.1

require (
github.com/fatih/color v1.17.0 // indirect
github.com/fatih/color v1.17.0
github.com/joho/godotenv v1.5.1
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.18.0 // indirect
Expand Down
Binary file added jsonBin/jsonBin
Binary file not shown.
12 changes: 10 additions & 2 deletions jsonBin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ package main

import (
"fmt"
"jsonBin/api"
"jsonBin/bins"
"jsonBin/config"
"jsonBin/file"
"jsonBin/print"
"jsonBin/storage"
)

func main() {
var err error
binList := storage.GetBinList()
config := config.NewConfig()
api.Init(config)
fileStorage := file.NewFileStorage("bins.json")
binStorage := storage.NewStorage(fileStorage)

binList := binStorage.GetBinList()

name := promptBinName()

Expand All @@ -20,7 +28,7 @@ func main() {
return
}

storage.SaveBinList(binList)
binStorage.SaveBinList(binList)
}

func promptBinName() string {
Expand Down
5 changes: 3 additions & 2 deletions jsonBin/print/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func Prompt(prompt string, newLine bool) {
c.Print(prompt)
}

func Success(message string) {
color.Green(message)
func Success(message any) {
success := color.New(color.FgGreen)
success.Println(message)
}

func Data(text string, value any) {
Expand Down
24 changes: 19 additions & 5 deletions jsonBin/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,38 @@ import (
"encoding/json"
"errors"
"jsonBin/bins"
"jsonBin/file"
"jsonBin/print"
"time"
)

func SaveBinList(binList *bins.BinList) {
type Db interface {
Write ([]byte)
Read () ([]byte, error)
}

type Storage struct {
db Db
}

func NewStorage(db Db) *Storage {
return &Storage{
db: db,
}
}

func (storage *Storage) SaveBinList(binList *bins.BinList) {
binList.UpdatedAt = time.Now()
data, err := binList.ToBytes()

if err != nil {
print.Error(err)
return
}
file.WriteFile(data, "bins.json")
storage.db.Write(data)
}

func GetBinList() *bins.BinList {
data, err := file.ReadFile("bins.json")
func (storage *Storage) GetBinList() *bins.BinList {
data, err := storage.db.Read()
newBinList :=
&bins.BinList{
Bins: []bins.Bin{},
Expand Down