Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "reflect"
  10. )
  11.  
  12. // Apps struct
  13. type Apps struct {
  14. Apps []App `json:"apps"`
  15. }
  16.  
  17. // App struct
  18. type App struct {
  19. Name string `json:"name"`
  20. Price float32 `json:"price"`
  21. Downloads int `json:"downloads"`
  22. SalePrice float32
  23. }
  24.  
  25. const ARRAYSIZE = 7
  26. const threadCount = 4
  27. const downloadsMin = 1
  28.  
  29. var elementcounter = 0
  30. var globalcounterremoved = 0
  31. var globalcounteradd = 0
  32. var globalcounterresadd = 0
  33.  
  34. func main() {
  35. fileName := "IFF714_JanonisA_L2_dat_1.json"
  36. //fileName := "IFF714_JanonisA_L2_dat_2.json"
  37. //fileName := "IFF714_JanonisA_L2_dat_3.json"
  38. rezFile := "IFF714_JanonisA_L2_rez"
  39.  
  40. var apps Apps
  41. var err error
  42. apps, err = readData(fileName)
  43. if err != nil {
  44. fmt.Println(err)
  45. return
  46. }
  47. //fmt.Println(apps)
  48.  
  49. // pasitikrinimui, ar veikia nuskaitymas
  50. for i := 0; i < len(apps.Apps)-1; i++ {
  51. elementcounter++
  52. // fmt.Println("App Name: " + apps.Apps[i].Name)
  53. // fmt.Println("App Price: ", apps.Apps[i].Price)
  54. // fmt.Println("App Downloads: " + strconv.Itoa(apps.Apps[i].Downloads))
  55. // fmt.Println("")
  56. }
  57.  
  58. elementcounter++
  59.  
  60. elAdd := make(chan int)
  61. elTake := make(chan int)
  62.  
  63. dataAdd := make(chan App)
  64. dataTake := make(chan App)
  65.  
  66. resultAdd := make(chan App)
  67. resultTake := make(chan []App)
  68.  
  69. for i := 0; i < threadCount; i++ {
  70. go ThreadWork(elTake, dataTake, resultAdd)
  71. }
  72. go DataManager(dataAdd, dataTake, elAdd, elTake)
  73. go MonitorResults(resultAdd, resultTake)
  74.  
  75. for i := 0; i < len(apps.Apps); i++ {
  76. // ateityje visa tai atlikti su vienu kanalu
  77. elAdd <- 0
  78. dataAdd <- apps.Apps[i]
  79. }
  80. // perduodamas fiktyvus objektas, nurodantis, jog nauju elementu nebebus
  81. var results []App
  82. results = <-resultTake
  83.  
  84. resultFile, err := os.Create(rezFile)
  85. defer resultFile.Close()
  86.  
  87. writer := bufio.NewWriter(resultFile)
  88. fmt.Println(" %-20s %-20s %-20s %-20s \n", "Name", "Price", "Downloads", "Sale Price\n")
  89. _, err = fmt.Fprintf(writer, "%-20s %-20s %-20s %-20s \n", "Name", "Price", "Downloads", "Sale Price\n")
  90. line := "---------------------------------------------------------------------------------------------\n"
  91. _, err = fmt.Fprintf(writer, "%s", line)
  92.  
  93. for i := 0; i < len(results); i++ {
  94. fmt.Println(results[i].Name, results[i].Price, results[i].Downloads, results[i].SalePrice)
  95. _, err = fmt.Fprintf(writer, "%-20s %-20f %-20d %-20f \n", results[i].Name, results[i].Price, results[i].Downloads, results[i].SalePrice)
  96. }
  97.  
  98. _, err = fmt.Fprintf(writer, "%s", line)
  99. writer.Flush()
  100. fmt.Println("End of work")
  101. }
  102.  
  103. //json failo nuskaitymas
  104. func readData(fileName string) (Apps, error) {
  105. var apps Apps
  106. jsonFile, err := os.Open(fileName)
  107. if err != nil {
  108. return apps, err
  109. }
  110. byteValue, _ := ioutil.ReadAll(jsonFile)
  111. defer jsonFile.Close()
  112. json.Unmarshal(byteValue, &apps)
  113.  
  114. return apps, nil
  115. }
  116.  
  117. func DataManager(addData <-chan App, takeData chan<- App, askAdd <-chan int, askTake <-chan int) {
  118.  
  119. var data [ARRAYSIZE]App
  120. count := 0
  121. maxValue := len(data)
  122. status := true
  123.  
  124. for status == true { //kol nepasiektas paskutinis elementas
  125. var activeChannels = []<-chan int{nil}
  126. if count > 0 {
  127. activeChannels = append(activeChannels, askTake)
  128. } else {
  129. activeChannels = append(activeChannels, nil)
  130. }
  131. if count < maxValue && status == true {
  132. activeChannels = append(activeChannels, askAdd)
  133. } else {
  134. activeChannels = append(activeChannels, nil)
  135. }
  136. var cases []reflect.SelectCase
  137. for _, c := range activeChannels {
  138. cases = append(cases, reflect.SelectCase{
  139. Dir: reflect.SelectRecv,
  140. Chan: reflect.ValueOf(c),
  141. })
  142. }
  143. chooserIndex, _, _ := reflect.Select(cases)
  144. switch chooserIndex {
  145.  
  146. case 1:
  147. if globalcounterremoved == elementcounter {
  148. status = false
  149. } else {
  150. user := data[count-1]
  151. takeData <- user
  152. count = count - 1
  153. globalcounterremoved++
  154. }
  155.  
  156. case 2:
  157. var app App
  158. app = <-addData
  159. globalcounteradd++
  160. //fmt.Println("--->" + app.Name)
  161. if globalcounterremoved == elementcounter {
  162. data[count] = app
  163. count = count + 1
  164. status = false
  165. } else {
  166. data[count] = app
  167. count = count + 1
  168. }
  169. }
  170. }
  171. }
  172.  
  173. func MonitorResults(resultGet <-chan App, resultsAdd chan<- []App) {
  174. var results []App
  175. continueWork := true
  176. counter := 0
  177. for continueWork /*|| counter < elementCount*/ {
  178. var app = <-resultGet //is threadwork
  179. globalcounterresadd++
  180.  
  181. counter++
  182.  
  183. if app.Downloads > downloadsMin {
  184. index := 0
  185. for i := 0; i < len(results); i++ {
  186. if app.Downloads > results[i].Downloads {
  187. index = index + 1
  188. }
  189. }
  190. if index >= len(results) { //jeigu paskutinis
  191. results = append(results, app)
  192. } else { //jeigu reikia iterpt i viduri
  193. results = append(results, app)
  194. copy(results[index+1:], results[index:])
  195. results[index] = app
  196. }
  197. }
  198. if globalcounterresadd >= elementcounter { //jeigu i rezultatus grazinta tiek elementu kiek yra duomenu, darbas baigiamas
  199. continueWork = false
  200. }
  201.  
  202. // }
  203. }
  204. resultsAdd <- results
  205. }
  206.  
  207. func ThreadWork(askGet chan<- int, dataGet <-chan App, resultAdd chan<- App) {
  208. // parodo ar gija dar turi testi darba
  209. continueWork := true
  210. counter := 0
  211. for continueWork == true { //kol ne paskutinis elementas
  212. askGet <- 1
  213. var app = <-dataGet
  214. if globalcounterremoved >= elementcounter { //jeigu elementu paimta tiek kiek yra duomenu, idedame paskutini elementa ir darba baigiame
  215. app.SalePrice = incPrice(app.Price, false)
  216. fmt.Println(globalcounterremoved)
  217. resultAdd <- app
  218. counter++
  219. continueWork = false
  220. } else {
  221. app.SalePrice = incPrice(app.Price, false)
  222. resultAdd <- app
  223. counter++
  224. }
  225. }
  226. }
  227.  
  228. func incPrice(price float32, prefix bool) float32 {
  229. var temp float32
  230. temp = price - (price * 0.2)
  231. return temp
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement