Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework_7 - Васильева Анна #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions homeworks/homework_7/src/main/scala/Breakfast.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package ru.dru

import zio.CanFail.canFailAmbiguous1
import zio.{Duration, Exit, Fiber, Scope, ZIO, ZIOApp, ZIOAppArgs, ZIOAppDefault, durationInt}
import zio.Clock.{currentDateTime, sleep}
import zio.{Duration, Exit, Fiber, Scope, ZIO, ZIOApp, ZIOAppArgs, ZIOAppDefault, durationLong}

import java.time.LocalDateTime
import scala.concurrent.TimeoutException
Expand Down Expand Up @@ -32,9 +33,25 @@ object Breakfast extends ZIOAppDefault {
def makeBreakfast(eggsFiringTime: Duration,
waterBoilingTime: Duration,
saladInfoTime: SaladInfoTime,
teaBrewingTime: Duration): ZIO[Any, Throwable, Map[String, LocalDateTime]] = ???


teaBrewingTime: Duration): ZIO[Any, Throwable, Map[String, LocalDateTime]] = {
val totalTimeSalad = durationLong(saladInfoTime.cucumberTime.toSeconds + saladInfoTime.tomatoTime.toSeconds)

for {
startTime <- ZIO.succeed(LocalDateTime.now())
eggsFryFiber <- ZIO.sleep(eggsFiringTime).as(startTime.plusSeconds(eggsFiringTime.toSeconds)).fork
waterBoilFiber <- ZIO.sleep(waterBoilingTime).as(startTime.plusSeconds(waterBoilingTime.toSeconds)).fork
saladWithSourCreamTime <- ZIO.sleep(totalTimeSalad.seconds)
.as(startTime.plusSeconds(saladInfoTime.cucumberTime.toSeconds).plusSeconds(saladInfoTime.tomatoTime.toSeconds))
waterBoiledTime <- waterBoilFiber.join
teaTime <- ZIO.sleep(teaBrewingTime).as(waterBoiledTime.plusSeconds(teaBrewingTime.toSeconds))
eggsFriedTime <- eggsFryFiber.join
} yield Map(
"eggs" -> eggsFriedTime,
"water" -> waterBoiledTime,
"saladWithSourCream" -> saladWithSourCreamTime,
"tea" -> teaTime
)
}

override def run: ZIO[Any with ZIOAppArgs with Scope, Any, Any] = ZIO.succeed(println("Done"))

Expand Down
15 changes: 12 additions & 3 deletions homeworks/homework_7/src/main/scala/ResuourceTraining.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ import java.io.{BufferedReader, BufferedWriter, FileReader, FileWriter}

object ResuourceTraining extends ZIOAppDefault {

def readData(filePath: String): IO[Throwable, String] = ???

def writeData(filePath: String, data: String): ZIO[Any, Nothing, Unit] = ???
def readData(filePath: String): IO[Throwable, String] =
ZIO.acquireReleaseWith(ZIO.attempt(new BufferedReader(new FileReader(filePath)))
)(reader => ZIO.attempt(reader.close()).orDie)(reader => ZIO.attempt(reader.readLine()))

def writeData(filePath: String, data: String): ZIO[Any, Nothing, Unit] =
ZIO.acquireReleaseWith(ZIO.attempt(new BufferedWriter(new FileWriter(filePath)))
)(writer => ZIO.attempt(writer.close()).orDie)(
writer => ZIO.attempt {
writer.write(data)
writer.flush()
}
).orDie

override def run: ZIO[Any with ZIOAppArgs with Scope, Any, Any] = ZIO.succeed("Done")
}