forked from nus-cs2113-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from ChongXern/master
Adjust exceptions for timeout and typo, and add taxes
- Loading branch information
Showing
9 changed files
with
184 additions
and
5 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/main/java/customexceptions/InactivityTimeoutException.java
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
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
29
src/main/java/customexceptions/IncompletePromptException.java
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
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("?"); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package customexceptions; | ||
|
||
public class SecurityException extends Exception { | ||
} |
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
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
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
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
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
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); | ||
} | ||
} | ||
} |
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
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)); | ||
} | ||
|
||
} |