scala图书管理系统 【dao】软件包
BookDAO
package org.app
package dao
import org.app.modeis.UserMode
import scala.collection.mutable.ListBuffe
class UserDAO {
// 加载所有的用户
def loadUsers(): ListBuffer[UserModel] = {
val users = new ListBuffer[UserModel]()
val source = scala.io.Source.fromFile("users.txt")
for(line <- source.getLines()){
val Array(username, password, role) = line.split(",")
// 实例化一个用户,保存到List
users += UserModel(username, password, role)
}
// 关闭连接
source.close()
users
}
def saveUsers(users: ListBuffer[UserModel]): Unit = {
val writer = new java.io.PrintWriter(new java.io.File("users.txt"))
for (user <- users) {
writer.println(user.username+ "," + user.password + "," + user.role)
}
writer.close()
}
}
BorrowRecordDAO
package org.app
package dao
import org.app.modeis.BorrowRecordModel
import scala.collection.mutable.ListBuffer
import scala.io.Source
class BorrowRecordDAO {
// 读出借阅记录
def loadBorrowRecords(): ListBuffer[BorrowRecordModel] = {
val borrowRecords = ListBuffer[BorrowRecordModel]()
val lines = Source.fromFile("borrow_records.txt")
for(line <- lines.getLines()){
val parts=line.split(",")
borrowRecords += BorrowRecordModel(
parts(0),
parts(1).toInt,
parts(2),
parts(3),
if (parts.length>4) Some(parts(4)) else None
)
}
borrowRecords
}
// 写入借阅记录
def saveBorrowRecords(records: ListBuffer[BorrowRecordModel]): Unit = {
val writer = new java.io.PrintWriter("borrow_records.txt")
for(record <- records){ writer.println(record.userName+","+record.bookID+","+record.bookName+","+record.borrowDate+","+record.returnDate.getOrElse(""))
}
writer.close()
}
}
UserDAO
package org.app
package dao
import org.app.modeis.UserModel
import scala.collection.mutable.ListBuffer
class UserDAO {
// 加载所有的用户
def loadUsers(): ListBuffer[UserModel] = {
val users = new ListBuffer[UserModel]()
val source = scala.io.Source.fromFile("users.txt")
for(line <- source.getLines()){
val Array(username, password, role) = line.split(",")
// 实例化一个用户,保存到List
users += UserModel(username, password, role)
}
// 关闭连接
source.close()
users
}
def saveUsers(users: ListBuffer[UserModel]): Unit = {
val writer = new java.io.PrintWriter(new java.io.File("users.txt"))
for (user <- users) {
writer.println(user.username+ "," + user.password + "," + user.role)
}
writer.close()
}
}