import scala.collection.mutable.ListBuffer
//1
case class Book(id: String, name: String, author: String, price: Double, quantity: Int)
object Test1 {
def main(args: Array[String]): Unit = {
//2
var bookList =ListBuffer[Book]()
//3
bookList +=Book("001","book1","author1",12,3)
bookList +=Book("002","book2","author2",14,6)
bookList +=Book("003","book3","author3",17,5)
//4
val book1 =Book("005","book5","author5",18,20)
val rs=bookList.find(_.id==book1.id)
if(rs.isEmpty){
println("没有")
bookList +=book1
}else{
println("有")
val s=rs.get
s.quantity+=book1.quantity
}
//5
var name="book5"
val rs1=bookList.find(_.name == name)
if(rs1.isEmpty)
println(s"《$name》 不存在")
else
println(s"《$name》 存在")
//6.
name="book1"
val rs2 = bookList.find(_.name==name)
if(rs2.isEmpty)
println(s"《$name》 不存在,删除失败")
else {
println(s"《$name》 存在,删除成功")
bookList -=rs2.get
}
//7.
val id="002"
val rs3 =bookList.find(_.id == id)
if(rs3.isEmpty)
println(s"id: $id 不存在,删除失败")
else {
println(s"id: $id 存在,删除成功")
bookList-=rs3.get
}
//8.
val orderList=bookList.sortWith((a,b)=>a.price >b.price)
//9.
orderList.foreach(el =>{
println(s"${el.name},${el.author},${el.price},${el.quantity}")
})
//10
var sum:Double=0;
bookList.foreach((el=> {
sum +=(el.quantity*el.price)
}))
println(s"总的金额是:${sum}")
}
}