Advertisement
mcaurette

SEB Scala #4

Jan 23rd, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.61 KB | None | 0 0
  1. package com.groupeseb.sebanalytics.dummy
  2.  
  3. import scala.util.{Failure, Success, Try, Using}
  4. import scala.util.matching.Regex
  5. //import org.apache.commons.lang3.StringUtils
  6.  
  7. import com.groupeseb.sebanalytics.dummy
  8.  
  9.  
  10. //import pureconfig.*
  11. //import pureconfig.generic.auto.*
  12. //import pureconfig.generic.semiauto.deriveReader
  13. //import commons.lang3.bridge.StringUtils.ops._
  14.  
  15.  
  16. object Dummy {
  17.  
  18.   private def reformatDate(dateStr: String): CustomDate = {
  19.     val Array(year, month, day) = dateStr.split("-").map(_.trim)
  20.     CustomDate(year = year.toInt, month = month.toInt, day = day.toInt)
  21.   }
  22.  
  23.   private def SaleMatch(fields: Seq[String]): Try[SaleStruct] = {
  24.     import SaleFields._
  25.  
  26.     fields match {
  27.       case Seq(numberRegex(field1), numberRegex(field2), numberRegex(field3), numberRegex(field4), dateRegex(field5)) =>
  28.         Success(SaleStruct(field1.toInt, field2.toInt, field3.toInt, field4.toInt, reformatDate(field5)))
  29.       case _ => Failure(new Exception("Invalid input format"))
  30.  
  31.     }
  32.   }
  33.  
  34.  
  35.   private def ClientMatch(fields: Seq[String]): Try[ClientStruct] = {
  36.     import ClientFields._
  37.  
  38.     fields match {
  39.       case Seq(numberRegex(field1), strRegex(field2), numberRegex(field3)) =>
  40.         Success(ClientStruct(field1.toInt, field2, field3.toInt))
  41.       case _ => Failure(new Exception("Invalid input format"))
  42.  
  43.     }
  44.   }
  45.  
  46.   private def ProductMatch(fields: Seq[String]): Try[ProductStruct] = {
  47.     import ProductFields._
  48.  
  49.     fields match {
  50.       case Seq(numberRegex(field1), strRegex(field2), strRegex(field3), strRegex(field4)) =>
  51.         Success(ProductStruct(field1.toInt, field2, field3, field4))
  52.       case _ => Failure(new Exception("Invalid input format"))
  53.  
  54.     }
  55.   }
  56.  
  57.   def splitCSVLine(line: String): Seq[String] = {
  58.     line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1).map(_.trim)
  59.   }
  60.  
  61.   def parseCSV[T](path: String, matchFunction: Seq[String] => Try[T]): List[Try[T]] = {
  62.     Using.resource(scala.io.Source.fromFile(path)) { source =>
  63.       val lines = source.getLines().toList
  64.  
  65.       lines.tail.map(line => matchFunction(splitCSVLine(line)))
  66.       }
  67.    
  68.   }
  69.  
  70.   def flattenSeq[T](seq: Seq[Try[T]]): Seq[T] = {
  71. //    seq.map(_.toOption).flatten
  72. //    seq.flatMap(_.toOption)
  73. //    seq.filter(_.isSuccess).map(_.get)
  74.     seq.collect{
  75.       case Success(x) => x
  76.     }
  77.  
  78.     val (successes, failures) = seq.partition(_.isSuccess)
  79.    
  80.     successes.map(_.get)
  81.   }
  82.   def main(args: Seq[String]): Unit = {
  83.    
  84.     val config: Config = Config(
  85.       paths = Paths(
  86.         sales = "scala/Dummy/target/classes/sales.csv",
  87.         client = "scala/Dummy/target/classes/client.csv",
  88.         products = "scala/Dummy/target/classes/products.csv"
  89.       ), filters = Filters(
  90.         age = 50,
  91.         year = 2023,
  92.         category = "SDA"
  93.       )
  94.     )
  95.  
  96.     //    case class Conf(val1: Int, val2: String)
  97.     //    implicit val configReader: ConfigReader[Conf] = deriveReader[Conf]
  98.     //
  99.     //    val conf: Conf = ConfigSource.default.load[Conf] match
  100.     //      case Left(failure) => throw new RuntimeException(s"Error loading configuration: $failure")
  101.     //      case Right(res) => conf
  102.     //
  103.     //      println(conf.val1)
  104.  
  105. //        println(System.getProperty("user.dir"))
  106.     val salesList = flattenSeq(
  107.       parseCSV(config.paths.sales, SaleMatch)
  108.     )
  109.     val clientList = flattenSeq(
  110.       parseCSV(config.paths.client, ClientMatch)
  111.     )
  112.     val productList = flattenSeq(
  113.       parseCSV(config.paths.products, ProductMatch)
  114.     )
  115.  
  116.     if (salesList.isEmpty || clientList.isEmpty || productList.isEmpty) {
  117.       throw new IllegalArgumentException("One of the CSV does not contain any valid data")
  118.     }
  119.  
  120.     val clientdFiftyID = clientList.filter(_.age < config.filters.age).map(_.id)
  121.     val sales2023 = salesList.filter(_.date.year == config.filters.year)
  122.     val productPoeleID = productList.filter(_.category.contains(config.filters.category)).map(_.id)
  123.  
  124.  
  125.     val salesFilterClient = sales2023.filter(sale => clientdFiftyID.contains(sale.client_id)).toList
  126.     val finalList: List[SaleStruct] = salesFilterClient.filter(sale => productPoeleID.contains(sale.product_id))
  127. //    val regedx1: Regex = ".*".r
  128. //    finalList match {
  129. //      case regedx1(head) :: head1 :: head2 :: _ => SaleStruct(head, head1 ...)
  130. //      case _ =>
  131. //    }
  132.  
  133. // find the number of SDA sales for people younger than 50 for year 2023
  134. // le nombre de poele achetées par les ménagères de moins de 50 ans en 2023
  135. //    val result = 234
  136.     val result = finalList.map(_.quantity).sum
  137.     if (result == 234) println("Yay you win !")
  138.     else println("Try again !")
  139.  
  140.   }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement