From 7b6a007e3c027d73d5561214e675b73c1f4cf58a Mon Sep 17 00:00:00 2001 From: JiveOff Date: Fri, 19 Jan 2024 21:00:33 +0100 Subject: [PATCH] feat: UI tweaks --- src/main/scala/UI.scala | 44 ++++++++++++++----- .../analysis/PowerTemperatureAnalysis.scala | 4 +- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/main/scala/UI.scala b/src/main/scala/UI.scala index 9341e03..bf9ed7a 100644 --- a/src/main/scala/UI.scala +++ b/src/main/scala/UI.scala @@ -15,13 +15,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 () } @@ -33,7 +36,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 @@ -47,7 +51,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 @@ -58,10 +62,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): ") @@ -72,9 +90,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" + GlobalStatisticsAnalysis.carbonIntensityTab(data, startDate, endDate)) @@ -91,6 +109,8 @@ object UI { temperatureAndPowerPeakPearsonCorrelation(data) ) ) + _ <- printLine("\nPress enter to go back to the menu...") + _ <- readLine } yield () } } diff --git a/src/main/scala/analysis/PowerTemperatureAnalysis.scala b/src/main/scala/analysis/PowerTemperatureAnalysis.scala index 0cd0b42..be6b553 100644 --- a/src/main/scala/analysis/PowerTemperatureAnalysis.scala +++ b/src/main/scala/analysis/PowerTemperatureAnalysis.scala @@ -64,7 +64,7 @@ object PowerTemperatureAnalysis { * A formatted string containing information about maximum power peak and minimum temperature entries for each year, with each entry on a new line. */ def formatPowerPeakAndTemperature(yearsData: Chunk[(Int, DailyPowerPeakWithTemperature, DailyPowerPeakWithTemperature)], correlationCoef: Double): String = { - val title = "+-------------------------------------------------------------------------+\n" + + val title = "\n+-------------------------------------------------------------------------+\n" + "| Case Study : Power and Temperature Summary |\n" + "+-------------------------------------------------------------------------+\n" + "| We want to know the maximum power peak and the minimum temperature for |\n" + @@ -92,7 +92,7 @@ object PowerTemperatureAnalysis { "| - As daily temperatures decrease, daily power peak tends to increase. |\n" + "| - People tend to use more electricity for heating purposes when |\n" + "| temperatures fall |\n" + - "+-------------------------------------------------------------------------+\n" + "+-------------------------------------------------------------------------+" title + yearsSummaries + pearsonCoefFormatted + conclusion }