Created
May 23, 2022 19:48
-
-
Save vasmarfas/f9aee87e671b01dad0b95bff06924a6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlinx.coroutines.* | |
| import kotlinx.coroutines.channels.Channel | |
| import java.util.Objects | |
| class SortingCentre( | |
| val loadingPortsAmount: Int = 1, | |
| private val unloadingPortsAmount: Int = 3 | |
| ) { | |
| val trucksForUnload = mutableListOf<Truck>() | |
| private val truckGenerator = TruckGenerator | |
| private val channelTruckQuery = Channel<Truck>() | |
| class FirstUnloadingPort: UnloadingPort(numOfPort = 1) {} | |
| class SecondUnloadingPort: UnloadingPort(numOfPort = 2) {} | |
| class ThirdUnloadingPort: UnloadingPort(numOfPort = 3) {} | |
| suspend fun startWork() = runBlocking { | |
| launch { | |
| while (true) channelTruckQuery.send(truckGenerator.randomTruck()) | |
| } | |
| // var currentQueryAmount: Int = 0 | |
| launch { | |
| while (true) { | |
| if (trucksForUnload.lastIndex < 3) { | |
| for (i in 0 until unloadingPortsAmount) { | |
| val receivedTruck = channelTruckQuery.receive() | |
| trucksForUnload.add(receivedTruck) | |
| println("Truck $receivedTruck added to list") | |
| println(trucksForUnload) | |
| // currentQueryAmount++ | |
| // println(currentQueryAmount) | |
| } | |
| } | |
| } | |
| } | |
| launch { | |
| delay(100) | |
| var removedTruck = trucksForUnload[1] | |
| trucksForUnload.remove(removedTruck) | |
| // currentQueryAmount -= 1 | |
| println("Truck $removedTruck was removed") | |
| // println(currentQueryAmount) | |
| trucksForUnload.forEachIndexed{index, truck -> println("index $index truck $truck") } | |
| } | |
| } | |
| } | |
| open class LoadingPort(var isEmpty: Boolean = true) { | |
| open fun loading(loadingProduct: Objects) = runBlocking { | |
| } | |
| } | |
| open class UnloadingPort(var isEmpty: Boolean = true, val numOfPort: Int) { | |
| open fun unloading() = runBlocking { | |
| launch { | |
| while (true) { | |
| if (SortingCentre().trucksForUnload[numOfPort-1].loadCapacity > 0) { | |
| val unloadingTruck = SortingCentre().trucksForUnload[numOfPort-1] | |
| var i = 0 | |
| while (i < unloadingTruck.amountOfLoadedProduct) { | |
| delay(unloadingTruck.loadedProduct.timeForLoading.toLong()) | |
| unloadingTruck.amountOfLoadedProduct = unloadingTruck.amountOfLoadedProduct - 1 | |
| Storage().storageList.add(unloadingTruck.loadedProduct) | |
| i++ | |
| } | |
| SortingCentre().trucksForUnload.remove(unloadingTruck) | |
| } else { | |
| println("Query is empty") | |
| delay(200) | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment