Skip to content

Commit

Permalink
Merge pull request #62 from chenhowy/master
Browse files Browse the repository at this point in the history
Added feature to handle multiple users
  • Loading branch information
chenhowy committed Mar 30, 2024
2 parents fcea0fd + 25356a4 commit 917bf63
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 39 deletions.
12 changes: 6 additions & 6 deletions src/main/java/financeproject/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ public static void main(String[] args) throws SecurityException, ExceededAttempt
Parser parser = new Parser(ui);
BaseCommand baseCommand = null;
String response = "";

BaseUser tempUser = new BaseUser("Bob", ui);
Authentication auth = tempUser.getAuthentication();
BaseUser user = new BaseUser(ui, storage);
Authentication auth = user.getAuthentication();
InactivityTimer inactivityTimer = new InactivityTimer();
boolean isAuthenticated = false;

while (!isAuthenticated && auth.getWrongAttempts() < 3) {
try {
if (!auth.authenticate()) {
if (!auth.authenticate(user.getUsername())) {
ui.printMessage("Authentication error");
} else {
ui.printMessage("Password is correct. You are now logged in");
manager = storage.loadFile();
manager = storage.loadFile(user.getUsername());
isAuthenticated = true;
}
} catch (ExceededAttemptsException e) {
Expand Down Expand Up @@ -71,7 +71,7 @@ public static void main(String[] args) throws SecurityException, ExceededAttempt
System.out.println("Uh-oh, something went wrong: " + e.getMessage());
}

storage.saveFile(manager);
storage.saveFile(user.getUsername(), manager);

try {
inactivityTimer.checkTimeElapsed();
Expand Down
41 changes: 33 additions & 8 deletions src/main/java/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,36 @@ public class Storage {
public Storage(String filePath) {
this.filePath = filePath;
}

public TransactionManager loadFile() {
File f = new File(filePath + "/data.txt");

public void addNewUser(String username, String password) {
try {
FileWriter fw = new FileWriter(filePath + "/passwords.txt", true);
fw.write(username + "|" + password + "\n");
fw.close();
} catch (IOException e) {
System.out.println("Could not add user");
}
}

public String loadPassword(String username) {
File f = new File(filePath + "/passwords.txt");
try {
Scanner sc = new Scanner(f);
while (sc.hasNext()) {
String line = sc.nextLine();
if (line.startsWith(username)) {
return line.split("\\|")[1];
}
}
return null;
} catch (FileNotFoundException e) {
createFileDir();
return null;
}
}

public TransactionManager loadFile(String username) {
File f = new File(filePath + String.format("/%s.txt", username));
TransactionManager manager = new TransactionManager();
try {
Scanner sc = new Scanner(f);
Expand All @@ -44,14 +71,12 @@ public TransactionManager loadFile() {

private void createFileDir() {
File f = new File(filePath);
if (!f.mkdir()) {
System.out.println("create file failed");
}
f.mkdir();
}

public void saveFile(TransactionManager tm) {
public void saveFile(String username, TransactionManager tm) {
try {
FileWriter fw = new FileWriter(filePath + "/data.txt");
FileWriter fw = new FileWriter(filePath + String.format("/%s.txt", username));
fw.write(tm.toSave());
fw.close();
} catch (IOException e) {
Expand Down
14 changes: 1 addition & 13 deletions src/main/java/user/Authentication.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,9 @@ public int getWrongAttempts() {
return wrongAttempts;
}

public boolean authenticate() throws ExceededAttemptsException {
System.out.println("username: ");
String inputUsername = this.ui.readInput();
public boolean authenticate(String inputUsername) throws ExceededAttemptsException {
System.out.println("password: ");
String inputPassword = this.ui.readInput();
/*try {
return this.checkPassword(inputUsername, inputPassword);
} catch (ExceededAttemptsException e) {
if (e.isCanTryAgain()) {
System.out.println("Wrong password or username, please try again.");
} else {
System.out.println("Sorry, too many incorrect attempts.");
}
return false;
}*/
return this.checkPassword(inputUsername, inputPassword);
}
}
37 changes: 30 additions & 7 deletions src/main/java/user/BaseUser.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
package user;

import storage.Storage;
import userinterface.UI;

public class BaseUser {
String name;
String username;
Authentication auth;
UI ui;

public BaseUser(String name, UI ui) {
this.name = name;
String username = name.replace(" ", "_");
this.auth = new Authentication("password", username, ui);
public BaseUser(UI ui, Storage storage) {
ui.printMessage("username:");
String pw = null;
do {
username = ui.readInput().replace(" ", "_");
pw = storage.loadPassword(username);
if (pw == null) {
ui.printMessage(String.format("User does not exist, do you want to create a new user with " +
"username %s? Yes or No", username));
if (ui.readInput().equals("Yes")) {
ui.printMessage("Please enter a new password:");
pw = ui.readInput();
storage.addNewUser(username, pw);
ui.printMessage("User successfully created, enter your password again to login");
} else {
ui.printMessage("Please enter another username:");
}
}
} while (pw == null);
this.auth = new Authentication(pw, username, ui);
this.ui = ui;
}

public BaseUser(String username, String pw, UI ui, Storage storage) {
this.username = username;
this.auth = new Authentication(pw, username, ui);
this.ui = ui;
}

public String getName() {
return name;
public String getUsername() {
return username;
}

public Authentication getAuthentication(){
Expand Down
1 change: 1 addition & 0 deletions src/test/data/passwords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bob|password
4 changes: 3 additions & 1 deletion src/test/java/user/BaseUserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import customexceptions.ExceededAttemptsException;
import org.junit.jupiter.api.Test;

import storage.Storage;
import userinterface.UI;

import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -11,7 +12,8 @@ public class BaseUserTest {
@Test
public void sampleTest() throws ExceededAttemptsException {
UI ui = new UI();
BaseUser user = new BaseUser("Bob", ui);
Storage storage = new Storage("./data");
BaseUser user = new BaseUser("Bob", "password", ui, storage);
Authentication auth = user.getAuthentication();
assertTrue(auth.checkPassword("Bob", "password"));
}
Expand Down
2 changes: 1 addition & 1 deletion text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Welcome. Enter your username and password to login.
username:
username:
password:
Password is correct. You are now logged in
Ok. Added inflow
Expand Down
3 changes: 0 additions & 3 deletions text-ui-test/data/data.txt

This file was deleted.

1 change: 1 addition & 0 deletions text-ui-test/data/passwords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bob|password

0 comments on commit 917bf63

Please sign in to comment.