|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + filename = "datajson.json" |
| 12 | +) |
| 13 | + |
| 14 | +func main() { |
| 15 | + |
| 16 | + plan, _ := ioutil.ReadFile(filename) |
| 17 | + var data interface{} |
| 18 | + err := json.Unmarshal(plan, &data) |
| 19 | + if err != nil { |
| 20 | + fmt.Println(err.Error()) |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + dataConvString := string(plan) |
| 25 | + |
| 26 | + hargaItem := GetHargaItem(dataConvString, "voucher10", "voucher", "Contoh Voucher") |
| 27 | + fmt.Println("Harga perItem: ", hargaItem) |
| 28 | +} |
| 29 | + |
| 30 | +func GetHargaItem(dataArray, productCode, productType, productDesc string) float64 { |
| 31 | + |
| 32 | + productDescReplace := strings.ReplaceAll(productDesc, " ", "_") |
| 33 | + |
| 34 | + fmt.Println("productType: ", productType) |
| 35 | + fmt.Println("productDescReplace: ", productDescReplace) |
| 36 | + |
| 37 | + var dataMap map[string]interface{} |
| 38 | + json.Unmarshal([]byte(dataArray), &dataMap) |
| 39 | + |
| 40 | + dataMap2 := dataMap["data"].(map[string]interface{}) |
| 41 | + |
| 42 | + var totalHarga float64 |
| 43 | + feeTransaction := 500.0 |
| 44 | + for _, item := range dataMap2["pricelist"].([]interface{}) { |
| 45 | + itemMap := item.(map[string]interface{}) |
| 46 | + if itemMap["product_code"] != nil && itemMap["product_code"].(string) != "" { |
| 47 | + if strings.ToLower(itemMap["product_code"].(string)) == strings.ToLower(productCode) { |
| 48 | + totalHarga += itemMap["product_price"].(float64) + feeTransaction |
| 49 | + } else { |
| 50 | + continue |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return totalHarga |
| 56 | +} |
0 commit comments