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
|
||||
// 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)
|
||||
}
|
||||
|
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 (
|
||||
"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/
|
||||
//
|
||||
|
||||
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) {
|
@ -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
|
||||
}
|
Reference in New Issue
Block a user