package dao
import models.BookModel
import scala.collection.mutable.ListBuffer
class BookDAO {
//加载图书。从文件中读入
def loadBooks():List[BookModel]={
val books =new ListBuffer[BookModel]()
val source= scala.io.Source.fromFile("books.txt")
for(line <-source.getLines()){
val Array(id, name, author, available) = line.split(",")
// 实例化一本书,保存到List
books += BookModel(id.toInt, name, author, available.toBoolean)
}
// 关闭连接
source.close()
books
}
// 保存图书。将图书写入文件
def saveBooks(books: ListBuffer[BookModel]): Unit = {
val writer = new java.io.PrintWriter(new java.io.File("books.txt"))
for (book <- books) {
writer.println(book.id + "," + book.name + "," + book.author + "," + book.available)
}
writer.close()
}
}
图书展示功能2
yzj2311282024-12-22 11:05
相关推荐
小坏讲微服务3 小时前
Spring Cloud Alibaba 整合 Scala 教程完整使用Kiri霧3 小时前
Scala 循环控制:掌握 while 和 for 循环pale_moonlight1 天前
十、 Scala 应用实践 (上)小坏讲微服务1 天前
SpringCloud整合Scala实现MybatisPlus实现业务增删改查今天没有盐2 天前
Scala Map集合完全指南:从入门到实战应用BD_Marathon3 天前
sbt 编译打包 scala92743 天前
26 trait-多继承详解渣渣盟4 天前
Flink分布式文件Sink实战解析-大头.4 天前
Scala全链路实战:零基础到生产级今天没有盐5 天前
Scala 访问权限详解:private、protected 与 private