17 阅读1分钟
package caseclass

import scala.collection.mutable.ListBuffer

object caseclass4 {
  //1
  case class Book(id:Int,name:String,author:String,price:Double,amount:Int)
  def main(args: Array[String]): Unit = {
  //2
  val BookList:ListBuffer[Book]=ListBuffer()
  //3
  val book1=Book(1,"凡人修仙传","梦语",20.2,1)
  val book2=Book(2,"霸道总裁爱上我","梦语",100,1)
  val book3=Book(3,"重生之超级保镖","梦语",50,1)
  BookList+=book1
  BookList+=book2
  BookList+=book3

  //7删除id为1的书
  // val id=1
    //BookList.find(

  // BookList.remove(0)
  //8
  val newList=BookList.sortWith((a,b)=>{
    a.price>b.price
  })
  //9
    newList.foreach(ele=>{
    println(s"${ele.id}${ele.name}${ele.price}")
  })

  //10
  var totalPrice=0.0
  BookList.foreach(ele=>{
    totalPrice+=ele.price*ele.amount
  })
    println(s"总价格:${totalPrice}")

  }
}

image.png