Skip to content

Instantly share code, notes, and snippets.

@DhavalDalal
Last active April 15, 2019 03:29
Show Gist options
  • Save DhavalDalal/8bfb3f4bec989d7362ff58535bc9d386 to your computer and use it in GitHub Desktop.
Save DhavalDalal/8bfb3f4bec989d7362ff58535bc9d386 to your computer and use it in GitHub Desktop.
Try this or try that

Try this or try that (Scala)

  • authenticate attempts to login the user into the Application. It does so as defined below:
    • It first attempts to log the user using the application user name and password.
    • In case appLogin fails, it attempts to log the user using user's Gmail Id and password.
    • In case gmailLogin fails, it attempts to log the user using user's Facebook Id and password.
    • In case fbLogin fails, it gives up and does not authenticate the user.
  • Compare notes on your solutions after solving Part 1 and Part 2

Problem Part 1

  • Look at login.scala and refactor this code using Try

Problem Part 2

  • Look at login2.scala and implement the methods marked with ???
  • Refactor authenticate using recoverWith

Problem Part 3

  • Look at login3.scala and refactor this code using Future.
  • Feel free to change other functions as you deem fit, but you are not allowed to change the signature of the authenticate function.
class AuthenticationFailed private(message: String = "") extends Throwable(message) {
def this(message: String, cause: Throwable) = {
this(message)
initCause(cause)
}
def this(cause: Throwable) = this(Option(cause).map(_.toString).orNull, cause)
}
// Companion Object
object AuthenticationFailed {
def apply() = new AuthenticationFailed(null: String)
def apply(message: String) = new AuthenticationFailed(message)
def apply(cause: Throwable) = new AuthenticationFailed(cause)
def apply(message: String, cause: Throwable) = new AuthenticationFailed(message, cause)
def unapply(ex: AuthenticationFailed): Option[(String, Throwable)] = Some((ex.getMessage, ex.getCause))
}
def appLogin(name: String, password: String) = {
if (name.equals("tester")) true
else throw AuthenticationFailed(s"App. account with $name does not exist!")
}
def gmailLogin(name: String, password: String) = {
if (name.equals("[email protected]")) true
else throw AuthenticationFailed(s"Gmail account with $name does not exist!")
}
def fbLogin(name: String, password: String) = {
if (name.equals("[email protected]")) true
else throw AuthenticationFailed(s"Facebook with $name does not exist!")
}
// NOTE: printlns are given to help you grasp the flow very quickly,
// when you run the program. They are not required to be present
// when refactoring.
//
def authenticate(name: String, password: String): Boolean = {
try {
println(s"Trying Application Login...")
appLogin(name, password)
} catch {
case AuthenticationFailed(msg, cause) =>
println(s"Failed App Login => $msg")
try {
println(s"Trying Gmail Login...")
gmailLogin(name, password)
} catch {
case AuthenticationFailed(msg, cause) =>
println(s"Failed Gmail Login => $msg")
try {
println(s"Trying Facebook Login...")
fbLogin(name, password)
} catch {
case AuthenticationFailed(msg, cause) =>
println(s"Failed Facebook Login => $msg")
false
}
}
}
}
println(authenticate("tester", "password")) // true
println(authenticate("[email protected]", "aPassword")) // true
println(authenticate("[email protected]", "psswd")) // true
println(authenticate("someone", "somepassword")) // false
class AuthenticationFailed private(message: String = "") extends Throwable(message) {
def this(message: String, cause: Throwable) = {
this(message)
initCause(cause)
}
def this(cause: Throwable) = this(Option(cause).map(_.toString).orNull, cause)
}
// Companion Object
object AuthenticationFailed {
def apply() = new AuthenticationFailed(null: String)
def apply(message: String) = new AuthenticationFailed(message)
def apply(cause: Throwable) = new AuthenticationFailed(cause)
def apply(message: String, cause: Throwable) = new AuthenticationFailed(message, cause)
def unapply(ex: AuthenticationFailed): Option[(String, Throwable)] = Some((ex.getMessage, ex.getCause))
}
// Implement these
import scala.util.{Try, Success, Failure}
def appLogin(name: String, password: String): Try[Boolean] = ???
def gmailLogin(name: String, password: String): Try[Boolean] = ???
def fbLogin(name: String, password: String): Try[Boolean] = ???
def authenticate(name: String, password: String): Boolean = ???
println(authenticate("tester", "password")) // true
println(authenticate("[email protected]", "aPassword")) // true
println(authenticate("[email protected]", "psswd")) // true
println(authenticate("someone", "somepassword")) // false
def appLogin(name: String, password: String) = {
println(s"appLogin on => ${Thread.currentThread}")
Thread.sleep(500)
if (name.equals("tester")) true
else throw AuthenticationFailed(s"App. account with $name does not exist!")
}
def gmailLogin(name: String, password: String) = {
println(s"gmailLogin on => ${Thread.currentThread}")
Thread.sleep(1000)
if (name.equals("[email protected]")) true
else throw AuthenticationFailed(s"Gmail account with $name does not exist!")
}
def fbLogin(name: String, password: String) = {
println(s"gmailLogin on => ${Thread.currentThread}")
Thread.sleep(1500)
if (name.equals("[email protected]")) true
else throw AuthenticationFailed(s"Facebook with $name does not exist!")
}
// NOTE: printlns are given to help you grasp the flow very quickly,
// when you run the program. They are not required to be present
// when refactoring.
//
def authenticate(name: String, password: String): Boolean = {
try {
println(s"Trying Application Login...")
appLogin(name, password)
} catch {
case AuthenticationFailed(msg, cause) =>
println(s"Failed App Login => $msg")
try {
println(s"Trying Gmail Login...")
gmailLogin(name, password)
} catch {
case AuthenticationFailed(msg, cause) =>
println(s"Failed Gmail Login => $msg")
try {
println(s"Trying Facebook Login...")
fbLogin(name, password)
} catch {
case AuthenticationFailed(msg, cause) =>
println(s"Failed Facebook Login => $msg")
false
}
}
}
}
println(authenticate("tester", "password")) // true
println(authenticate("[email protected]", "aPassword")) // true
println(authenticate("[email protected]", "psswd")) // true
println(authenticate("someone", "somepassword")) // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment