我需要在Scala中创建一个todo-list的控制台界面应用程序。我已经创建了数据访问层,其中我的数据库对象以及通过Slick 3上的查询。此外,我还试图使用STDIN在Scala中读取输入来创建一个简单的接口。这是我的接口代码:
object Main {
val db = Database.forConfig("scalaxdb")
val userRepository = new UserRepository(db)
val taskRepository = new TaskRepository(db)
def main(args: Array[String]): Unit = {
println("Main menu:" + " n1 - Login" + "n2 - Exit")
println("nChoose the operation you want to perform:")
val inputMainMenu = readInt()
buildMainMenu(inputMainMenu)
}
def buildMainMenu(inputNumber: Int) = inputNumber match {
case 1 => enterSystem()
case 2 => System.exit(0)
case _ => println("Your input was wrong. Try again"); System.exit(0)
}
def enterSystem(): Unit ={
println("Input you login, please:")
val inputLogin = readLine()
println("Input you password, please:")
val inputPassword = readLine()
val checkLogin = Await.result(DAO.checkUserLogin(inputLogin, inputPassword), Duration.Inf).toString
val userId = DAO.selectUserId(inputLogin)
def changeOutputs(checkLogin: String):Unit = checkLogin match {
case "true" => println("You have successfully entered");
displayMenu(); buildMenu(userId)
case "false" => println("Your input for login or password is
wrong"); System.exit(1)
case _ => println("Your input is wrong"); System.exit(1)
}
changeOutputs(checkLogin)
}
def displayMenu(): Unit ={
println("TODO List:" + "n1 - Display tasks" + "n2 - Display finished tasks" + "n3 - Display unfinished tasks"
+ "n4 - Add task" + "n5 - Delete task" + "n6 - Mark task as finished")
println("nChoose the operation you want to perform:")
}
val inputNum = readInt()
inputNum
}
def displayUnfinishedTasks(id: Long) = {
println()
println("User's unfinished tasks:n" + Await.result(DAO.selectUnfinishedTasks(id), Duration.Inf).toList.toString)
displayMenu()
}
问题是我需要以用户的身份进入系统,然后输出,创建,删除特定用户的任务。因此,在这里,我决定将用户ID作为参数传递给我所有操纵方法。因此,我尝试使用此界面来执行此操作,但是,我得到了一个运行时错误java.lang.NumberFormatException: For input string: "Vector(1)"
我知道要解决此错误,我可能会以不同的方式安排我的方法,但是我将无法通过用户ID。
要清楚,这就是我的数据访问层方法的看起来像:
def getUserId(login: String) = {
val queryToGetUserId = (for {
user <- UserTable.table if user.login === login
} yield (user.idUser))
db.run(queryToGetUserId.result)
}
那么,如何更改此代码中保留的用户状态的接口方法或整体逻辑?我会感谢任何帮助!
更新!代码更改了一些(方法buildmenu,displayMenu)。另外,我在DAO而不是Getuserid中创建了这种人行道方法。因此,接口有效,但我想知道如何仍然可以更改我的getuserid方法,或者应该调用ID将ID传递到诸如DisplayFinedIndedTasks之类的方法。这是走路方法:
def selectUserId(login: String) = login match {
case "data" => 1
case "root" => 2
}
在上面的评论中,您提到以下行失败:
val userId = Await.result(DAO.getUserId(inputLogin), Duration.Inf).toString.toLong
抛出的例外是:
java.lang.NumberFormatException: For input string: "Vector(1)"
这表明DAO.getUserId
正在返回Vector
而不是Int
。查看该方法的代码:
def getUserId(login: String) = {
val queryToGetUserId = (for {
user <- UserTable.table if user.login === login
} yield (user.idUser))
db.run(queryToGetUserId.result)
}
我们可以看到此方法没有指定的返回类型。返回类型很可能是Future[Seq[Int]]
。由于您只想要一个结果,您应该通过调用head
:
Future[Int]
def getUserId(login: String): Future[Long] = {
val queryToGetUserId = (for {
user <- UserTable.table if user.login === login
} yield (user.idUser)).head // Will throw exception if empty result
db.run(queryToGetUserId.result)
}
...
val userId = Await.result(DAO.getUserId(inputLogin), Duration.Inf)
在这里,我假设idUser
已经是Long
。如果不是这样,那么您需要包括一个.toLong
,但这一次应该起作用!
上面的代码很混乱,我们可以获取github链接还是另一个更新?将其扔入Intellij时,我什至不会格式。
似乎有未完成的行:changeOutputs(checkLogin)=