Skip to content

Commit

Permalink
refactoring + average price of gold ang currency
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaskulski committed Dec 13, 2020
1 parent 5afdd36 commit 1bc4ea1
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 89 deletions.
90 changes: 45 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ More examples in the \ example folder.
- GetTableByDay
- GetTableCByDay
* methods that return data (downloaded previously by TableByDate, or TableLast methods) as text, ready to be printed or saved to a file:
- GetPrettyOutput
- GetCSVOutput
- GetRawOutput
- CreatePrettyOutput
- CreateCSVOutput
- CreateRawOutput
* other methods:
- SetTableType
2. **NBPCurrency** type - particular currency exchange rates:
Expand All @@ -125,9 +125,9 @@ More examples in the \ example folder.
- GetRateToday
- GetRateByDate
* methods that return data (downloaded previously) as text, ready to be printed or saved to a file:
- GetPrettyOutput
- GetCSVOutput
- GetRawOutput
- CreatePrettyOutput
- CreateCSVOutput
- CreateRawOutput
* other methods:
- SetTableType
3. **NBPGold** type - gold prices:
Expand All @@ -141,9 +141,9 @@ More examples in the \ example folder.
- GetPriceCurrent
- GetPriceByDate
* methods that return data (downloaded previously) as text, ready to be printed or saved to a file:
- GetPrettyOutput
- GetCSVOutput
- GetRawOutput
- CreatePrettyOutput
- CreateCSVOutput
- CreateRawOutput


Detailed documentation:
Expand Down Expand Up @@ -231,6 +231,28 @@ type NBPCurrency struct {
func NewCurrency(tableType string) *NBPCurrency
NewCurrency - function creates new currency type
func (c *NBPCurrency) CreateCSVOutput(lang string) string
CreateCSVOutput - function returns currency rates, in the form of CSV (data
separated by a comma), depending on the tableType field: for type A and B
tables a column with an average rate is printed, for type C two columns: buy
price and sell price
Parameters:
lang - 'en' or 'pl'
func (c *NBPCurrency) CreatePrettyOutput(lang string) string
CreatePrettyOutput - function returns exchange rates as formatted table
depending on the tableType field: for type A and B tables a column with an
average rate is printed, for type C two columns: buy price and sell price
Parameters:
lang - 'en' or 'pl'
func (c *NBPCurrency) CreateRawOutput() string
CreateRawOutput - function print just result of request (json or xml)
func (c *NBPCurrency) CurrencyByDate(code, date string) error
CurrencyByDate - function downloads and writes data to exchange (exchangeC)
slice, raw data (json) still available in result field
Expand Down Expand Up @@ -294,25 +316,6 @@ func (c *NBPCurrency) CurrencyToday(code string) error
code - ISO 4217 currency code, depending on the type of the
table available currencies may vary
func (c *NBPCurrency) GetCSVOutput(lang string) string
GetCSVOutput - function returns currency rates, in the form of CSV (data
separated by a comma), depending on the tableType field: for type A and B
tables a column with an average rate is printed, for type C two columns: buy
price and sell price
Parameters:
lang - 'en' or 'pl'
func (c *NBPCurrency) GetPrettyOutput(lang string) string
GetPrettyOutput - function returns exchange rates as formatted table
depending on the tableType field: for type A and B tables a column with an
average rate is printed, for type C two columns: buy price and sell price
Parameters:
lang - 'en' or 'pl'
func (c *NBPCurrency) GetRateByDate(code, date string) ([]Rate, error)
GetRateByDate - function downloads today's currency exchange rate and
returns slice of Rate struct (or error)
Expand Down Expand Up @@ -344,9 +347,6 @@ func (c *NBPCurrency) GetRateToday(code string) (Rate, error)
code - ISO 4217 currency code, depending on the type of the
table available currencies may vary
func (c *NBPCurrency) GetRawOutput() string
GetRawOutput - function print just result of request (json or xml)
func (c *NBPCurrency) SetTableType(tableType string) error
SetTableType - the function allows to set the supported type of exchange
rate table
Expand All @@ -362,21 +362,24 @@ type NBPGold struct {
func NewGold() *NBPGold
NewGold - function creates new gold type
func (g *NBPGold) GetCSVOutput(lang string) string
GetCSVOutput - function returns prices of gold in CSV format (comma
func (g *NBPGold) CreateCSVOutput(lang string) string
CreateCSVOutput - function returns prices of gold in CSV format (comma
separated data)
Parameters:
lang - 'en' or 'pl'
func (g *NBPGold) GetPrettyOutput(lang string) string
GetPrettyOutput - function returns a formatted table of gold prices
func (g *NBPGold) CreatePrettyOutput(lang string) string
CreatePrettyOutput - function returns a formatted table of gold prices
Parameters:
lang - 'en' or 'pl'
func (g *NBPGold) CreateRawOutput() string
CreateRawOutput - function returns just result of request (json or xml)
func (g *NBPGold) GetPriceByDate(date string) ([]GoldRate, error)
GetPriceByDate - function returns gold prices (as slice of struct), by date
("YYYY-MM-DD") or range of dates ("YYYY-MM-DD:YYYY-MM-DD")
Expand All @@ -395,9 +398,6 @@ func (g *NBPGold) GetPriceToday() (GoldRate, error)
GetPriceToday - function downloads and returns today's gold price, as
GoldRate struct
func (g *NBPGold) GetRawOutput() string
GetRawOutput - function returns just result of request (json or xml)
func (g *NBPGold) GoldByDate(date string) error
GoldByDate - function downloads and writes data to goldRates slice, raw data
(json) still available in NBPGold.result field
Expand Down Expand Up @@ -451,18 +451,18 @@ type NBPTable struct {
func NewTable(tableType string) *NBPTable
NewTable - function creates new table type
func (t *NBPTable) GetCSVOutput(lang string) string
GetCSVOutput - function prints tables of exchange rates in the console, in
the form of CSV (data separated by a comma), depending on the tableType
func (t *NBPTable) CreateCSVOutput(lang string) string
CreateCSVOutput - function prints tables of exchange rates in the console,
in the form of CSV (data separated by a comma), depending on the tableType
field: for type A and B tables a column with an average rate is printed, for
type C two columns: buy price and sell price
Parameters:
lang - 'en' or 'pl'
func (t *NBPTable) GetPrettyOutput(lang string) string
GetPrettyOutput - function returns tables of exchange rates as formatted
func (t *NBPTable) CreatePrettyOutput(lang string) string
CreatePrettyOutput - function returns tables of exchange rates as formatted
table, depending on the tableType field: for type A and B tables a column
with an average rate is printed, for type C two columns: buy price and sell
price
Expand All @@ -471,8 +471,8 @@ func (t *NBPTable) GetPrettyOutput(lang string) string
lang - 'en' or 'pl'
func (t *NBPTable) GetRawOutput() string
GetRawOutput - function returns just result of request (json or xml)
func (t *NBPTable) CreateRawOutput() string
CreateRawOutput - function returns just result of request (json or xml)
func (t *NBPTable) GetTableByDate(date string) ([]ExchangeTable, error)
GetTableByDate - function retrieves a table of exchange rates for a given
Expand Down
34 changes: 28 additions & 6 deletions currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func (c *NBPCurrency) GetRateByDate(code, date string) ([]Rate, error) {
}

/*
GetPrettyOutput - function returns exchange rates as formatted table
CreatePrettyOutput - function returns exchange rates as formatted table
depending on the tableType field:
for type A and B tables a column with an average rate is printed,
for type C two columns: buy price and sell price
Expand All @@ -356,10 +356,11 @@ Parameters:
lang - 'en' or 'pl'
*/
func (c *NBPCurrency) GetPrettyOutput(lang string) string {
func (c *NBPCurrency) CreatePrettyOutput(lang string) string {
const padding = 3
var builder strings.Builder
var output string
var total, totalBuy, totalSell float64

// output language
setLang(lang)
Expand All @@ -377,8 +378,16 @@ func (c *NBPCurrency) GetPrettyOutput(lang string) string {
fmt.Fprintln(w, l.Get("----- \t ---- \t -------------"))
for _, currencyItem := range c.Exchange.Rates {
currencyValue := fmt.Sprintf("%.4f", currencyItem.Mid)
total += currencyItem.Mid
fmt.Fprintln(w, currencyItem.No+" \t "+currencyItem.EffectiveDate+" \t "+currencyValue)
}

if len(c.Exchange.Rates) > 1 {
avg := total / float64(len(c.Exchange.Rates))
fmt.Fprintln(w)
fmt.Fprintln(w, l.Get("The average exchange rate (from downloaded): "), fmt.Sprintf("%.4f", avg))
fmt.Fprintln(w)
}
} else {
output += fmt.Sprintln()
output += fmt.Sprintln(l.Get("Table type:")+"\t", c.ExchangeC.Table)
Expand All @@ -391,16 +400,29 @@ func (c *NBPCurrency) GetPrettyOutput(lang string) string {
for _, currencyItem := range c.ExchangeC.Rates {
currencyValueBid := fmt.Sprintf("%.4f", currencyItem.Bid)
currencyValueAsk := fmt.Sprintf("%.4f", currencyItem.Ask)
totalBuy += currencyItem.Bid
totalSell += currencyItem.Ask
fmt.Fprintln(w, currencyItem.No+" \t "+currencyItem.EffectiveDate+" \t "+currencyValueBid+" \t "+currencyValueAsk)
}

if len(c.ExchangeC.Rates) > 1 {
avgBuy := totalBuy / float64(len(c.ExchangeC.Rates))
avgSell := totalSell / float64(len(c.ExchangeC.Rates))

fmt.Fprintln(w)
fmt.Fprintln(w, l.Get("Buy: the average exchange rate (from downloaded): "), fmt.Sprintf("%.4f", avgBuy))
fmt.Fprintln(w, l.Get("Sell: the average exchange rate (from downloaded): "), fmt.Sprintf("%.4f", avgSell))
fmt.Fprintln(w)
}
}

w.Flush()

return output + builder.String()
}

/*
GetCSVOutput - function returns currency rates,
CreateCSVOutput - function returns currency rates,
in the form of CSV (data separated by a comma), depending on the
tableType field: for type A and B tables a column with an average
rate is printed, for type C two columns: buy price and sell price
Expand All @@ -409,7 +431,7 @@ Parameters:
lang - 'en' or 'pl'
*/
func (c *NBPCurrency) GetCSVOutput(lang string) string {
func (c *NBPCurrency) CreateCSVOutput(lang string) string {
var output string = ""

// output language
Expand All @@ -433,8 +455,8 @@ func (c *NBPCurrency) GetCSVOutput(lang string) string {
return output
}

// GetRawOutput - function print just result of request (json or xml)
func (c *NBPCurrency) GetRawOutput() string {
// CreateRawOutput - function print just result of request (json or xml)
func (c *NBPCurrency) CreateRawOutput() string {
return string(c.result)
}

Expand Down
4 changes: 2 additions & 2 deletions currency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func TestCurrencyCSVOutput(t *testing.T) {
if err != nil {
t.Error(err)
}
output := client.GetCSVOutput("en")
output := client.CreateCSVOutput("en")
if output[:24] != want {
t.Errorf("invalid csv output, expected header: %s, got: %s", want, output[:29])
}
Expand All @@ -494,7 +494,7 @@ func TestCurrencyPrettyOutput(t *testing.T) {
if err != nil {
t.Error(err)
}
output := client.GetPrettyOutput("en")
output := client.CreatePrettyOutput("en")

if len(output) == 0 {
t.Errorf("incorrect (empty) pretty output")
Expand Down
5 changes: 4 additions & 1 deletion example/chf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ func main() {
fmt.Println(err)
} else {
// print polish version
fmt.Println(client.GetPrettyOutput("pl"))
fmt.Println(client.CreatePrettyOutput("pl"))
fmt.Println()
// print english version
fmt.Println(client.CreatePrettyOutput("en"))
fmt.Println()
}
fmt.Println()
Expand Down
4 changes: 2 additions & 2 deletions example/csv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
}
defer f.Close()

_, err = f.WriteString(nbpMid.GetCSVOutput("en"))
_, err = f.WriteString(nbpMid.CreateCSVOutput("en"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -41,6 +41,6 @@ func main() {
fmt.Println("Print last 5 (CHF) currency rates...")

// print as CSV, polish version
fmt.Println(nbpMid.GetCSVOutput("pl"))
fmt.Println(nbpMid.CreateCSVOutput("pl"))
fmt.Println()
}
13 changes: 12 additions & 1 deletion example/gold/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
if err != nil {
fmt.Println(err)
} else {
fmt.Println(gold.GetPrettyOutput("en"))
fmt.Println(gold.CreatePrettyOutput("en"))
}
fmt.Println()

Expand All @@ -53,4 +53,15 @@ func main() {
fmt.Println(rate.Data, "\t", rate.Price)
}
}

/*
function GoldLast return slice of GoldPrice struct
*/
err = gold.GoldLast(10)
if err != nil {
fmt.Println(err)
}

fmt.Println(gold.CreatePrettyOutput("en"))
fmt.Println(gold.CreatePrettyOutput("pl"))
}
2 changes: 1 addition & 1 deletion example/pretty/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func main() {
if err != nil {
fmt.Println(err)
} else {
fmt.Println(nbpTable.GetPrettyOutput("en"))
fmt.Println(nbpTable.CreatePrettyOutput("en"))
}
}
Loading

0 comments on commit 1bc4ea1

Please sign in to comment.