diff --git a/hw02_fix_app/go.mod b/hw02_fix_app/go.mod index cd922a2..949c832 100644 --- a/hw02_fix_app/go.mod +++ b/hw02_fix_app/go.mod @@ -1,5 +1,3 @@ module github.com/fixme_my_friend/hw02_fix_app go 1.20 - -require golang.org/x/example v0.0.0-20230714141244-83a29069fa80 // indirect diff --git a/hw02_fix_app/go.sum b/hw02_fix_app/go.sum index 46ddcb7..e69de29 100644 --- a/hw02_fix_app/go.sum +++ b/hw02_fix_app/go.sum @@ -1,2 +0,0 @@ -golang.org/x/example v0.0.0-20230714141244-83a29069fa80 h1:dIO+42L/3wms+HG8CICCnwjAoQ9K7FD3JodZPpG5ymY= -golang.org/x/example v0.0.0-20230714141244-83a29069fa80/go.mod h1:DJ5Pz7jIW3ezl8PmYmOf5B1CZ98NJv/GaEcafLB3Lzk= diff --git a/hw02_fix_app/main.go b/hw02_fix_app/main.go index 45593d4..3cc7d61 100644 --- a/hw02_fix_app/main.go +++ b/hw02_fix_app/main.go @@ -1,14 +1,44 @@ -package init +package main import ( + "fmt" + "github.com/fixme_my_friend/hw02_fix_app/printer" "github.com/fixme_my_friend/hw02_fix_app/reader" - "github.com/fixme_my_friend/hw02_fix_app/types" +) + +func main() { + path := "data.json" + + fmt.Printf("Enter data file path (press Enter to use default): ") + fmt.Scanln(&path) + + if len(path) == 0 { + path = "data.json" + } + + staff, err := reader.ReadJSON(path, -1) + if err != nil { + fmt.Printf("Error reading JSON: %v\n", err) + return + } + + printer.PrintStaff(staff) +} + +/* +package main + +import ( "fmt" + + "github.com/fixme_my_friend/hw02_fix_app/printer" + "github.com/fixme_my_friend/hw02_fix_app/reader" + "github.com/fixme_my_friend/hw02_fix_app/types" ) -func init() { - var path string = "data.json" +func main() { + var path = "data.json" fmt.Printf("Enter data file path: ") fmt.Scanln(&path) @@ -18,7 +48,6 @@ func init() { if len(path) == 0 { path = "data.json" - } else { } staff, err = reader.ReadJSON(path, -1) @@ -27,3 +56,4 @@ func init() { printer.PrintStaff(staff) } +*/ diff --git a/hw02_fix_app/printer/pkg.go b/hw02_fix_app/printer/pkg.go index 8fa2b33..1d89fd7 100644 --- a/hw02_fix_app/printer/pkg.go +++ b/hw02_fix_app/printer/pkg.go @@ -1,15 +1,34 @@ package printer import ( + "fmt" + + "github.com/fixme_my_friend/hw02_fix_app/types" +) + +func PrintStaff(staff []types.Employee) { + for _, s := range staff { + fmt.Println(s.String()) + } +} + +/* +package printer + +import ( + "fmt" + "github.com/fixme_my_friend/hw02_fix_app/types" ) -func PrintStaff(staff types.Employee) { +func PrintStaff(staff []types.Employee) { var str string for i := 0; i < len(staff); i++ { - str := fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", staff[i].UserID, staff[i].Age, staff[i].Name, staff[i].DepartmentID) + str := fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", + staff[i].UserID, staff[i].Age, staff[i].Name, staff[i].DepartmentID) fmt.Println(str) } fmt.Println(str) } +*/ diff --git a/hw02_fix_app/reader/pkg.go b/hw02_fix_app/reader/pkg.go index 7adeb97..085e33a 100644 --- a/hw02_fix_app/reader/pkg.go +++ b/hw02_fix_app/reader/pkg.go @@ -1,12 +1,51 @@ package reader -import "encoding/json" -import "fmt" -import "io" -import "os" +import ( + "encoding/json" + "fmt" + "io" + "os" -import "github.com/fixme_my_friend/hw02_fix_app/types" + "github.com/fixme_my_friend/hw02_fix_app/types" +) +func ReadJSON(filePath string, limit int) ([]types.Employee, error) { + f, err := os.Open(filePath) + if err != nil { + return nil, fmt.Errorf("failed to open file: %w", err) + } + defer f.Close() + + bytes, err := io.ReadAll(f) + if err != nil { + return nil, fmt.Errorf("failed to read file: %w", err) + } + + var data []types.Employee + + err = json.Unmarshal(bytes, &data) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal JSON: %w", err) + } + + // если limit >= 0, обрезаем + if limit >= 0 && limit < len(data) { + data = data[:limit] + } + + return data, nil +} + +/*package reader + +import ( + "encoding/json" + "fmt" + "io" + "os" + + "github.com/fixme_my_friend/hw02_fix_app/types" +) func ReadJSON(filePath string, limit int) ([]types.Employee, error) { f, err := os.Open(filePath) @@ -14,7 +53,7 @@ func ReadJSON(filePath string, limit int) ([]types.Employee, error) { fmt.Printf("Error: %v", err) } - byte, err := io.ReadAll(f) + bytes, err := io.ReadAll(f) if err != nil { fmt.Printf("Error: %v", err) return nil, nil @@ -28,3 +67,4 @@ func ReadJSON(filePath string, limit int) ([]types.Employee, error) { return res, nil } +*/ diff --git a/hw02_fix_app/types/employee.go b/hw02_fix_app/types/employee.go index 08b0a04..8a10b4b 100644 --- a/hw02_fix_app/types/employee.go +++ b/hw02_fix_app/types/employee.go @@ -3,12 +3,29 @@ package types import "fmt" type Employee struct { - UserID int `json:"user_id"` + UserID int `json:"userId"` Age int `json:"age"` Name string `json:"name"` - DepartmentID int `json:"department_id"` + DepartmentID int `json:"departmentId"` } func (e Employee) String() string { - return fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", e.UserID, e.Age, e.FirstName, e.DepartmentID) + return fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d", e.UserID, e.Age, e.Name, e.DepartmentID) } + +/* +package types + +import "fmt" + +type Employee struct { + UserID int `json:"userId"` + Age int `json:"age"` + Name string `json:"name"` + DepartmentID int `json:"departmentId"` +} + +func (e Employee) String() string { + return fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", e.UserID, e.Age, e.Name, e.DepartmentID) +} +*/