diff --git a/src/main/java/seedu/nuscents/commands/ListOfCommands.java b/src/main/java/seedu/nuscents/commands/ListOfCommands.java index 6d5b0ab125..0f870e6587 100644 --- a/src/main/java/seedu/nuscents/commands/ListOfCommands.java +++ b/src/main/java/seedu/nuscents/commands/ListOfCommands.java @@ -7,7 +7,7 @@ public class ListOfCommands { public static final String COMMAND_EXIT = "exit"; public static final String COMMAND_LIST = "list"; public static final String COMMAND_ALLOWANCE = "allowance"; - public static final String COMMAND_DEADLINE = "deadline"; + public static final String COMMAND_EXPENSE = "expense"; public static final String COMMAND_EVENT = "event"; public static final String COMMAND_DELETE = "delete"; public static final String COMMAND_FIND = "find"; diff --git a/src/main/java/seedu/nuscents/data/Expense.java b/src/main/java/seedu/nuscents/data/Expense.java index 910d106844..a18b7d52ac 100644 --- a/src/main/java/seedu/nuscents/data/Expense.java +++ b/src/main/java/seedu/nuscents/data/Expense.java @@ -1,7 +1,9 @@ package seedu.nuscents.data; +import java.time.LocalDateTime; + public class Expense extends Transaction { - public Expense(String description) { - super(description); + public Expense(String amount, LocalDateTime date, String description, String additionalInfo) { + super(amount, date, description, additionalInfo); } } diff --git a/src/main/java/seedu/nuscents/parser/Parser.java b/src/main/java/seedu/nuscents/parser/Parser.java index 4ac1203bbd..e115f47ad7 100644 --- a/src/main/java/seedu/nuscents/parser/Parser.java +++ b/src/main/java/seedu/nuscents/parser/Parser.java @@ -12,6 +12,7 @@ import seedu.nuscents.data.Transaction; import seedu.nuscents.data.Allowance; +import seedu.nuscents.data.Expense; import seedu.nuscents.data.exception.NuscentsException; import java.time.LocalDateTime; @@ -20,10 +21,13 @@ import static seedu.nuscents.commands.ListOfCommands.COMMAND_EXIT; import static seedu.nuscents.commands.ListOfCommands.COMMAND_LIST; import static seedu.nuscents.commands.ListOfCommands.COMMAND_ALLOWANCE; +import static seedu.nuscents.commands.ListOfCommands.COMMAND_EXPENSE; import static seedu.nuscents.commands.ListOfCommands.COMMAND_DELETE; import static seedu.nuscents.commands.ListOfCommands.COMMAND_FIND; import static seedu.nuscents.commands.ListOfCommands.COMMAND_HELP; + import static seedu.nuscents.ui.Messages.MESSAGE_EMPTY_ALLOWANCE; +import static seedu.nuscents.ui.Messages.MESSAGE_EMPTY_EXPENSE; import static seedu.nuscents.ui.Messages.MESSAGE_EMPTY_INDEX; import static seedu.nuscents.ui.Messages.MESSAGE_EMPTY_KEYWORD; import static seedu.nuscents.ui.Messages.MESSAGE_INVALID_DATE; @@ -54,6 +58,8 @@ public static Command parseCommand(String text, TaskList tasks) throw return new ListCommand(); case COMMAND_ALLOWANCE: return new AddCommand(parseAllowance(arguments)); + case COMMAND_EXPENSE: + return new AddCommand(parseExpense(arguments)); case COMMAND_DELETE: return new DeleteCommand(parseTaskIndex(arguments)); case COMMAND_FIND: @@ -115,6 +121,28 @@ public static Allowance parseAllowance(String arguments) throws NuscentsExceptio } } + /** + * Parsers arguments in the context of adding an expense. + * + * @param arguments full command argument string + * @return a {@link Expense} object + * @throws NuscentsException If the description of the allowance is empty. + */ + public static Expense parseExpense(String arguments) throws NuscentsException { + if (arguments == null) { + throw new NuscentsException(MESSAGE_EMPTY_EXPENSE); + } else { + String amount = extractValue(arguments, AMT_PATTERN, false); + String date = extractValue(arguments, DATE_PATTERN, false); + String description = extractValue(arguments, DESC_PATTERN, false); + String additionalInformation = extractValue(arguments, NOTE_PATTERN, true); + String format = dateTimePatternValidation(date); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); + LocalDateTime formattedDate = parseDate(date, format, formatter); + return new Expense(amount, formattedDate, description, additionalInformation); + } + } + public static int parseTaskIndex(String arguments) throws IndexOutOfBoundsException, NuscentsException { if (arguments == null) { diff --git a/src/main/java/seedu/nuscents/storage/Storage.java b/src/main/java/seedu/nuscents/storage/Storage.java index 1621cee4d6..9a15b4a3d8 100644 --- a/src/main/java/seedu/nuscents/storage/Storage.java +++ b/src/main/java/seedu/nuscents/storage/Storage.java @@ -1,6 +1,7 @@ package seedu.nuscents.storage; import seedu.nuscents.data.Allowance; +import seedu.nuscents.data.Expense; import seedu.nuscents.data.Transaction; import seedu.nuscents.data.TransactionList; @@ -18,12 +19,13 @@ public class Storage { private String filePath; - public Storage (String filePath) { + public Storage(String filePath) { this.filePath = filePath; } /** * Reads data from the storage file and return it. + * * @return an arraylist of tasks * @throws FileNotFoundException If the storage file does not exist. */ @@ -36,7 +38,8 @@ public ArrayList readDataFromFile() throws FileNotFoundException { /** * Decodes the storage data file and store it into the arraylist of tasks. - * @param file storage data file + * + * @param file storage data file * @param transactions arraylist of tasks * @throws FileNotFoundException If the storage data file does not exist. */ @@ -46,19 +49,36 @@ private static void transactionDecoder(File file, ArrayList transac while (data.hasNext()) { String transactionDetails = data.nextLine(); char transactionType = transactionDetails.charAt(0); + String[] columns; + String amount = ""; + LocalDateTime date; + String description = ""; + String note = ""; switch (transactionType) { case 'A': - String[] columns = transactionDetails.split("\\s*\\|\\s*"); - String amount = columns[1]; - LocalDateTime date = LocalDateTime.parse(columns[2]); - String description = columns[3]; - String note = ""; + columns = transactionDetails.split("\\s*\\|\\s*"); + amount = columns[1]; + date = LocalDateTime.parse(columns[2]); + description = columns[3]; + note = ""; if (columns.length > 4) { note = columns[4]; } transactions.add(new Allowance(amount, date, description, note)); break; + case 'E': + columns = transactionDetails.split("\\s*\\|\\s*"); + amount = columns[1]; + date = LocalDateTime.parse(columns[2]); + description = columns[3]; + note = ""; + if (columns.length > 4) { + note = columns[4]; + } + transactions.add(new Expense(amount, date, description, note)); + break; + default: break; } @@ -68,6 +88,7 @@ private static void transactionDecoder(File file, ArrayList transac /** * Writes the data to the storage file. * Creates a new file if the file does not exist. + * * @param transactionList list of tasks * @throws IOException If there were errors converting and/or storing the data to the file. */ @@ -76,7 +97,6 @@ public void writeToFile(TransactionList transactionList) throws IOException { FileWriter fw = new FileWriter(file); ArrayList transactions = transactionList.getTransactions(); for (Transaction transaction : transactions) { - // int markedIndex = encodeTaskStatus(transaction.getTaskStatus()); String output = toString(transaction); fw.write(output); fw.write("\n"); @@ -87,6 +107,7 @@ public void writeToFile(TransactionList transactionList) throws IOException { /** * Encode task status to integers to be stored in the storage data file. * A task marked as done is stored as 1, while a task marked as not done is stored as 0. + * * @param isMarked boolean to indicate if the task is marked as done * @return integer indicator of the task status */ @@ -100,6 +121,7 @@ private static int encodeTaskStatus(boolean isMarked) { /** * Converts the task details to a String to be stored in the storage data file. + * * @param transaction task being converted to String * @param markedIndex integer indicator of the task status * @return a String object to be stored in the storage data file @@ -108,9 +130,17 @@ private static String toString(Transaction transaction) { if (transaction instanceof Allowance) { return "A" + " | " + transaction.getAmount() + " | " - + transaction.getDate() + " | " + + transaction.getDate() + " | " + transaction.getDescription() + " | " + transaction.getAdditionalInfo(); + + } else if (transaction instanceof Expense) { + return "E" + " | " + + transaction.getAmount() + " | " + + transaction.getDate() + " | " + + transaction.getDescription() + " | " + + transaction.getAdditionalInfo(); + } else { return null; } diff --git a/src/main/java/seedu/nuscents/ui/Messages.java b/src/main/java/seedu/nuscents/ui/Messages.java index 8f31f99c2d..e34bba54ae 100644 --- a/src/main/java/seedu/nuscents/ui/Messages.java +++ b/src/main/java/seedu/nuscents/ui/Messages.java @@ -14,6 +14,7 @@ public class Messages { public static final String MESSAGE_UNMARK = "OK, I've marked this task as not done yet:"; public static final String MESSAGE_EMPTY_TODO = "OOPS!!! The description of a todo cannot be empty."; public static final String MESSAGE_EMPTY_ALLOWANCE = "OOPS!!! Invalid input format for adding an allowance."; + public static final String MESSAGE_EMPTY_EXPENSE = "OOPS!!! Invalid input format for adding an expense."; public static final String MESSAGE_EMPTY_BY = "OOPS!!! The /by parameter of a deadline cannot be empty."; public static final String MESSAGE_EMPTY_EVENT = "OOPS!!! The description of an event cannot be empty."; public static final String MESSAGE_EMPTY_FROM = "OOPS!!! The /from parameter of an event cannot be empty.";