题目如下
- 使用case class创建图书信息类Book:包含四个属性:ID,书名,作者,价格,数量。
- 创建一个名为bookList的可变List,它只能用来保存Book的实例。
- 初始化三本不同的书(注意id不同就是不同的书),加入到bookList中。
- 添加一本已经存在的书。此时应该去修改bookList中对应书目的数量,而不是去添加一条新数据。
- 根据图书名称查询是否在列表中(通过遍历列表对比名称)
- 删除指定书名的书
- 删除指定ID的书
- 对于图书按价格从高到低排序。
- 遍历图书列表,并打印每本书的详细信息
- 计算全部总的金额
代码如下
package caseclass
object caseclass03 {
case class Book(id: String, name: String, anthor: String, price: Double, amount: Int)
def main(args: Array[String]): Unit = {
val book1 = Book("1", "凡人修仙传", "忘语", 80.5, 10)
val book2 = Book("2", "霸道总裁爱上我", "忘语1", 50.0, 9)
val book3 = Book("3", "重生之我的十八岁太奶", "忘语", 20.0, 5)
val bookList = scala.collection.mutable.ListBuffer(book1, book2, book3)
println("=== 初始化后的图书列表 ===")
printBookList(bookList)
println("\n=== 4. 添加已存在的图书(ID=1,数量+5) ===")
val existBook = Book("1", "凡人修仙传", "忘语", 80.5, 5)
addOrUpdateBook(bookList, existBook)
printBookList(bookList)
println("\n=== 5. 按书名查询 ===")
val queryName1 = "凡人修仙传"
val queryName2 = "不存在的书"
println(s"查询《$queryName1》:${if (checkBookExist(bookList, queryName1)) "存在" else "不存在"}")
println(s"查询《$queryName2》:${if (checkBookExist(bookList, queryName2)) "存在" else "不存在"}")
println("\n=== 6. 删除指定书名的书(霸道总裁爱上我) ===")
deleteBookByName(bookList, "霸道总裁爱上我")
printBookList(bookList)
println("\n=== 7. 删除指定ID的书(ID=3) ===")
deleteBookById(bookList, "3")
printBookList(bookList)
bookList += Book("2", "霸道总裁爱上我", "忘语1", 50.0, 9)
bookList += Book("3", "重生之我的十八岁太奶", "忘语", 20.0, 5)
println("\n=== 重新补充数据后 ===")
printBookList(bookList)
println("\n=== 8. 按价格从高到低排序 ===")
val sortedBooks = sortBooksByPriceDesc(bookList)
sortedBooks.foreach(ele => {
println(s"${ele.id} ${ele.name} ${ele.anthor} ${ele.price} ${ele.amount}")
})
println("\n=== 10. 计算总金额 ===")
val totalAmount = calculateTotalAmount(bookList)
println(f"全部图书总金额:¥$totalAmount%.2f")
}
def addOrUpdateBook(bookList: scala.collection.mutable.ListBuffer[Book], newBook: Book): Unit = {
val index = bookList.indexWhere(_.id == newBook.id)
if (index != -1) {
val oldBook = bookList(index)
bookList.update(index, oldBook.copy(amount = oldBook.amount + newBook.amount))
} else {
bookList += newBook
}
}
def checkBookExist(bookList: scala.collection.mutable.ListBuffer[Book], name: String): Boolean = {
var exist = false
for (book <- bookList if !exist) {
if (book.name == name) {
exist = true
}
}
exist
}
def deleteBookByName(bookList: scala.collection.mutable.ListBuffer[Book], name: String): Unit = {
bookList --= bookList.filter(_.name == name)
}
def deleteBookById(bookList: scala.collection.mutable.ListBuffer[Book], id: String): Unit = {
bookList --= bookList.filter(_.id == id)
}
def sortBooksByPriceDesc(bookList: scala.collection.mutable.ListBuffer[Book]): List[Book] = {
bookList.sortBy(_.price)(Ordering[Double].reverse).toList
}
def printBookList(bookList: scala.collection.mutable.ListBuffer[Book]): Unit = {
bookList.foreach(ele => {
println(s"${ele.id} ${ele.name} ${ele.anthor} ${ele.price} ${ele.amount}")
})
}
def calculateTotalAmount(bookList: scala.collection.mutable.ListBuffer[Book]): Double = {
var total = 0.0
for (book <- bookList) {
total += book.price * book.amount
}
total
}
}