Skip to content

Commit

Permalink
Merge pull request nus-cs2113-AY2223S2#35 from Sherlock-YH/master
Browse files Browse the repository at this point in the history
Add parseSavedFile
  • Loading branch information
tyuyang authored Mar 14, 2023
2 parents 4be0141 + ae93b46 commit 7100911
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
1 change: 0 additions & 1 deletion data/save.txt

This file was deleted.

12 changes: 8 additions & 4 deletions src/main/java/seedu/bankwithus/BankWithUs.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void exit(String filePath) throws IOException {
/**
* Creates a new Account for a first time user
*/
public void createAccount() {
public void createAccount() {
System.out.println("Whats your name?");
String userName = ui.getNextLine();
System.out.println("How much would you like to add as Balance?");
Expand All @@ -69,13 +69,19 @@ public void createAccount() {
/**
* The main command and output loop. Takes in user input line by line
* and gives it to the parser to execute the command.
*
*
* @throws IOException if something goes wrong while exiting the program
*/
public void run() throws IOException {
ui.createScanner();
if (storage.saveFile.length() < 1) {
createAccount();
} else {
try {
parser.parseSavedFile(accounts);
} catch (IOException e) {
ui.showIOError();
}
}
while (!isExitEntered) {
String line = ui.getNextLine();
Expand All @@ -92,8 +98,6 @@ public static void main(String[] args) {
try {
new BankWithUs(FILE_PATH).run();
} catch (IOException e) {
return;
}
}

}
31 changes: 30 additions & 1 deletion src/main/java/seedu/bankwithus/Parser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package seedu.bankwithus;

import seedu.bankwithus.exceptions.CommandNotFoundException;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Parser {
private BankWithUs bwu;

Expand All @@ -21,7 +26,31 @@ public void parseUserInput(String input) throws CommandNotFoundException {
if (command.equals("exit")) {
bwu.isExitEntered = true;
} else {
throw new CommandNotFoundException();
switch (command) {
case "exit":
bwu.isExitEntered = true;
break;
default:
throw new CommandNotFoundException();
}
}
}

/**
* This method reads any existing file and add the saved data
* into current programme
*
* @param list current operation AccountList
*/
public void parseSavedFile(AccountList list) throws IOException {
File f = new File("data/save.txt");
Scanner myReader = new Scanner(f);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
String[] splitDetails = data.split(";");
String name = splitDetails[0];
String balance = splitDetails[1];
list.addAccount(name, balance);
}
}
}
9 changes: 9 additions & 0 deletions src/test/java/seedu/bankwithus/StorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.jupiter.api.Test;

import java.util.Scanner;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

public class StorageTest {
Expand All @@ -11,4 +13,11 @@ public void load_normalSaveFile_noMessage() {
Storage storage = new Storage("data/save.txt");
assertDoesNotThrow(() -> storage.load());
}

@Test
public void save_accountList_noMessage() {
AccountList accountList = new AccountList();
Storage storage = new Storage("data/save.txt");
assertDoesNotThrow(() -> storage.saveToFile(accountList));
}
}

0 comments on commit 7100911

Please sign in to comment.