From f2c272fb77937eeae3242db0b4ffea935666977a Mon Sep 17 00:00:00 2001 From: AndrewTrieu Date: Wed, 3 May 2023 12:48:41 +0300 Subject: [PATCH] Update code --- README.md | 5 +- src/main/scala/Main.scala | 101 ++++++++++++++++++++++---------------- 2 files changed, 62 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index b9fb9cf..c05eafd 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,6 @@ sequenceDiagram System->>Fingrid API: Get data Fingrid API-->>System: Return data System->>Files: Store data in files - ``` - The system provides a view of the power plant's energy generation and storage capacity, allowing operators to adjust the power plant's operation as needed. This view shows the data stored in the file. @@ -119,5 +118,7 @@ classDiagram +alertUser() +scanSystem(data: List[String]) } +``` + +## Video -``` \ No newline at end of file diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala index b399e6c..a11286b 100644 --- a/src/main/scala/Main.scala +++ b/src/main/scala/Main.scala @@ -88,17 +88,23 @@ object Main { ): Unit = { // Read the energy sources println("Reading sources...") - val bufferedSource = Source.fromFile(energySources) - def readSourcesHelper(lines: Iterator[String]): Unit = { - if (lines.hasNext) { - val line = lines.next() - val cols = line.split(",").map(_.trim) - println(s"${cols(0)}\t${cols(1)}\t${cols(2)}") - readSourcesHelper(lines) + try { + val bufferedSource = Source.fromFile(energySources) + def readSourcesHelper(lines: Iterator[String]): Unit = { + if (lines.hasNext) { + val line = lines.next() + val cols = line.split(",").map(_.trim) + println(s"${cols(0)}\t${cols(1)}\t${cols(2)}") + readSourcesHelper(lines) + } } + readSourcesHelper(bufferedSource.getLines) + bufferedSource.close + } catch { + case e: Exception => + println("No sources found") + return } - readSourcesHelper(bufferedSource.getLines) - bufferedSource.close print("Enter your choice:\n1) Modify\n2) Exit\n") val choice = readLine() @@ -266,24 +272,30 @@ object Main { "Sources:\n1) Wind\n2) Hydro\n3) Nuclear\n4) All\n5) Exit\nEnter your choice: " ) val choice = readLine() - val bufferedSource = choice match { - case "1" => - Source.fromFile("wind.csv") - case "2" => - Source.fromFile("hydro.csv") - case "3" => - Source.fromFile("nuclear.csv") - case "4" => - Source.fromFile("data.csv") - case "5" => - return null - case _ => - println("Invalid choice") + try { + val bufferedSource = choice match { + case "1" => + Source.fromFile("wind.csv") + case "2" => + Source.fromFile("hydro.csv") + case "3" => + Source.fromFile("nuclear.csv") + case "4" => + Source.fromFile("data.csv") + case "5" => + return null + case _ => + println("Invalid choice") + null + } + val data = bufferedSource.getLines.toList + bufferedSource.close + data + } catch { + case e: Exception => + println("File not found") null } - val data = bufferedSource.getLines.toList - bufferedSource.close - data } // Sort the data by timestamp or value @@ -360,25 +372,30 @@ object Main { // Alert user if energy production is below the threshold def alertUser(): Unit = { - val sources = dataSources.values.toList.dropRight(1) - sources.map { source => - val bufferedSource = Source.fromFile(source) - val data = bufferedSource.getLines.toList - bufferedSource.close - val alert = data.drop(1).foldLeft(false) { (acc, line) => - val cols = line.split(",").map(_.trim) - val value = cols(2).toDouble - if (value < alertThreshold) { - true - } else { - acc + try { + val sources = dataSources.values.toList.dropRight(1) + sources.map { source => + val bufferedSource = Source.fromFile(source) + val data = bufferedSource.getLines.toList + bufferedSource.close + val alert = data.drop(1).foldLeft(false) { (acc, line) => + val cols = line.split(",").map(_.trim) + val value = cols(2).toDouble + if (value < alertThreshold) { + true + } else { + acc + } + } + if (alert) { + println( + s"ALERT: ${source} has production values below the threshold of ${alertThreshold}MW. Please scan system for details!" + ) } } - if (alert) { - println( - s"ALERT: ${source} has production values below the threshold of ${alertThreshold}MW. Please scan systems for details!" - ) - } + } catch { + case e: Exception => + return } }