Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibClnt committed Jan 19, 2024
2 parents 791df7d + 7b6a007 commit 33cbcab
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/main/scala/UI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ object UI {
*/
def consoleLoop(data: LoadedData): ZIO[Any, Any, Unit] = {
for {
_ <- printLine(menuString)
input <- readLine
_ <- menuChoices.lift(input.toInt - 1) match {
case Some((_, action)) => action(data)
case None => printLineError("Invalid choice")
}
_ <- consoleLoop(data)
_ <- printLine(menuString)
number <- readNumber
_ <-
if (number == menuChoices.length) ZIO.unit
else
menuChoices.lift(number - 1) match {
case Some((_, action)) => action(data)
case None => printLineError("Invalid choice")
}
_ <- if (number == menuChoices.length) ZIO.unit else consoleLoop(data)
} yield ()
}

Expand All @@ -35,7 +38,8 @@ object UI {
Chunk(
("Get stats for a specific day", printDailyStats),
("Global statistics for a given period", printGlobalStats),
("Case study: Temperature vs power peak", printCaseStudy)
("Case study: Temperature vs power peak", printCaseStudy),
("Exit", (_) => ZIO.unit)
)

/** Menu string
Expand All @@ -49,7 +53,7 @@ object UI {
s"${index + 1}. $name"
}
.mkString("\n")
+ "\n\nPlease enter your choice: "
+ "\n\nPlease enter your choice:"

/** Reads a date from the console
* @return
Expand All @@ -60,10 +64,24 @@ object UI {
input <- readLine
date <- Try(LocalDate.parse(input, DateTimeFormatter.ofPattern("dd/MM/yyyy"))).toOption match {
case Some(date) => ZIO.succeed(date)
case None => printLineError("You entered an invalid date, retry.\n") *> readDate
case None => printLineError("You entered an invalid date, retry:") *> readDate
}
} yield date

/** Reads a number from the console
*
* @return
* a ZIO effect that will return an Int
*/
def readNumber: ZIO[Any, Any, Int] =
for {
input <- readLine
number <- Try(input.toInt).toOption match {
case Some(number) => ZIO.succeed(number)
case None => printLineError("You entered an invalid number, retry:") *> readNumber
}
} yield number

def printDailyStats(data: LoadedData): ZIO[Any, Any, Unit] = {
for {
_ <- printLine("Please enter a date (dd/MM/yyyy): ")
Expand All @@ -74,9 +92,9 @@ object UI {

def printGlobalStats(data: LoadedData): ZIO[Any, Any, Unit] = {
for {
_ <- printLine("Please enter a start date (dd/MM/yyyy): ")
_ <- printLine("Please enter a start date (dd/MM/yyyy):")
startDate <- readDate
_ <- printLine("Please enter an end date (dd/MM/yyyy): ")
_ <- printLine("Please enter an end date (dd/MM/yyyy):")
endDate <- readDate

_ <- printLine("\n\n" + GlobalStatisticsFormatter.carbonIntensityTab(data, startDate, endDate))
Expand All @@ -93,6 +111,8 @@ object UI {
temperatureAndPowerPeakPearsonCorrelation(data)
)
)
_ <- printLine("\nPress enter to go back to the menu...")
_ <- readLine
} yield ()
}
}

0 comments on commit 33cbcab

Please sign in to comment.