Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.groupeseb.sebanalytics.dummy
- import scala.util.{Failure, Success, Try, Using}
- import scala.util.matching.Regex
- //import org.apache.commons.lang3.StringUtils
- import com.groupeseb.sebanalytics.dummy
- //import pureconfig.*
- //import pureconfig.generic.auto.*
- //import pureconfig.generic.semiauto.deriveReader
- //import commons.lang3.bridge.StringUtils.ops._
- object Dummy {
- private def reformatDate(dateStr: String): CustomDate = {
- val Array(year, month, day) = dateStr.split("-").map(_.trim)
- CustomDate(year = year.toInt, month = month.toInt, day = day.toInt)
- }
- private def SaleMatch(fields: Seq[String]): Try[SaleStruct] = {
- import SaleFields._
- fields match {
- case Seq(numberRegex(field1), numberRegex(field2), numberRegex(field3), numberRegex(field4), dateRegex(field5)) =>
- Success(SaleStruct(field1.toInt, field2.toInt, field3.toInt, field4.toInt, reformatDate(field5)))
- case _ => Failure(new Exception("Invalid input format"))
- }
- }
- private def ClientMatch(fields: Seq[String]): Try[ClientStruct] = {
- import ClientFields._
- fields match {
- case Seq(numberRegex(field1), strRegex(field2), numberRegex(field3)) =>
- Success(ClientStruct(field1.toInt, field2, field3.toInt))
- case _ => Failure(new Exception("Invalid input format"))
- }
- }
- private def ProductMatch(fields: Seq[String]): Try[ProductStruct] = {
- import ProductFields._
- fields match {
- case Seq(numberRegex(field1), strRegex(field2), strRegex(field3), strRegex(field4)) =>
- Success(ProductStruct(field1.toInt, field2, field3, field4))
- case _ => Failure(new Exception("Invalid input format"))
- }
- }
- def splitCSVLine(line: String): Seq[String] = {
- line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1).map(_.trim)
- }
- def parseCSV[T](path: String, matchFunction: Seq[String] => Try[T]): List[Try[T]] = {
- Using.resource(scala.io.Source.fromFile(path)) { source =>
- val lines = source.getLines().toList
- lines.tail.map(line => matchFunction(splitCSVLine(line)))
- }
- }
- def flattenSeq[T](seq: Seq[Try[T]]): Seq[T] = {
- // seq.map(_.toOption).flatten
- // seq.flatMap(_.toOption)
- // seq.filter(_.isSuccess).map(_.get)
- seq.collect{
- case Success(x) => x
- }
- val (successes, failures) = seq.partition(_.isSuccess)
- successes.map(_.get)
- }
- def main(args: Seq[String]): Unit = {
- val config: Config = Config(
- paths = Paths(
- sales = "scala/Dummy/target/classes/sales.csv",
- client = "scala/Dummy/target/classes/client.csv",
- products = "scala/Dummy/target/classes/products.csv"
- ), filters = Filters(
- age = 50,
- year = 2023,
- category = "SDA"
- )
- )
- // case class Conf(val1: Int, val2: String)
- // implicit val configReader: ConfigReader[Conf] = deriveReader[Conf]
- //
- // val conf: Conf = ConfigSource.default.load[Conf] match
- // case Left(failure) => throw new RuntimeException(s"Error loading configuration: $failure")
- // case Right(res) => conf
- //
- // println(conf.val1)
- // println(System.getProperty("user.dir"))
- val salesList = flattenSeq(
- parseCSV(config.paths.sales, SaleMatch)
- )
- val clientList = flattenSeq(
- parseCSV(config.paths.client, ClientMatch)
- )
- val productList = flattenSeq(
- parseCSV(config.paths.products, ProductMatch)
- )
- if (salesList.isEmpty || clientList.isEmpty || productList.isEmpty) {
- throw new IllegalArgumentException("One of the CSV does not contain any valid data")
- }
- val clientdFiftyID = clientList.filter(_.age < config.filters.age).map(_.id)
- val sales2023 = salesList.filter(_.date.year == config.filters.year)
- val productPoeleID = productList.filter(_.category.contains(config.filters.category)).map(_.id)
- val salesFilterClient = sales2023.filter(sale => clientdFiftyID.contains(sale.client_id)).toList
- val finalList: List[SaleStruct] = salesFilterClient.filter(sale => productPoeleID.contains(sale.product_id))
- // val regedx1: Regex = ".*".r
- // finalList match {
- // case regedx1(head) :: head1 :: head2 :: _ => SaleStruct(head, head1 ...)
- // case _ =>
- // }
- // find the number of SDA sales for people younger than 50 for year 2023
- // le nombre de poele achetées par les ménagères de moins de 50 ans en 2023
- // val result = 234
- val result = finalList.map(_.quantity).sum
- if (result == 234) println("Yay you win !")
- else println("Try again !")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement