Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY1819S1#57 from CS2103-AY1819S1-W12-2/…
Browse files Browse the repository at this point in the history
…shaw_yeong_changes

Update AnalyticsCommand, FinancialDatabaseParser and Deadline classes
  • Loading branch information
Aadit Kamat authored Oct 23, 2018
2 parents b4e504c + bd7bd9f commit 711463c
Show file tree
Hide file tree
Showing 11 changed files with 18,447 additions and 46 deletions.
18,315 changes: 18,315 additions & 0 deletions sResources gradlew processResources

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package seedu.address.commons.core;

/**
* Container for user visible messages.
* Represents error messages displayed to the user
*/
public class Messages {

public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_TRANSACTION_DISPLAYED_INDEX =
"The transaction index provided is invalid";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The transaction index provided is invalid";
public static final String MESSAGE_TRANSACTIONS_LISTED_OVERVIEW = "%1$d transactions listed!";
public static final String MESSAGE_INVALID_DATE =
"The transaction deadline must be a valid date in the future in the DD/MM/YYYY format";
}
41 changes: 29 additions & 12 deletions src/main/java/seedu/address/logic/commands/AnalyticsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.transaction.Amount;
import seedu.address.model.transaction.Deadline;
import seedu.address.model.transaction.Transaction;

/**
Expand All @@ -18,32 +21,46 @@ public class AnalyticsCommand extends Command {
public static final String COMMAND_ALIAS = "an";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Analyse the your financial status and generate \n"
+ "your financial status to view.\n";
+ "your financial status to view.\n"
+ "It will either generate your financial status base on all the list,\n"
+ "or generate to a certain date, base on the date you input."
+ "eg an or an dd/mm/yyyy";

public static final String MESSAGE_SUCCESS = "Financial status : SGD";
public static final String MESSAGE_SUCCESS = "Financial status : SGD %.2f";

private final Deadline deadline;

public AnalyticsCommand() { this.deadline = null; }
public AnalyticsCommand(Deadline deadline) {
this.deadline = deadline;
}


@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
double amount;
amount = 0;
amount = 0.0;
requireNonNull(model);

//List<Person> personList = model.getFilteredPersonList();
List<Transaction> transactionList = model.getFilteredTransactionList();


if (deadline != null && !Deadline.isValidDeadline(deadline.toString())) {
throw new CommandException(Messages.MESSAGE_INVALID_DATE);
}

for (int i = 0; i < transactionList.size(); i++) {
Transaction t = transactionList.get(i);

if (t.getType().toString().compareTo("Debt") == 0) {
amount += Double.parseDouble(t.getAmount().getValue());
} else {
amount -= Double.parseDouble(t.getAmount().getValue());
if (deadline == null || deadline.compareTo(t.getDeadline()) == 1) {
Amount currentAmount = Amount.convertCurrency(t.getAmount());
if (currentAmount != null) {
if (t.getType().toString().compareTo("debt") == 0) {
amount -= Double.parseDouble(currentAmount.getValue().split(" ")[1]);
} else if ((t.getType().toString().compareTo("loan") == 0)) {
amount += Double.parseDouble(currentAmount.getValue().split(" ")[1]);
}
}
}
}


return new CommandResult(String.format(MESSAGE_SUCCESS, amount));
}
}
Expand Down
30 changes: 2 additions & 28 deletions src/main/java/seedu/address/logic/commands/ConvertCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,10 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
requireNonNull(model);
StringBuilder convertedAmounts = new StringBuilder();
for (int i = 0; i < amounts.size(); i += 2) {
convertedAmounts.append(convertCurrency(amounts.get(i) + " " + amounts.get(i + 1)));
Amount currentAmount = new Amount(amounts.get(i) + " " + amounts.get(i + 1));
convertedAmounts.append(Amount.convertCurrency(currentAmount));
}
return new CommandResult(String.format(MESSAGE_SUCCESS, convertedAmounts));
}

/**
* Handles the conversion of foreign currency to Singaporean currency.
*
* @param amount the amount in a given currency which is to be converted to Singaporean currency
*/
private static String convertCurrency(String amount) {
if (!Amount.isValidAmount(amount)) {
return "";
}
String currencyCode = amount.split(" ")[0].toUpperCase();
String currencyConverterFilePath = String.format(
"http://free.currencyconverterapi.com/api/v5/convert?q=%s_SGD&compact=y", currencyCode);
ObjectMapper mapper = new ObjectMapper();
try {
InputStream is = new URL(currencyConverterFilePath).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = rd.readLine();
Map<String, Map<String, Double>> map = mapper.readValue(jsonText, Map.class);
double result = map.get(String.format("%s_SGD", currencyCode)).get("val");
result *= Double.parseDouble(amount.split(" ")[1]);
return String.format("SGD %.2f ", result);
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* @@author xiaoyeong */
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.logic.commands.AnalyticsCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.transaction.Deadline;

/**
* Parses input arguments and creates a new AnalyticsCommand object
*/
public class AnalyticsCommandParser implements Parser<AnalyticsCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AnalyticsCommand
* and returns an AnalyticsCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AnalyticsCommand parse(String args) throws ParseException {
try {
if (args != null && args.equals("")) {
return new AnalyticsCommand();
}
Deadline deadline = ParserUtil.parseDeadline(args);
return new AnalyticsCommand(deadline);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AnalyticsCommand.MESSAGE_USAGE), pe);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Command parseCommand(String userInput) throws ParseException {

case AnalyticsCommand.COMMAND_WORD:
case AnalyticsCommand.COMMAND_ALIAS:
return new AnalyticsCommand();
return new AnalyticsCommandParser().parse(arguments);

case CalendarCommand.COMMAND_WORD:
case CalendarCommand.COMMAND_ALIAS:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static Type parseType(String type) throws ParseException {
public static Deadline parseDeadline(String deadline) throws ParseException {
requireNonNull(deadline);
String trimmedDeadline = deadline.trim();
if (deadline.isEmpty()) {
if (deadline.isEmpty() || !Deadline.isValidDeadline(trimmedDeadline)) {
throw new ParseException(UniqueId.MESSAGE_TRANSACTION_PERSONUID_CONSTRAINTS);
}
return new Deadline(trimmedDeadline);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/model/person/Photo.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ public static boolean checkPicture(String path) {
} catch (IOException error) {
return false;
}

if (pictureNew.length() > TENMB) {
return false;
}

return true;
}

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/seedu/address/model/transaction/Amount.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package seedu.address.model.transaction;

import com.fasterxml.jackson.databind.ObjectMapper;
import seedu.address.commons.core.LogsCenter;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Currency;
import java.util.Map;
import java.util.Set;


Expand Down Expand Up @@ -58,6 +67,34 @@ private static boolean checkCurrency(String test) {
return false;
}

/**
* Handles the conversion of foreign currency to Singaporean currency.
*
* @param amount the amount in a given currency which is to be converted to Singaporean currency
*/
public static Amount convertCurrency(Amount amount) {
String amountValue = amount.value;
if (!Amount.isValidAmount(amountValue)) {
return null;
}
String currencyCode = amountValue.split(" ")[0].toUpperCase();
String currencyConverterFilePath = String.format(
"http://free.currencyconverterapi.com/api/v5/convert?q=%s_SGD&compact=y", currencyCode);
ObjectMapper mapper = new ObjectMapper();
try {
InputStream is = new URL(currencyConverterFilePath).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = rd.readLine();
Map<String, Map<String, Number>> map = mapper.readValue(jsonText, Map.class);
double result = 1.00 * map.get(String.format("%s_SGD", currencyCode)).get("val").doubleValue();
result *= Double.parseDouble(amountValue.split(" ")[1]);

return new Amount(String.format("SGD %.2f", result));
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
@Override
public String toString() {
return value;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/seedu/address/model/transaction/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.util.Date;


import seedu.address.commons.core.LogsCenter;

Expand Down Expand Up @@ -75,4 +79,22 @@ public boolean equals(Object other) {
public int hashCode() {
return value.hashCode();
}

/**
* Compare two deadline and check which deadline is nearer
* @param other the other deadline to compare.
*/
public int compareTo(Deadline other) {
try {
Date dateOne = new SimpleDateFormat("dd/MM/yyyy").parse(this.toString());
Date dateTwo = new SimpleDateFormat("dd/MM/yyyy").parse(other.toString());
if (dateOne.compareTo(dateTwo) > -1) {
return 1;
}
return -1;
} catch (ParseException e) {
return 2;
}
}

}
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/transaction/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Type {
public Type(String type) {
requireNonNull(type);
checkArgument(isValidType(type), MESSAGE_TRANSACTION_TYPE_CONSTRAINTS);
value = type;
value = type.toLowerCase();
}


Expand All @@ -45,7 +45,7 @@ public boolean equals(Object other) {
return false;
}
Type type = (Type) other;
return other == this || value.equals(type.value);
return other == this || value.equalsIgnoreCase(type.value);
}

@Override
Expand Down

0 comments on commit 711463c

Please sign in to comment.