@@ -34,19 +34,28 @@ type Config struct {
3434
3535func main () {
3636
37- log .Printf ("Fetching today's prices...\n \n " )
37+ log .Printf ("Fetching today's prices..." )
3838
3939 json_file := "items.json"
4040 product_link_map := getProductList (json_file )
4141
4242 for name , link := range product_link_map {
4343 price := getPriceFromSite (name , link )
44+
45+ fmt .Println ("" )
4446 log .Printf (`Today's price for "%s" is %s` , name , price )
47+ fmt .Println ("" )
4548
4649 addPriceToProductList (name , price , json_file )
4750
4851 average_price , sample_size := getAveragePriceForItem (name , json_file )
49- log .Printf ("The Average price for this item was $%.2f over %d samples\n \n " , average_price , sample_size )
52+ log .Printf ("The Average price for this item was $%.2f over %d samples" , average_price , sample_size )
53+
54+ min_price , max_price , valid := getMinMaxPriceForItem (name , json_file )
55+ if valid {
56+ log .Printf ("The max price for this item was %s on %s" , max_price .Price , max_price .Date )
57+ log .Printf ("The min price for this item was %s on %s" , min_price .Price , min_price .Date )
58+ }
5059 }
5160}
5261
@@ -172,6 +181,48 @@ func getAveragePriceForItem(name string, filename string) (float32, int) {
172181}
173182
174183
184+ func getMinMaxPriceForItem (name string , filename string ) (Price , Price , bool ) {
185+
186+ raw , err := ioutil .ReadFile (filename )
187+
188+ if err != nil {
189+ panic ("Failed to read JSON file : " + filename + " => " + err .Error ())
190+ }
191+
192+ var conf Config
193+ json .Unmarshal (raw , & conf )
194+
195+ valid := false
196+ samples := 0
197+ var min_price Price
198+ var max_price Price
199+
200+ for item := range conf .Items {
201+ if conf .Items [item ].Name == name {
202+ for i := range conf .Items [item ].Prices {
203+
204+ current_price := conf .Items [item ].Prices [i ]
205+
206+ if current_price .Price > max_price .Price {
207+ max_price = current_price
208+ }
209+
210+ if current_price .Price < min_price .Price || i == 0 {
211+ min_price = current_price
212+ }
213+
214+ samples += 1
215+ }
216+ }
217+ }
218+
219+ if samples > 0 {
220+ valid = true
221+ }
222+
223+ return min_price , max_price , valid
224+ }
225+
175226
176227func toJson (j Config ) string {
177228 bytes , err := json .MarshalIndent (j , "" , "\t " )
0 commit comments