SHARE
TWEET

Untitled

a guest Jan 14th, 2020 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "fmt"
  6.     "io/ioutil"
  7.     "os"
  8.     "sync"
  9. )
  10.  
  11. type Items struct {
  12.     Items []Item `json:"cars"`
  13. }
  14.  
  15. type Item struct {
  16.     Name string `json:"name"`
  17.     Year float64 `json:"year"`
  18.     Mileage float64 `json:"mileage"`
  19. }
  20.  
  21. type ItemsResult struct {
  22.     ItemsResult [20]ItemResult `json:"items"`
  23.     Count int
  24. }
  25.  
  26. type ItemResult struct {
  27.     Item Item
  28.     AnnualMileage float64
  29. }
  30.  
  31. type ItemsMonitor struct {
  32.     Items [10]Item
  33.     Count int
  34.     Mutex sync.Mutex
  35.     MutexForCount sync.Mutex
  36. }
  37.  
  38. func (monitor *ItemsMonitor) addItem(item Item){
  39.     if monitor.Count == 0 {
  40.         monitor.MutexForCount.Unlock()
  41.     }
  42.     monitor.MutexForCount.Lock()
  43.     monitor.Items[monitor.Count] = item
  44.     monitor.Count++
  45.     if monitor.Count < 10 {
  46.         monitor.MutexForCount.Unlock()
  47.     }
  48. }
  49.  
  50. func (monitor *ItemsMonitor) removeItem(itemsResult *ItemsResult, i int){
  51.     if monitor.Count < 10 {
  52.         monitor.MutexForCount.Lock()
  53.     }
  54.     var item Item
  55.     if monitor.Count != 0 {
  56.         monitor.Items[monitor.Count - 1] = item
  57.         monitor.Count--
  58.     }
  59.     if monitor.Count < 10 {
  60.         monitor.MutexForCount.Unlock()
  61.     }
  62.     if monitor.Count == 0 {
  63.         monitor.MutexForCount.Lock()
  64.     }
  65. }
  66.  
  67. func (monitor *ItemsMonitor) calculateAndSort(value bool, i int, itemsMonitor *ItemsMonitor, items *Items, itemsResult *ItemsResult) {
  68.     var index int
  69.     for value == false {
  70.         if(itemsResult.ItemsResult[index].Item.Year == 0){
  71.             i = index
  72.             value = true
  73.         }
  74.         index++
  75.     }
  76.     monitor.Mutex.Lock()
  77.     defer monitor.Mutex.Unlock()
  78.     if itemsMonitor.Items[itemsMonitor.Count - 1].Year >= 15 {
  79.         var itemResult ItemResult
  80.         itemResult.Item = itemsMonitor.Items[itemsMonitor.Count - 1]
  81.         itemResult.AnnualMileage = itemsMonitor.Items[itemsMonitor.Count - 1].Mileage  / itemsMonitor.Items[itemsMonitor.Count - 1].Year
  82.  
  83.         less := false
  84.         if i == 0 {
  85.             itemsResult.ItemsResult[i] = itemResult
  86.         }
  87.         var itemToMove ItemResult
  88.         var copyOfItem ItemResult
  89.         for j := 1; j <= i ; j++  {
  90.             if less == true  {
  91.                 copyOfItem = itemToMove
  92.                 itemToMove = itemsResult.ItemsResult[j]
  93.                 itemsResult.ItemsResult[j] = copyOfItem
  94.             } else {
  95.                 if j - 1 >= 0 && j <= len(items.Items) && itemsResult.ItemsResult[j - 1].AnnualMileage > itemResult.AnnualMileage && itemResult.AnnualMileage < itemsResult.ItemsResult[j].AnnualMileage {
  96.                     itemToMove = itemsResult.ItemsResult[j]
  97.                     itemsResult.ItemsResult[j] = itemsResult.ItemsResult[j - 1]
  98.                     itemsResult.ItemsResult[j - 1] = itemResult
  99.  
  100.                     less = true
  101.                 }
  102.                 if j - 1 >= 0 && j + 1 <= 19 && j <= len(items.Items) && itemsResult.ItemsResult[j - 1].AnnualMileage > itemResult.AnnualMileage && 0 == itemsResult.ItemsResult[j].AnnualMileage {
  103.                     itemsResult.ItemsResult[j] = itemsResult.ItemsResult[j - 1]
  104.                     itemsResult.ItemsResult[j - 1] = itemResult
  105.                     less = true
  106.                 } else if j == i {
  107.                     itemsResult.ItemsResult[j] = itemResult
  108.                 }
  109.             }
  110.         }
  111.         itemsResult.Count++
  112.     }
  113. }
  114.  
  115. func check(e error) {
  116.     if e != nil {
  117.         panic(e)
  118.     }
  119. }
  120.  
  121. func writeToResultFile(itemsResult ItemsResult)  {
  122.     f, err := os.Create("result.txt")
  123.     check(err)
  124.     defer f.Close()
  125.     f.WriteString(fmt.Sprintf("%20s%20s%20s%30s \n", "Pavadinimas", "Metai", "Rida", "Vid. Km per metus"))
  126.     f.WriteString("------------------------------------------------------------------------------------------- \n")
  127.     for i := 0; i < itemsResult.Count; i++ {
  128.         test := fmt.Sprintf("%20s%20f%20f%30f \n", itemsResult.ItemsResult[i].Item.Name, itemsResult.ItemsResult[i].Item.Year, itemsResult.ItemsResult[i].Item.Mileage, itemsResult.ItemsResult[i].AnnualMileage)
  129.         f.WriteString(test)
  130.     }
  131. }
  132.  
  133. func main() {
  134.     jsonFile1, err := os.Open("IFF-74_TomkeviciusZ_L1_dat_2.json")
  135.     if err != nil {
  136.         fmt.Println(err)
  137.     }
  138.     defer jsonFile1.Close()
  139.     byteValue, _ := ioutil.ReadAll(jsonFile1)
  140.     var items Items
  141.     _ = json.Unmarshal(byteValue, &items)
  142.  
  143.     var itemsMonitor ItemsMonitor
  144.     var itemsResult ItemsResult
  145.     var wg sync.WaitGroup
  146.     wg.Add(4)
  147.     itemsMonitor.MutexForCount.Lock()
  148.     go func() {
  149.         for i := 0; i < 5; i++ {
  150.             itemsMonitor.addItem(items.Items[i])
  151.             itemsMonitor.calculateAndSort(false, 1, &itemsMonitor, &items, &itemsResult)
  152.             itemsMonitor.removeItem(&itemsResult, i)
  153.         }
  154.         wg.Done()
  155.     }()
  156.     go func() {
  157.         for i := 5; i < 10; i++ {
  158.             itemsMonitor.addItem(items.Items[i])
  159.             itemsMonitor.calculateAndSort(false, 1, &itemsMonitor, &items, &itemsResult)
  160.             itemsMonitor.removeItem(&itemsResult, i)
  161.         }
  162.         wg.Done()
  163.     }()
  164.     go func() {
  165.         for i := 10; i < 15; i++ {
  166.             itemsMonitor.addItem(items.Items[i])
  167.             itemsMonitor.calculateAndSort(false, 1, &itemsMonitor, &items, &itemsResult)
  168.             itemsMonitor.removeItem(&itemsResult, i)
  169.         }
  170.         wg.Done()
  171.  
  172.     }()
  173.     go func() {
  174.         for i := 15; i < 20; i++ {
  175.             itemsMonitor.addItem(items.Items[i])
  176.             itemsMonitor.calculateAndSort(false, 1, &itemsMonitor, &items, &itemsResult)
  177.             itemsMonitor.removeItem(&itemsResult, i)
  178.         }
  179.         wg.Done()
  180.     }()
  181.     wg.Wait()
  182.  
  183.     writeToResultFile(itemsResult)
  184. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top