Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[W7][T11-2]Li Guanlong #40

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
15 changes: 15 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ Format: `help`
Help is also shown if you enter an incorrect command e.g. `abcd`
====

== Login to the system : `login`

Allow the user to login to the system +
Format: `login username password`

[NOTE]
====
Login is required in order to use the address book
====

Examples:

* `login guanlong 12345` +
Logged in as guanlong if the account and password are correct.

== Adding a person: `add`

Adds a person to the address book. +
Expand Down
51 changes: 51 additions & 0 deletions src/seedu/addressbook/login/Login.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package seedu.addressbook.login;

import java.util.Map;
import java.util.HashMap;

public class Login {

private boolean loginStatus;

public static final String LOGIN_PROMPT = "Please login in order to use the address book";
private static final String SUCCESS = "Login Success, loading...";
private static final String Invalid_USERNAME_OR_PASSWORD = "Invalid username or password, please try again";
private static final String Invalid_FORMAT = "Please input the correct login command: login username password";
private Map<String, String> accounts = new HashMap<String, String>();


public Login(){
this.loginStatus = false;
this.accounts.put("guanlong", "12345");
this.accounts.put("doctorA", "doctorA");
}

public void setLoginStatus(boolean newStatus){
this.loginStatus = newStatus;
}

public boolean getLoginStatus(){
return loginStatus;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing header comment. All non-trivial methods should have java doc format header comments.

public String checkLoginInfo(String userCommandText){
String[] accountInfo = userCommandText.split(" ");
try {
if (accountInfo.length == 3 && accountInfo[0].equals("login")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could apply some refactoring principles here. Multi-condition criteria could be extracted into booleans.

if (accounts.containsKey(accountInfo[1]) && accounts.get(accountInfo[1]).equals(accountInfo[2])) {
this.loginStatus = true;
return SUCCESS;
}
else {
return Invalid_USERNAME_OR_PASSWORD;
}
}
else{
return Invalid_FORMAT;
}
}
catch (Exception e){
return Invalid_FORMAT;
}
}
}
27 changes: 19 additions & 8 deletions src/seedu/addressbook/ui/MainWindow.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package seedu.addressbook.ui;


import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
Expand All @@ -15,13 +14,17 @@

import static seedu.addressbook.common.Messages.*;

import seedu.addressbook.login.Login;

/**
* Main Window of the GUI.
*/

public class MainWindow {

private Logic logic;
private Stoppable mainApp;
private Login login = new Login();

public MainWindow(){
}
Expand All @@ -45,13 +48,21 @@ public void setMainApp(Stoppable mainApp){
void onCommand(ActionEvent event) {
try {
String userCommandText = commandInput.getText();
CommandResult result = logic.execute(userCommandText);
if(isExitCommand(result)){
exitApp();
return;
}
displayResult(result);
String loginResult = login.checkLoginInfo(userCommandText);
CommandResult msg = new CommandResult(loginResult);
displayResult(msg);
clearCommandInput();

if(login.getLoginStatus())
{
CommandResult result = logic.execute(userCommandText);
if(isExitCommand(result)){
exitApp();
return;
}
displayResult(result);
clearCommandInput();
}
} catch (Exception e) {
display(e.getMessage());
throw new RuntimeException(e);
Expand Down Expand Up @@ -89,7 +100,7 @@ public void displayResult(CommandResult result) {

public void displayWelcomeMessage(String version, String storageFilePath) {
String storageFileInfo = String.format(MESSAGE_USING_STORAGE_FILE, storageFilePath);
display(MESSAGE_WELCOME, version, MESSAGE_PROGRAM_LAUNCH_ARGS_USAGE, storageFileInfo);
display(MESSAGE_WELCOME, version, Login.LOGIN_PROMPT, MESSAGE_PROGRAM_LAUNCH_ARGS_USAGE, storageFileInfo);
}

/**
Expand Down
41 changes: 41 additions & 0 deletions test/java/seedu/addressbook/login/LoginTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package seedu.addressbook.login;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.Parameterized;

import static org.junit.Assert.*;

import seedu.addressbook.login.Login;

public class LoginTest {

private static final String SUCCESS = "Login Success, loading...";
private static final String Invalid_USERNAME_OR_PASSWORD = "Invalid username or password, please try again";
private static final String Invalid_FORMAT = "Please input the correct login command: login username password";
private Login login = new Login();

@Test

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing header comment. All non-trivial methods should have java doc format header comments.

public void setLoginStatus() {
login.setLoginStatus(true);
assertEquals(login.getLoginStatus(), true);
}

@Test

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing header comment. All non-trivial methods should have java doc format header comments.

public void getLoginStatus() {
login.setLoginStatus(false);
assertEquals(login.getLoginStatus(), false);
}

@Test

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing header comment. All non-trivial methods should have java doc format header comments.

public void checkLoginInfo() {
final String input1 = "login guanlong 12345";
final String input2 = "some_random_string";
final String input3 = "login wrong_username wrong_password";
assertEquals(login.checkLoginInfo(input1), SUCCESS);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job here, providing the positive and negative test cases.

assertEquals(login.checkLoginInfo(input2), Invalid_FORMAT);
assertEquals(login.checkLoginInfo(input3), Invalid_USERNAME_OR_PASSWORD);
}
}