move: pricing funcs project to x-tba
This commit is contained in:
@ -9,18 +9,24 @@ package main
|
|||||||
|
|
||||||
// Remember: This should point to the directory exactly after GOPATH
|
// Remember: This should point to the directory exactly after GOPATH
|
||||||
// Use / not \ even on Windows
|
// Use / not \ even on Windows
|
||||||
import "github.com/inancgumus/learngo/x-tba/3-functions/xxx-project-pricings/pricing"
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// TODO: funcs can call other funcs
|
// TODO: funcs can call other funcs
|
||||||
|
|
||||||
const data = `New York,100,2,1,100000
|
const data = `New York,100,2,1,100000
|
||||||
New York,150,3,2,200000
|
Hong Kong,150,3,2,300000
|
||||||
Paris,200,4,3,400000
|
Paris,200,4,3,250000
|
||||||
Istanbul,500,10,5,1000000`
|
Istanbul,500,10,5,four hundred thousand`
|
||||||
|
|
||||||
props := pricing.New(data)
|
props, err := parse(data)
|
||||||
pricing.Print(props)
|
if err != nil {
|
||||||
|
fmt.Printf("> err: %s\n\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
show(props)
|
||||||
|
|
||||||
// fmt.Printf("%#v\n", props.list)
|
// fmt.Printf("%#v\n", props.list)
|
||||||
}
|
}
|
||||||
|
84
x-tba/3-functions/xxx-project-pricings/parse.go
Normal file
84
x-tba/3-functions/xxx-project-pricings/parse.go
Normal file
@ -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
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package pricing
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
@ -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
|
|
||||||
}
|
|
@ -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
|
|
||||||
}
|
|
@ -5,29 +5,29 @@
|
|||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
//
|
//
|
||||||
|
|
||||||
package pricing
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Print prints the given properties
|
// show shows the given properties
|
||||||
func Print(props Properties) {
|
func show(props []property) {
|
||||||
printHeader()
|
showHeader()
|
||||||
|
|
||||||
for _, p := range props.list {
|
for _, p := range props {
|
||||||
fmt.Printf("%-15s", p.Location)
|
fmt.Printf("%-15s", p.location)
|
||||||
fmt.Printf("%-15d", p.Size)
|
fmt.Printf("%-15d", p.size)
|
||||||
fmt.Printf("%-15d", p.Beds)
|
fmt.Printf("%-15d", p.beds)
|
||||||
fmt.Printf("%-15d", p.Baths)
|
fmt.Printf("%-15d", p.baths)
|
||||||
fmt.Printf("%-15d", p.Price)
|
fmt.Printf("%-15d", p.price)
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// printHeader prints the header
|
// showHeader prints the header
|
||||||
func printHeader() {
|
func showHeader() {
|
||||||
const header = "Location,Size,Beds,Baths,Price"
|
const header = "Location,Size,Beds,Baths,Price"
|
||||||
|
|
||||||
for _, h := range strings.Split(header, separator) {
|
for _, h := range strings.Split(header, separator) {
|
@ -5,11 +5,10 @@
|
|||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
//
|
//
|
||||||
|
|
||||||
package pricing
|
package main
|
||||||
|
|
||||||
const separator = ","
|
// property stores data about a property
|
||||||
|
type property struct {
|
||||||
// New parses and returns a new Properties data
|
location string
|
||||||
func New(data string) Properties {
|
size, beds, baths, price int
|
||||||
return Properties{parse(data)}
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user