LibrarayPresentation
package org.apppackage ui
import org.app.modeis.{BookModel, UserModel}
import org.app.service.{BookService, UserService}
import scala.io.StdIn
import scala.io.StdIn.readLine
class LibrarayPresentation {
private val BookService = new BookService()
private val UserService = new UserService()
//辅助方法,输入图书信息,返回一个BookModel
private def inputBookInfo():BookModel={
println("请输入图书的名称:")
val name=readLine().trim
println("请输入图书的作者:")
val author=readLine().trim
println("请输入图书是否可以外接(true/false):")
val available=StdIn.readBoolean()
BookModel(0,name,author,available)
}
// 显示游客的菜单
def showVisitorMenu(): Unit = {
var running = true
while (running) {
println("欢迎来到我的图书管理系统, 请选择")
println("1. 查看所有图书")
println("2. 查询图书")
println("3. 登录")
println("4. 离开")
// 获取用户的操作
val choice = StdIn.readLine().trim
choice match {
case "1" =>
// 调用业务逻辑层的方法
val results = BookService.searchBooks("")
if(results.nonEmpty){
results.foreach(println)
} else {
println("没有找到图书")
}
case "2" =>
val query = readLine("请输入查询关键字(书名,作者):").trim
val results = BookService.searchBooks(query)
if(results.nonEmpty){
println("=======查询图书的结果:=======")
results.foreach(println)
} else {
println("没有找到图书")
}
case "3" =>
println("请输入用户名:")
val username = StdIn.readLine().trim
println("请输入密码:")
val password = StdIn.readLine().trim
// 调用Service的方法,进行登录
val userOpt = UserService.authenticateUser(username, password)
if(userOpt.isEmpty){
println("用户名或密码错误")
} else {
println("登录成功")
val user = userOpt.get
user.role match {
case "管理员" => showAdminMenu(user)
case "普通用户" => showUserMenu(user)
}
}
case "4" =>
running = false
println("感谢你的使用,下次再见")
case _ => println("无效的选择")
}
}
}
// 显示管理员的菜单
def showAdminMenu(user:UserModel): Unit = {
var running = true
while (running) {
println(s"欢迎管理员:${user.username},来到我的图书管理系统, 请选择")
println("1. 添加图书")
println("2. 查询图书")
println("3. 添加用户")
println("4. 退出")
// 获取用户的操作
val choice = StdIn.readLine().trim
choice match {
case "1" =>
val book=inputBookInfo()
BookService.addBook(book)
println(s"图书《${book.name}》添加成功")
case "2" => println("查询图书")
case "3" =>
val username=StdIn.readLine("请输入用户名:")
if (UserService.addUser(username)){
println("用户添加成功")
}else{
println("用户添加失败")
}
case "4" => running = false
case _ => println("无效的选择")
}
}
}
// 显示登录用户的菜单
def showUserMenu(user:UserModel): Unit = {
var running = true
while (running) {
println(s"欢迎用户:${user.username},来到我的图书管理系统, 请选择")
println("1. 借阅图书")
println("2. 查询借阅图书")
println("3. 还书")
println("4. 退出")
// 获取用户的操作
val choice = StdIn.readLine().trim
choice match {
case "1" =>
try {
val id = readLine("请输入图书的ID:").toInt
BookService.borrowBook(user.username, id)
} catch {
case e:Exception =>
println(e)
println("输入的图书ID无效")
}
case "2" => println("查询借阅图书")
//
val borrowRecords=BookService.queryBorrwRecords(user.username)
if(borrowRecords.isEmpty){
println("没有借阅记录")
}else{
println("查询结果,一共接了几本,换了?本,?本为归还")
for (record <- borrowRecords){
println(record)
val returnDate=record.returnDate.getOrElse("未归还")
println(s"借阅日期:{record.borrowDate}.归还日期:returnDate")
}
}
case "3"=>
try {
val id = readLine("请输入要归还的图书的ID:").toInt
if(BookService.returnBook(user.username, id)){
println("归还图书成功")
}else{
println("归还图书失败")
}
} catch {
case e: Exception =>
println(e)
println("输入的图书ID无效")
}
case "4" => running = false
case _ => println("无效的选择")
}
}
}
def showMenu(): Unit = {
showVisitorMenu()
}
}
大数据