diff --git a/x-tba/3-functions/xxx-project-pricings/main.go b/x-tba/3-functions/xxx-project-pricings/main.go index 23dd9e0..4185931 100644 --- a/x-tba/3-functions/xxx-project-pricings/main.go +++ b/x-tba/3-functions/xxx-project-pricings/main.go @@ -9,18 +9,24 @@ package main // Remember: This should point to the directory exactly after GOPATH // Use / not \ even on Windows -import "github.com/inancgumus/learngo/x-tba/3-functions/xxx-project-pricings/pricing" +import ( + "fmt" +) func main() { // TODO: funcs can call other funcs const data = `New York,100,2,1,100000 -New York,150,3,2,200000 -Paris,200,4,3,400000 -Istanbul,500,10,5,1000000` +Hong Kong,150,3,2,300000 +Paris,200,4,3,250000 +Istanbul,500,10,5,four hundred thousand` - props := pricing.New(data) - pricing.Print(props) + props, err := parse(data) + if err != nil { + fmt.Printf("> err: %s\n\n", err) + } + + show(props) // fmt.Printf("%#v\n", props.list) } diff --git a/x-tba/3-functions/xxx-project-pricings/parse.go b/x-tba/3-functions/xxx-project-pricings/parse.go new file mode 100644 index 0000000..f6c6f1a --- /dev/null +++ b/x-tba/3-functions/xxx-project-pricings/parse.go @@ -0,0 +1,84 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import ( + "strconv" + "strings" +) + +const separator = "," + +// Parse parses the given data and returns a slice of Properties +func parse(data string) (props []property, err error) { + rows := strings.Split(data, "\n") + + for _, row := range rows { + cols := strings.Split(row, separator) + + loc := cols[0] + + // size, err := strconv.Atoi(cols[1]) + // if err != nil { + // return props, err + // } + + // beds, err := strconv.Atoi(cols[2]) + // if err != nil { + // return props, err + // } + + // baths, err := strconv.Atoi(cols[3]) + // if err != nil { + // return props, err + // } + + // price, err := strconv.Atoi(cols[4]) + // if err != nil { + // return props, err + // } + + size, err := atoi(cols[1], err) + beds, err := atoi(cols[2], err) + baths, err := atoi(cols[3], err) + price, err := atoi(cols[4], err) + + if err != nil { + return props, err + } + + prop := property{ + location: loc, + size: size, + beds: beds, + baths: baths, + price: price, + } + + props = append(props, prop) + } + return +} + +// atoi is a helper for strconv.Atoi +// it saves the previous error to simplify the error handling. +// usage: +// n, err := atoi(p, err) +// m, err := atoi(q, err) +func atoi(s string, err error) (int, error) { + // if there was an error return it instead: skip the Atoi + if err != nil { + return 0, err + } + + n, lerr := strconv.Atoi(s) + if lerr != nil { + return 0, lerr + } + return n, nil +} diff --git a/x-tba/3-functions/xxx-project-pricings/pricing/parse_test.go b/x-tba/3-functions/xxx-project-pricings/parse_test.go similarity index 98% rename from x-tba/3-functions/xxx-project-pricings/pricing/parse_test.go rename to x-tba/3-functions/xxx-project-pricings/parse_test.go index ce4b6f4..8ad3938 100644 --- a/x-tba/3-functions/xxx-project-pricings/pricing/parse_test.go +++ b/x-tba/3-functions/xxx-project-pricings/parse_test.go @@ -1,4 +1,4 @@ -package pricing +package main import ( "reflect" diff --git a/x-tba/3-functions/xxx-project-pricings/pricing/parse.go b/x-tba/3-functions/xxx-project-pricings/pricing/parse.go deleted file mode 100644 index 18ce0a9..0000000 --- a/x-tba/3-functions/xxx-project-pricings/pricing/parse.go +++ /dev/null @@ -1,38 +0,0 @@ -// For more tutorials: https://blog.learngoprogramming.com -// -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// - -package pricing - -import ( - "strconv" - "strings" -) - -// named result values -func parse(data string) (props []Property) { - rows := strings.Split(data, "\n") - - for _, row := range rows { - cols := strings.Split(row, separator) - - size, _ := strconv.Atoi(cols[1]) - beds, _ := strconv.Atoi(cols[2]) - baths, _ := strconv.Atoi(cols[3]) - price, _ := strconv.Atoi(cols[4]) - - prop := Property{ - Location: cols[0], - Size: size, - Beds: beds, - Baths: baths, - Price: price, - } - - props = append(props, prop) - } - return -} diff --git a/x-tba/3-functions/xxx-project-pricings/pricing/property.go b/x-tba/3-functions/xxx-project-pricings/pricing/property.go deleted file mode 100644 index 56e8b02..0000000 --- a/x-tba/3-functions/xxx-project-pricings/pricing/property.go +++ /dev/null @@ -1,19 +0,0 @@ -// For more tutorials: https://blog.learngoprogramming.com -// -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// - -package pricing - -// Property stores the data about a property -type Property struct { - Location string - Size, Beds, Baths, Price int -} - -// Properties stores all the properties -type Properties struct { - list []Property -} diff --git a/x-tba/3-functions/xxx-project-pricings/pricing/print.go b/x-tba/3-functions/xxx-project-pricings/print.go similarity index 55% rename from x-tba/3-functions/xxx-project-pricings/pricing/print.go rename to x-tba/3-functions/xxx-project-pricings/print.go index 5e24292..0502aaa 100644 --- a/x-tba/3-functions/xxx-project-pricings/pricing/print.go +++ b/x-tba/3-functions/xxx-project-pricings/print.go @@ -5,29 +5,29 @@ // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ // -package pricing +package main import ( "fmt" "strings" ) -// Print prints the given properties -func Print(props Properties) { - printHeader() +// show shows the given properties +func show(props []property) { + showHeader() - for _, p := range props.list { - fmt.Printf("%-15s", p.Location) - fmt.Printf("%-15d", p.Size) - fmt.Printf("%-15d", p.Beds) - fmt.Printf("%-15d", p.Baths) - fmt.Printf("%-15d", p.Price) + for _, p := range props { + fmt.Printf("%-15s", p.location) + fmt.Printf("%-15d", p.size) + fmt.Printf("%-15d", p.beds) + fmt.Printf("%-15d", p.baths) + fmt.Printf("%-15d", p.price) fmt.Println() } } -// printHeader prints the header -func printHeader() { +// showHeader prints the header +func showHeader() { const header = "Location,Size,Beds,Baths,Price" for _, h := range strings.Split(header, separator) { diff --git a/x-tba/3-functions/xxx-project-pricings/pricing/pricing.go b/x-tba/3-functions/xxx-project-pricings/property.go similarity index 56% rename from x-tba/3-functions/xxx-project-pricings/pricing/pricing.go rename to x-tba/3-functions/xxx-project-pricings/property.go index c32b1c0..e779ba8 100644 --- a/x-tba/3-functions/xxx-project-pricings/pricing/pricing.go +++ b/x-tba/3-functions/xxx-project-pricings/property.go @@ -5,11 +5,10 @@ // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ // -package pricing +package main -const separator = "," - -// New parses and returns a new Properties data -func New(data string) Properties { - return Properties{parse(data)} +// property stores data about a property +type property struct { + location string + size, beds, baths, price int }