package com.fuxi.zji
object Ten2 {
def main(args: Array[String]): Unit = {
val data1 = scala.io.Source.fromFile("./src/data1/").getLines()
val data2 = scala.io.Source.fromFile("./src/data2").getLines()
data1.next()
data2.next()
val d1 = data1.map(lien => {
val fields = lien.split("\t")
(fields(0).toInt, fields(1), fields(2), fields(3))
}).toList
val d2 = data2.map(line => {
val fields = line.split("\t")
(fields(0).toInt, fields(1).toInt, fields(2).toInt, fields(3).toInt,
fields(4).toDouble, fields(5).toDouble)
}).toList
println(d1)
/*未支付的订单数量*/
val re1 = d1.filter(_._4 == "PENDING_PAYMENT").size
println(re1)
println(d2)
//计算每个订单对应的商品id
import scala.collection.mutable.Map
val orderMap = Map[Int, List[Int]]()
d1.foreach(da => {
val products = d2.filter(_._2 == da._1).map(_._3)
orderMap.put(da._1, products)
println(products)
})
println(orderMap)
for ((k, v) <- orderMap) {
println(s"订单:$k 商品的明细:${v.mkString(",")}")
}
//计算每个订单的总金额
val orders = Map[Int, List[Double]]()
d1.foreach(order => {
val total = d2.filter(_._2 == order._1).map(_._5)
orders.put(order._1, total)
})
for ((k, v) <- orders) {
println(s"订单:$k 总金额:${v.sum}")
}
//找出消费最多的客户
val cus = d1.map(order => {
val toto = d2.filter(_._2 == order._1).map(_._5)
(order._1, order._3, toto.sum)
})
println(cus)
val (k, v) = cus.groupBy(_._2)
.map(kvs => (kvs._1, kvs._2.map(_._3).sum)).toList
.sortWith(_._2 > _._2).head
println(s"$k,$v")
}
}
scala 订单查询
最新推荐文章于 2024-08-30 23:27:36 发布