Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tychy committed Sep 21, 2024
1 parent b02ea69 commit 0159fb2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
33 changes: 33 additions & 0 deletions toukibo/houjin_stock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package toukibo

import (
"strconv"
"strings"
)

type HoujinPreferredStock struct {
Type string `yaml:"Type"`
Amount int `yaml:"Amount"`
}

type HoujinStock struct {
Total int
Preferred []HoujinPreferredStock
}

func (s HoujinStock) Sum() int {
sum := 0
for _, p := range s.Preferred {
sum += p.Amount
}
return sum
}

func (s HoujinStock) String() string {
var b strings.Builder
for _, p := range s.Preferred {
b.WriteString(" - Type: " + p.Type + "\n")
b.WriteString(" Amount: " + strconv.Itoa(p.Amount) + "\n")
}
return b.String()
}
31 changes: 9 additions & 22 deletions toukibo/stock_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,10 @@ func GetStockNumber(s string) (int, string) {
return sums, ""
}

type PreferredStock struct {
Name string
Num int
}

type Stock struct {
Total int
Preferred []PreferredStock
}

func (s Stock) Sum() int {
sum := 0
for _, p := range s.Preferred {
sum += p.Num
}
return sum
}

func StockToNumber(stock string) int {
func GetHoujinStock(stock string) HoujinStock {
stock = ZenkakuToHankaku(stock)
stock = trimAllSpace(stock)
res := Stock{}
res := HoujinStock{}

for {
if stock == "" {
Expand Down Expand Up @@ -90,7 +72,7 @@ func StockToNumber(stock string) int {
stock = strings.Replace(stock, "普通株式", "", -1)
normal, s := GetStockNumber(stock)
stock = s
res.Preferred = append(res.Preferred, PreferredStock{Name: "普通株式", Num: normal})
res.Preferred = append(res.Preferred, HoujinPreferredStock{Type: "普通株式", Amount: normal})
continue
}

Expand All @@ -102,7 +84,7 @@ func StockToNumber(stock string) int {
stock = strings.Replace(stock, matches[1], "", -1)
num, s := GetStockNumber(stock)
stock = s
res.Preferred = append(res.Preferred, PreferredStock{Name: matches[1], Num: num})
res.Preferred = append(res.Preferred, HoujinPreferredStock{Type: matches[1], Amount: num})
continue
}

Expand All @@ -112,5 +94,10 @@ func StockToNumber(stock string) int {
res.Total = res.Sum()
}

return res
}

func StockToNumber(stock string) int {
res := GetHoujinStock(stock)
return res.Total
}

0 comments on commit 0159fb2

Please sign in to comment.