Created
September 1, 2018 08:47
-
-
Save adammb86/2fde6fb79b8f7e38428573437a1a16ae to your computer and use it in GitHub Desktop.
Jawaban Latihan Clean Code For Error Handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cleancode.trycatchclean; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
public class DirtyMain { | |
public static void main(String[] args) throws MalformedURLException { | |
final URL dashboard = new URL("http://dashboard"); | |
final URL loginPage = new URL("http://login"); | |
final String userId = "userid"; | |
final String pwd = "password"; | |
final long twoFactorPwd = 12; | |
User user; | |
Authenticator authenticator = new Authenticator(); | |
try { | |
user = authenticator.login(userId, pwd); | |
} catch (Exception e) { | |
try { | |
user = authenticator.gmailLogin(userId, pwd); | |
} catch (Exception gmailException) { | |
Dispatcher.redirect(loginPage); | |
return; | |
} | |
} | |
URL target; | |
try { | |
authenticator.twoFactor(user, twoFactorPwd); | |
target = dashboard; | |
} catch (Exception e) { | |
target = loginPage; | |
} | |
Dispatcher.redirect(target); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cleancode.trycatchclean; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
public class CleanMain { | |
public static void main(String[] args) throws MalformedURLException { | |
final URL dashboard = new URL("http://dashboard"); | |
final URL loginPage = new URL("http://login"); | |
final String userId = "userid"; | |
final String pwd = "password"; | |
final long twoFactorPwd = 12; | |
AuthenticatorWrapper wrapper = new AuthenticatorWrapper(userId, pwd, twoFactorPwd); | |
URL target; | |
try { | |
wrapper.login(); | |
target = dashboard; | |
} catch (Exception e) { | |
target = loginPage; | |
} | |
Dispatcher.redirect(target); | |
} | |
static class AuthenticatorWrapper { | |
private final String userId; | |
private final String pwd; | |
private final long twoFactorPwd; | |
private final Authenticator authenticator; | |
private User user; | |
AuthenticatorWrapper(String userId, String pwd, long twoFactorPwd) { | |
super(); | |
this.userId = userId; | |
this.pwd = pwd; | |
this.twoFactorPwd = twoFactorPwd; | |
this.authenticator = new Authenticator(); | |
} | |
public void login() throws Exception { | |
user = tryLogin(); | |
if (null == user) { | |
user = authenticator.gmailLogin(userId, pwd); | |
} | |
authenticator.twoFactor(user, twoFactorPwd); | |
} | |
private User tryLogin() { | |
try { | |
user = authenticator.login(userId, pwd); | |
} catch (Exception e) { | |
user = null; | |
} | |
return user; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment