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

Adjust exceptions for timeout and typo, and add taxes #40

Merged
merged 7 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/customexceptions/InactivityTimeoutException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package customexceptions;

public class InactivityTimeoutException extends Exception {
private boolean isTimeOut = false; //Returns true if system is past 3 min, false otherwise
private boolean isGracePeriod = false; //Returns true if system is past 2.5 min, false otherwise

public InactivityTimeoutException(boolean isTimeOut, boolean isGracePeriod) {
this.isTimeOut = isTimeOut;
this.isGracePeriod = isGracePeriod;
}

public boolean isTimeOut() {
return isTimeOut;
}
public boolean isGracePeriod() {
return isGracePeriod;
}
}
29 changes: 29 additions & 0 deletions src/main/java/customexceptions/IncompletePromptException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package customexceptions;

public class IncompletePromptException extends Exception {
public static final String[] INSTRUCTIONS = {"add-inflow", "add-outflow", "delete-inflow", "delete-outflow",
"login", "quit", "view-history"};
private boolean isTypo = false;
public IncompletePromptException(String line) {
int spaceIndex = line.indexOf(" ");
String firstWord = (spaceIndex == -1) ? line : line.substring(0, spaceIndex);
checkTypo(firstWord);
}
public boolean getIsTypo() {
return this.isTypo;
}
public void checkTypo(String word) {
for (String instruction : INSTRUCTIONS) {
if (!instruction.equals(word) && instruction.contains(word)) {
if (!isTypo) {
isTypo = true;
System.out.println("Did you mean: ");
}
System.out.print(instruction + " ");
}
}
if (isTypo) {
System.out.println("?");
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/customexceptions/SecurityException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package customexceptions;

public class SecurityException extends Exception {
}
40 changes: 39 additions & 1 deletion src/main/java/financeproject/Main.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package financeproject;

import java.lang.reflect.InaccessibleObjectException;

import customexceptions.InactivityTimeoutException;
import command.BaseCommand;
//import financialtransactions.Inflow;
//import financialtransactions.Outflow;
import customexceptions.IncompletePromptException;
import financialtransactions.TransactionManager;
import parser.Parser;
import storage.Storage;
import user.InactivityTimer;
//import user.Authentication;
//import user.BaseUser;
//import userinteraction.UI;
import user.Authentication;
import user.BaseUser;
import userinterface.UI;
//import userinterface.UI;

public class Main {
public static void main(String[] args) {
Expand All @@ -20,6 +28,36 @@ public static void main(String[] args) {
ui.printMessage("Welcome. In order to login, type your command in the format:\nlogin u/USERNAME p/PASSWORD");

Parser parser = new Parser();
InactivityTimer inactivityTimer = new InactivityTimer();
while (parser.getIsContinue()) {
try {
inactivityTimer.checkTimeElapsed();
} catch (InactivityTimeoutException e) {
if (e.isTimeOut()) {
parser.setIsContinue(false);
} else if (e.isGracePeriod()) {
ui.printMessage("Some time has passed. Would you still like to continue: ");
String wantToContinue = ui.readInput();
if (wantToContinue.equalsIgnoreCase("y") ||
wantToContinue.equalsIgnoreCase("yes")) {
inactivityTimer.resetTimer();
} else if (wantToContinue.equalsIgnoreCase("n") ||
wantToContinue.equalsIgnoreCase("no")) {
parser.setIsContinue(false);
}
}
}
String command = ui.readInput();
try {
parser.parseCommand(command, manager);
} catch (IncompletePromptException e) {
if (e.getIsTypo()) {
System.out.println("Sorry, your prompt appears incomplete. Could you finish your sentence?");
} else {
System.out.println("Sorry, unknown prompt detected.");
}
}
storage.saveFile(manager);
BaseCommand baseCommand = null;
String response = "";

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/financialtransactions/Outflow.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package financialtransactions;

public class Outflow extends Transaction<Outflow.Category> {
private static final double TAX_AMOUNT = 0.09;
public enum Category {
RENT, DEBT, SHOPPING, TREAT, EDUCATION, OTHER
}

public Outflow(String name, double amount, String date) {
super(name, -1.00 * amount, date);
super.transactionType = "O";
super.amount *= TAX_AMOUNT;
}

public void setCategory(Category category) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/financialtransactions/TransactionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public boolean addTransaction(Transaction<?> transaction) {
if (transaction instanceof Inflow) {
Inflow inflow = (Inflow) transaction;
return inflows.addTransaction(inflow);
} else if (transaction instanceof Outflow) {
}
if (transaction instanceof Outflow) {
Outflow outflow = (Outflow) transaction;
return outflows.addTransaction(outflow);
}
Expand Down
40 changes: 37 additions & 3 deletions src/main/java/parser/Parser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package parser;

import customexceptions.IncompletePromptException;
import financialtransactions.Inflow;
import financialtransactions.Outflow;
//import userinteraction.UI;
import command.AddInflowCommand;
import command.AddOutflowCommand;
import command.BaseCommand;
Expand All @@ -9,22 +13,49 @@
import userinterface.UI;

public class Parser {
public boolean isContinue;
UI ui;

public Parser() {
this.ui = new UI();
}

public BaseCommand parseCommand(String command, TransactionManager manager) throws Exception {
public BaseCommand parseCommand(String command, TransactionManager manager) throws
IncompletePromptException, Exception {
String[] commandParts = command.split("\\s+");
String action = commandParts[0];
switch (action) {
case "add-inflow":
if (commandParts.length < 3) {
throw new IncompletePromptException(command);
}
return new AddInflowCommand(commandParts);
/*String inflowName = commandParts[1];
double inflowAmount = Double.parseDouble(commandParts[2]);
String inflowDate = commandParts[3] + " " + commandParts[4];

Inflow inflow = new Inflow(inflowName, inflowAmount, inflowDate);
manager.addTransaction(inflow);
ui.printMessage("Ok. Added inflow");*/
//return "Ok. Added inflow";*/
case "add-outflow":
if (commandParts.length < 3) {
throw new IncompletePromptException(command);
}
return new AddOutflowCommand(commandParts);
/*String outflowName = commandParts[1];
double outflowAmount = Double.parseDouble(commandParts[2]);
String outflowDate = commandParts[3] + " " + commandParts[4];

Outflow outflow = new Outflow(outflowName, outflowAmount, outflowDate);
manager.addTransaction(outflow);
ui.printMessage("Ok. Added outflow");*/
//return "Ok. Added outflow";
case "delete-inflow":
case "delete-outflow":
if (commandParts.length < 2) {
throw new IncompletePromptException(command);
}
String index = commandParts[1];
manager.removeTransaction(Integer.parseInt(index));
break;
Expand All @@ -33,9 +64,12 @@ public BaseCommand parseCommand(String command, TransactionManager manager) thro
case "quit":
return new ExitCommand(commandParts);
default:
System.out.println("Invalid command");
break;
throw new IncompletePromptException(command);
}
throw new Exception("Error parsing");
}

public void setIsContinue(boolean isContinue) {
this.isContinue = isContinue;
}
}
27 changes: 27 additions & 0 deletions src/main/java/user/InactivityTimer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package user;

import customexceptions.InactivityTimeoutException;

public class InactivityTimer {
public static final int INACTIVITY_TIME = 180_000;
public static final int GRACE_TIME = 30_000;
private long startTime;

public InactivityTimer() {
startTime = System.currentTimeMillis();
}

public void resetTimer() {
startTime = System.currentTimeMillis();
}

public void checkTimeElapsed() throws InactivityTimeoutException {
long timeDifference = System.currentTimeMillis() - startTime;
if (timeDifference >= INACTIVITY_TIME) {
throw new InactivityTimeoutException(true, false);
}
else if (timeDifference >= GRACE_TIME) {
throw new InactivityTimeoutException(false, true);
}
}
}
25 changes: 25 additions & 0 deletions src/test/java/parser/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package parser;

import customexceptions.IncompletePromptException;
import financialtransactions.TransactionManager;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
//import static org.junit.jupiter.api.Assertions.fail;

public class ParserTest {
TransactionManager manager = new TransactionManager();

@Test
public void addInflow_success() throws IncompletePromptException {
Parser test1 = new Parser();
assertEquals("Ok. Added inflow", test1.parseCommand("add-inflow Salary 5000 14/03/2024 1700", manager));
assertEquals("Ok. Added inflow", test1.parseCommand("add-inflow Investment 600 03/03/2024 1900", manager));
}

public void addOutflow_success() throws IncompletePromptException {
Parser test1 = new Parser();
assertEquals("Ok. Added outflow", test1.parseCommand("add-outflow Rent 1000 18/02/2024 1100", manager));
assertEquals("Ok. Added outflow", test1.parseCommand("add-outflow Shopping 150 30/01/2024 1430", manager));
}

}
Loading