Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "os"
- "reflect"
- )
- // Apps struct
- type Apps struct {
- Apps []App `json:"apps"`
- }
- // App struct
- type App struct {
- Name string `json:"name"`
- Price float32 `json:"price"`
- Downloads int `json:"downloads"`
- SalePrice float32
- }
- const ARRAYSIZE = 7
- const threadCount = 4
- const downloadsMin = 1
- var elementcounter = 0
- var globalcounterremoved = 0
- var globalcounteradd = 0
- var globalcounterresadd = 0
- func main() {
- fileName := "IFF714_JanonisA_L2_dat_1.json"
- //fileName := "IFF714_JanonisA_L2_dat_2.json"
- //fileName := "IFF714_JanonisA_L2_dat_3.json"
- rezFile := "IFF714_JanonisA_L2_rez"
- var apps Apps
- var err error
- apps, err = readData(fileName)
- if err != nil {
- fmt.Println(err)
- return
- }
- //fmt.Println(apps)
- // pasitikrinimui, ar veikia nuskaitymas
- for i := 0; i < len(apps.Apps)-1; i++ {
- elementcounter++
- // fmt.Println("App Name: " + apps.Apps[i].Name)
- // fmt.Println("App Price: ", apps.Apps[i].Price)
- // fmt.Println("App Downloads: " + strconv.Itoa(apps.Apps[i].Downloads))
- // fmt.Println("")
- }
- elementcounter++
- elAdd := make(chan int)
- elTake := make(chan int)
- dataAdd := make(chan App)
- dataTake := make(chan App)
- resultAdd := make(chan App)
- resultTake := make(chan []App)
- for i := 0; i < threadCount; i++ {
- go ThreadWork(elTake, dataTake, resultAdd)
- }
- go DataManager(dataAdd, dataTake, elAdd, elTake)
- go MonitorResults(resultAdd, resultTake)
- for i := 0; i < len(apps.Apps); i++ {
- // ateityje visa tai atlikti su vienu kanalu
- elAdd <- 0
- dataAdd <- apps.Apps[i]
- }
- // perduodamas fiktyvus objektas, nurodantis, jog nauju elementu nebebus
- var results []App
- results = <-resultTake
- resultFile, err := os.Create(rezFile)
- defer resultFile.Close()
- writer := bufio.NewWriter(resultFile)
- fmt.Println(" %-20s %-20s %-20s %-20s \n", "Name", "Price", "Downloads", "Sale Price\n")
- _, err = fmt.Fprintf(writer, "%-20s %-20s %-20s %-20s \n", "Name", "Price", "Downloads", "Sale Price\n")
- line := "---------------------------------------------------------------------------------------------\n"
- _, err = fmt.Fprintf(writer, "%s", line)
- for i := 0; i < len(results); i++ {
- fmt.Println(results[i].Name, results[i].Price, results[i].Downloads, results[i].SalePrice)
- _, err = fmt.Fprintf(writer, "%-20s %-20f %-20d %-20f \n", results[i].Name, results[i].Price, results[i].Downloads, results[i].SalePrice)
- }
- _, err = fmt.Fprintf(writer, "%s", line)
- writer.Flush()
- fmt.Println("End of work")
- }
- //json failo nuskaitymas
- func readData(fileName string) (Apps, error) {
- var apps Apps
- jsonFile, err := os.Open(fileName)
- if err != nil {
- return apps, err
- }
- byteValue, _ := ioutil.ReadAll(jsonFile)
- defer jsonFile.Close()
- json.Unmarshal(byteValue, &apps)
- return apps, nil
- }
- func DataManager(addData <-chan App, takeData chan<- App, askAdd <-chan int, askTake <-chan int) {
- var data [ARRAYSIZE]App
- count := 0
- maxValue := len(data)
- status := true
- for status == true { //kol nepasiektas paskutinis elementas
- var activeChannels = []<-chan int{nil}
- if count > 0 {
- activeChannels = append(activeChannels, askTake)
- } else {
- activeChannels = append(activeChannels, nil)
- }
- if count < maxValue && status == true {
- activeChannels = append(activeChannels, askAdd)
- } else {
- activeChannels = append(activeChannels, nil)
- }
- var cases []reflect.SelectCase
- for _, c := range activeChannels {
- cases = append(cases, reflect.SelectCase{
- Dir: reflect.SelectRecv,
- Chan: reflect.ValueOf(c),
- })
- }
- chooserIndex, _, _ := reflect.Select(cases)
- switch chooserIndex {
- case 1:
- if globalcounterremoved == elementcounter {
- status = false
- } else {
- user := data[count-1]
- takeData <- user
- count = count - 1
- globalcounterremoved++
- }
- case 2:
- var app App
- app = <-addData
- globalcounteradd++
- //fmt.Println("--->" + app.Name)
- if globalcounterremoved == elementcounter {
- data[count] = app
- count = count + 1
- status = false
- } else {
- data[count] = app
- count = count + 1
- }
- }
- }
- }
- func MonitorResults(resultGet <-chan App, resultsAdd chan<- []App) {
- var results []App
- continueWork := true
- counter := 0
- for continueWork /*|| counter < elementCount*/ {
- var app = <-resultGet //is threadwork
- globalcounterresadd++
- counter++
- if app.Downloads > downloadsMin {
- index := 0
- for i := 0; i < len(results); i++ {
- if app.Downloads > results[i].Downloads {
- index = index + 1
- }
- }
- if index >= len(results) { //jeigu paskutinis
- results = append(results, app)
- } else { //jeigu reikia iterpt i viduri
- results = append(results, app)
- copy(results[index+1:], results[index:])
- results[index] = app
- }
- }
- if globalcounterresadd >= elementcounter { //jeigu i rezultatus grazinta tiek elementu kiek yra duomenu, darbas baigiamas
- continueWork = false
- }
- // }
- }
- resultsAdd <- results
- }
- func ThreadWork(askGet chan<- int, dataGet <-chan App, resultAdd chan<- App) {
- // parodo ar gija dar turi testi darba
- continueWork := true
- counter := 0
- for continueWork == true { //kol ne paskutinis elementas
- askGet <- 1
- var app = <-dataGet
- if globalcounterremoved >= elementcounter { //jeigu elementu paimta tiek kiek yra duomenu, idedame paskutini elementa ir darba baigiame
- app.SalePrice = incPrice(app.Price, false)
- fmt.Println(globalcounterremoved)
- resultAdd <- app
- counter++
- continueWork = false
- } else {
- app.SalePrice = incPrice(app.Price, false)
- resultAdd <- app
- counter++
- }
- }
- }
- func incPrice(price float32, prefix bool) float32 {
- var temp float32
- temp = price - (price * 0.2)
- return temp
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement