Skip to content

Commit

Permalink
Add ability to add expense
Browse files Browse the repository at this point in the history
  • Loading branch information
lckjosh committed Oct 17, 2023
1 parent 2063ea7 commit f7f76be
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/nuscents/commands/ListOfCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/seedu/nuscents/data/Expense.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
28 changes: 28 additions & 0 deletions src/main/java/seedu/nuscents/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -54,6 +58,8 @@ public static <TaskList> 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:
Expand Down Expand Up @@ -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) {
Expand Down
48 changes: 39 additions & 9 deletions src/main/java/seedu/nuscents/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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.
*/
Expand All @@ -36,7 +38,8 @@ public ArrayList<Transaction> 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.
*/
Expand All @@ -46,19 +49,36 @@ private static void transactionDecoder(File file, ArrayList<Transaction> 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;
}
Expand All @@ -68,6 +88,7 @@ private static void transactionDecoder(File file, ArrayList<Transaction> 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.
*/
Expand All @@ -76,7 +97,6 @@ public void writeToFile(TransactionList transactionList) throws IOException {
FileWriter fw = new FileWriter(file);
ArrayList<Transaction> transactions = transactionList.getTransactions();
for (Transaction transaction : transactions) {
// int markedIndex = encodeTaskStatus(transaction.getTaskStatus());
String output = toString(transaction);
fw.write(output);
fw.write("\n");
Expand All @@ -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
*/
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/nuscents/ui/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down

0 comments on commit f7f76be

Please sign in to comment.