Skip to content

Commit

Permalink
Merge pull request #76 from ChongXern/master
Browse files Browse the repository at this point in the history
Create IncorrectCommandSyntaxException
  • Loading branch information
ChongXern committed Apr 4, 2024
2 parents a602006 + 32f0d97 commit 90b2ed7
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 26 deletions.
12 changes: 9 additions & 3 deletions src/main/java/command/AddInflowCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command;

import customexceptions.CategoryNotFoundException;
import customexceptions.IncorrectCommandSyntaxException;
import financialtransactions.Inflow;
import financialtransactions.TransactionManager;

Expand All @@ -10,20 +11,23 @@ public AddInflowCommand(String[] commandParts) {
super(false, commandParts);
try {
createInflow();
} catch (CategoryNotFoundException e) {
} catch (CategoryNotFoundException | IncorrectCommandSyntaxException e) {
System.out.println(e.getMessage());
}
}

private void createInflow() throws CategoryNotFoundException {
private void createInflow() throws CategoryNotFoundException, IncorrectCommandSyntaxException {
//@@author Kishen271828
String inflowName = null;
double inflowAmount = 0;
String inflowDate = null;
String inflowTime = null;
String inflowCategory = null;

for (String part : commandParts) {
/* Iterates through the parts of the original command string that checks and updates
relevant inflow information. */
for (int i = 1; i < commandParts.length; i++) {
String part = commandParts[i];
if (part.startsWith("n/")) {
inflowName = part.substring(2);
} else if (part.startsWith("a/")) {
Expand All @@ -34,6 +38,8 @@ private void createInflow() throws CategoryNotFoundException {
inflowTime = part.substring(2);
} else if (part.startsWith("c/")) {
inflowCategory = part.substring(2);
} else {
throw new IncorrectCommandSyntaxException(commandParts[0]);
}
}
String inflowDateTime = inflowDate + " " + inflowTime;
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/command/AddOutflowCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command;

import customexceptions.CategoryNotFoundException;
import customexceptions.IncorrectCommandSyntaxException;
import financialtransactions.Outflow;
import financialtransactions.TransactionManager;

Expand All @@ -10,20 +11,23 @@ public AddOutflowCommand(String[] commandParts) {
super(false, commandParts);
try {
createOutflow();
} catch (CategoryNotFoundException e) {
} catch (CategoryNotFoundException | IncorrectCommandSyntaxException e) {
System.out.println(e.getMessage());
}
}

private void createOutflow() throws CategoryNotFoundException {
private void createOutflow() throws CategoryNotFoundException, IncorrectCommandSyntaxException {
//@@author Kishen271828
String outflowName = null;
double outflowAmount = 0.0;
String outflowDate = null;
String outflowTime = null;
String outflowCategory = null;

for (String part : commandParts) {
/* Iterates through the parts of the original command string that checks and updates
relevant outflow information. */
for (int i = 1 ; i < commandParts.length; i++) {
String part = commandParts[i];
if (part.startsWith("n/")) {
outflowName = part.substring(2);
} else if (part.startsWith("a/")) {
Expand All @@ -34,6 +38,8 @@ private void createOutflow() throws CategoryNotFoundException {
outflowTime = part.substring(2);
} else if (part.startsWith("c/")) {
outflowCategory = part.substring(2);
} else {
throw new IncorrectCommandSyntaxException(commandParts[0]);
}
}
String outflowDateTime = outflowDate + " " + outflowTime;
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/command/AddReminderCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command;

import customexceptions.CategoryNotFoundException;
import customexceptions.IncorrectCommandSyntaxException;
import financialtransactions.Reminder;
import financialtransactions.TransactionManager;

Expand All @@ -9,19 +10,22 @@ public AddReminderCommand(String[] commandParts) {
super(false, commandParts);
try {
createReminder();
} catch (CategoryNotFoundException e) {
} catch (CategoryNotFoundException | IncorrectCommandSyntaxException e) {
System.out.println(e.getMessage());
}
}
private void createReminder() throws CategoryNotFoundException {

private void createReminder() throws CategoryNotFoundException, IncorrectCommandSyntaxException {
String reminderName = null;
double reminderAmount = 0.0;
String reminderDate = null;
String reminderTime = null;
String reminderCategory = null;

for (String part : commandParts) {
/* Iterates through the parts of the original command string that checks and updates
relevant reminder information. */
for (int i = 1; i < commandParts.length; i++) {
String part = commandParts[i];
if (part.startsWith("n/")) {
reminderName = part.substring(2);
} else if (part.startsWith("a/")) {
Expand All @@ -32,6 +36,8 @@ private void createReminder() throws CategoryNotFoundException {
reminderTime = part.substring(2);
} else if (part.startsWith("c/")) {
reminderCategory = part.substring(2);
} else {
throw new IncorrectCommandSyntaxException(commandParts[0]);
}
}
String reminderDateTime = reminderDate + " " + reminderTime;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/command/DeleteInflowCommand.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package command;

import customexceptions.IncorrectCommandSyntaxException;
import financialtransactions.TransactionManager;

public class DeleteInflowCommand extends BaseCommand {
private String inflowIndex = null;
public DeleteInflowCommand(String[] commandParts) {
super(false, commandParts);
}

public String execute(TransactionManager manager) throws Exception {
String inflowIndex = null;
if (commandParts[1].startsWith("i/")) {
inflowIndex = commandParts[1].substring(2);
} else {
throw new IncorrectCommandSyntaxException(commandParts[0]);
}
assert inflowIndex != null : "inflowIndex should not be null";
int inflowIndexParsed = Integer.parseInt(inflowIndex);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/command/DeleteOutflowCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package command;

import customexceptions.IncorrectCommandSyntaxException;
import financialtransactions.TransactionManager;

public class DeleteOutflowCommand extends BaseCommand {
Expand All @@ -11,6 +12,8 @@ public String execute(TransactionManager manager) throws Exception {
String outflowIndex = null;
if (commandParts[1].startsWith("i/")) {
outflowIndex = commandParts[1].substring(2);
} else {
throw new IncorrectCommandSyntaxException(commandParts[0]);
}
assert outflowIndex != null : "outflowIndex should not be null";
int outflowIndexParsed = Integer.parseInt(outflowIndex);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/command/DeleteReminderCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package command;

import customexceptions.IncorrectCommandSyntaxException;
import financialtransactions.TransactionManager;

public class DeleteReminderCommand extends BaseCommand {
Expand All @@ -9,10 +10,10 @@ public DeleteReminderCommand(String[] commandParts) {

public String execute(TransactionManager manager) throws Exception {
String reminderIndex = null;
for (String part : commandParts) {
if (part.startsWith("i/")) {
reminderIndex = part.substring(2);
}
if (commandParts[1].startsWith("i/")) {
reminderIndex = commandParts[1].substring(2);
} else {
throw new IncorrectCommandSyntaxException(commandParts[0]);
}
assert reminderIndex != null : "reminderIndex should not be null";
reminder = manager.removeReminder(Integer.parseInt(reminderIndex));
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/command/EditInflowCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package command;

import customexceptions.IncorrectCommandSyntaxException;
import financialtransactions.Inflow;
import financialtransactions.TransactionManager;

Expand All @@ -16,7 +17,10 @@ public String execute(TransactionManager manager) throws Exception {
String inflowTime = null;
String inflowCategory = null;

for (String part : commandParts) {
/* Iterates through the parts of the original command string that checks and updates
relevant inflow information. */
for (int i = 1; i < commandParts.length; i++) {
String part = commandParts[i];
if (part.startsWith("i/")) {
inflowIndex = Integer.parseInt(part.substring(2));
} else if (part.startsWith("n/")) {
Expand All @@ -29,6 +33,8 @@ public String execute(TransactionManager manager) throws Exception {
inflowTime = part.substring(2);
} else if (part.startsWith("c/")) {
inflowCategory = part.substring(2);
} else {
throw new IncorrectCommandSyntaxException(commandParts[0]);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/command/EditOutflowCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package command;

import customexceptions.IncorrectCommandSyntaxException;
import financialtransactions.Outflow;
import financialtransactions.TransactionManager;

Expand All @@ -16,7 +17,10 @@ public String execute(TransactionManager manager) throws Exception {
String outflowTime = null;
String outflowCategory = null;

for (String part : commandParts) {
/* Iterates through the parts of the original command string that checks and updates
relevant outflow information. */
for (int i = 1; i < commandParts.length; i++) {
String part = commandParts[i];
if (part.startsWith("i/")) {
outflowIndex = Integer.parseInt(part.substring(2));
} else if (part.startsWith("n/")) {
Expand All @@ -29,6 +33,8 @@ public String execute(TransactionManager manager) throws Exception {
outflowTime = part.substring(2);
} else if (part.startsWith("c/")) {
outflowCategory = part.substring(2);
} else {
throw new IncorrectCommandSyntaxException(commandParts[0]);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/command/EditReminderCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package command;

import customexceptions.IncorrectCommandSyntaxException;
import financialtransactions.Reminder;
import financialtransactions.TransactionManager;

Expand All @@ -16,7 +17,10 @@ public String execute(TransactionManager manager) throws Exception {
String reminderTime = null;
String reminderCategory = null;

for (String part : commandParts) {
/* Iterates through the parts of the original command string that checks and updates
relevant reminder information. */
for (int i = 1; i < commandParts.length; i++) {
String part = commandParts[i];
if (part.startsWith("i/")) {
reminderIndex = Integer.parseInt(part.substring(2));
} else if (part.startsWith("n/")) {
Expand All @@ -29,6 +33,8 @@ public String execute(TransactionManager manager) throws Exception {
reminderTime = part.substring(2);
} else if (part.startsWith("c/")) {
reminderCategory = part.substring(2);
} else {
throw new IncorrectCommandSyntaxException(commandParts[0]);
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/command/SetBudgetCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package command;

import customexceptions.IncorrectCommandSyntaxException;
import financialtransactions.TransactionManager;

public class SetBudgetCommand extends BaseCommand{
Expand All @@ -9,11 +10,11 @@ public SetBudgetCommand(String[] commandParts) {

public String execute(TransactionManager manager) throws Exception{
String budgetString = null;
for (String part : commandParts) {
if (part.startsWith("a/")) {
budgetString = part.substring(2);
}
}
if (commandParts[1].startsWith("a/")) {
budgetString = commandParts[1].substring(2);
} else {
throw new IncorrectCommandSyntaxException(commandParts[0]);
}
double budget = Double.parseDouble(budgetString);
manager.setBudget(budget);
return "Ok. Budget set.";
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/command/ViewHistoryCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package command;

import customexceptions.IncorrectCommandSyntaxException;
import financialtransactions.TransactionManager;

public class ViewHistoryCommand extends BaseCommand {
Expand All @@ -18,7 +19,7 @@ public String execute(TransactionManager manager) throws Exception{
} else if (commandParts[1].equals("all")) {
numTransactions = manager.getTransactionListSize();
} else {
return "Sorry, please use the correct syntax for view-history.";
throw new IncorrectCommandSyntaxException(commandParts[0]);
}
boolean isIncludeBarChart = commandParts.length == 3 && commandParts[2].equals("w/chart");
return manager.showLastNTransactions(numTransactions, isIncludeBarChart);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package customexceptions;

public class IncorrectCommandSyntaxException extends Exception {
public IncorrectCommandSyntaxException(String command) {
super("Sorry, please use the correct syntax for " + command);
}
}
3 changes: 2 additions & 1 deletion src/main/java/financeproject/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import customexceptions.InactivityTimeoutException;
import customexceptions.IncompletePromptException;
import customexceptions.UserNotFoundException;
import customexceptions.IncorrectCommandSyntaxException;
import financialtransactions.TransactionManager;
import parser.Parser;
import storage.Storage;
Expand Down Expand Up @@ -53,7 +54,7 @@ public static void main(String[] args) throws SecurityException {
response = baseCommand.execute(manager);
ui.printMessage(response);
inactivityTimer.resetTimer();
} catch (IncompletePromptException e) {
} catch (IncompletePromptException | IncorrectCommandSyntaxException e) {
ui.printMessage(e.getMessage());
} catch (Exception e) {
ui.printMessage("Uh-oh, something went wrong: " + e.getMessage());
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/financeproject/data/Bob.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
0.00
100.00
Stocks|400.00|Oct 24 2024 12:00PM|INVESTMENT|I
Refund|100.00|Jun 23 2023 05:00PM|REFUND|I
Salary|400.00|May 23 2022 07:00PM|INCOME|I
Salary|400.00|May 23 2022 07:00PM|INCOME|I
Groceries|-150.00|Jun 23 2023 06:30PM|SHOPPING|O
Rent|-1500.00|Jun 23 2023 06:00PM|RENT|O
Rent|-1500.00|Jun 23 2023 06:00PM|RENT|O

0 comments on commit 90b2ed7

Please sign in to comment.