Skip to content

Commit

Permalink
Merge pull request #150 from AY2122S1-CS2103-F10-4/sherwin-refactor-c…
Browse files Browse the repository at this point in the history
…ommand-messages-sort-commission

Sherwin refactor sort policy comparators, commission for download and command messages
  • Loading branch information
zhuoyang125 authored Nov 6, 2021
2 parents fb6068b + 14f65c0 commit 8423a8d
Show file tree
Hide file tree
Showing 54 changed files with 273 additions and 190 deletions.
32 changes: 16 additions & 16 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Format: `clearpolicy CONTACT_INDEX`
Finds and lists all contacts in address book whose name contains any of the argument keywords.

Format: `findcontact KEYWORD`
* Keyword matching is case insensitive.
* Keyword matching is case-insensitive.

Example:
* `findcontact john` returns a list with all the contacts containing the name john.
Expand All @@ -214,37 +214,37 @@ Format: `expiringpolicy`

#### Sorting contacts : `sortcontact`

Sorts the contact list alphabetically by the order specified.
Sorts the contact list alphabetically by the order specified. Case-insensitive.

Format: `sortcontact SORTER`

These are the current sorters implemented:
* `asc`: Sorts the contacts in ascending order based on the saved name
* `dsc`: Sorts the contacts in descending order based on the saved name
* `asc`: Sorts the contacts in ascending order alphabetically based on the saved name
* `dsc`: Sorts the contacts in descending order alphabetically based on the saved name

#### Sorting policies: `sortpolicy`

Sorts the policy list alphabetically by the order specified.
Sorts the policy list using the sorter specified.

Format: `sortpolicy SORTER`

These are the current sorts implemented:
* `titleasc`: Sorts the policies in ascending order based on the saved title
* `titledsc`: Sorts the policies in descending order based on the saved title
* `priceasc`: Sorts the policies in ascending order based on the saved payment
* `pricedsc`: Sorts the policies in descending order based on the saved payment
* `commasc`: Sorts the policies in ascending order based on the saved commission
* `commdsc`: Sorts the policies in descending order based on the saved commission
* `dateasc`: Sorts the policies in ascending order based on the saved expiry date
* `datedsc`: Sorts the policies in descending order based on the saved expiry date
These are the current sorters implemented:
* `titleasc`: Sorts the policies in ascending order alphabetically based on the saved title (Case-insensitive)
* `titledsc`: Sorts the policies in descending order alphabetically based on the saved title (Case-insensitive)
* `paymentasc`: Sorts the policies in ascending order based on the total payment amount (indefinite payments count as infinite)
* `paymentdsc`: Sorts the policies in descending order based on the total payment amount (indefinite payments count as infinite)
* `commasc`: Sorts the policies in ascending order based on the total commission amount
* `commdsc`: Sorts the policies in descending order based on the total commission amount
* `expiryasc`: Sorts the policies in ascending order based on the saved expiry date, if it exists
* `expirydsc`: Sorts the policies in descending order based on the saved expiry date, if it exists

### Statistics

#### Download useful statistics as CSV : `download`

Download a CSV file containing useful statistics for the user. This includes
- Most valuable contacts + commission from them
- Number of policies per contact
- Most valuable contacts + total commission from each of them
- Number of policies for each contact
- Average number of policies per contact
- Total commission

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/seedu/siasa/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public void init() throws Exception {
}

/**
* Returns a {@code ModelManager} with the data from {@code storage}'s address book and {@code userPrefs}. <br>
* The data from the sample address book will be used instead if {@code storage}'s address book is not found,
* or an empty address book will be used instead if errors occur when reading {@code storage}'s address book.
* Returns a {@code ModelManager} with the data from {@code storage}'s SIASA and {@code userPrefs}. <br>
* The data from the sample SIASA will be used instead if {@code storage}'s SIASA is not found,
* or an empty SIASA will be used instead if errors occur when reading {@code storage}'s SIASA.
*/
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
Optional<ReadOnlySiasa> siasaOptional;
Expand Down Expand Up @@ -173,7 +173,7 @@ public void start(Stage primaryStage) {

@Override
public void stop() {
logger.info("============================ [ Stopping Address Book ] =============================");
logger.info("============================ [ Stopping SIASA ] =============================");
try {
storage.saveUserPrefs(model.getUserPrefs());
} catch (IOException e) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/siasa/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class Messages {
public static final String MESSAGE_INVALID_CONTACT_DISPLAYED_INDEX = "The contact index provided is invalid";
public static final String MESSAGE_INVALID_POLICY_DISPLAYED_INDEX = "The policy index provided is invalid";
public static final String MESSAGE_CONTACTS_LISTED_OVERVIEW = "%1$d contacts listed!";
public static final String MESSAGE_POLICIES_LISTED_OVERVIEW = "%1$d policies listed!";
public static final String MESSAGE_CONTACTS_LIST_EMPTY = "There are no contacts to show";
public static final String MESSAGE_POLICIES_LIST_EMPTY = "There are no policies to show";
public static final String MESSAGE_CANCELLED_COMMAND = "Command cancelled";
public static final String MESSAGE_INVALID_COMMISSION_NUM_OF_PMT =
"The number of payments with receivable commission should not be "
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/seedu/siasa/commons/util/CurrencyUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package seedu.siasa.commons.util;

public class CurrencyUtil {
/**
* Converts money in cents to dollars in string format.
* @param moneyInCents Money in cents.
* @return Dollars in string.
*/
public static String centsToDollars(int moneyInCents) {
int cents = moneyInCents % 100;
int dollars = (moneyInCents - cents) / 100;

String centsStr;
if (cents <= 9) {
centsStr = 0 + "" + cents;
} else {
centsStr = Integer.toString(cents);
}

return "$" + dollars + "." + centsStr;
}
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/siasa/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface Logic {
ObservableList<Policy> getFilteredPolicyList();

/**
* Returns the user prefs' address book file path.
* Returns the user prefs' SIASA file path.
*/
Path getAddressBookFilePath();

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/seedu/siasa/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

import static java.util.Objects.requireNonNull;

import seedu.siasa.commons.core.Messages;
import seedu.siasa.model.Model;
import seedu.siasa.model.Siasa;

/**
* Clears the address book.
* Clears the SIASA.
*/
public class ClearCommand extends Command {

public static final String COMMAND_WORD = "clear";
public static final String MESSAGE_SUCCESS = "SIASA has been cleared!";

public static final String MESSAGE_CONFIRMATION = "Are you sure you wish to clear all contacts and policies?";

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
boolean response = Warning.isUserConfirmingCommand(MESSAGE_CONFIRMATION);
if (!response) {
return new CommandResult(Messages.MESSAGE_CANCELLED_COMMAND);
}
model.setSiasa(new Siasa());
return new CommandResult(MESSAGE_SUCCESS);
}
Expand Down
33 changes: 14 additions & 19 deletions src/main/java/seedu/siasa/logic/commands/DownloadCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.siasa.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.siasa.commons.util.CurrencyUtil.centsToDollars;

import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -68,23 +69,32 @@ private List<String> stringListBuilderForTxt(
stringList.add("Statistics for " + CURRENT_DATE + "\n");
stringList.add("Most premium contacts:\n" + TITLE_UNDERLINE);
commissionPerContact.forEach((contact, commission) -> {
stringList.add(contact + "; Commission: " + centsToDollars(commission));
stringList.add(contact + "; Commission: " + commissionToDollarsStr(commission));
});
stringList.add("\n");

stringList.add("Number of policies per contact:\n" + TITLE_UNDERLINE);
numberPoliciesPerContact.forEach((contact, noPolicies) -> {
stringList.add(contact + "; Number of Policies: " + noPolicies);
numberPoliciesPerContact.forEach((contact, policyCount) -> {
stringList.add(contact + "; Number of Policies: " + policyCount);
});
stringList.add("\n");

stringList.add("Average number of policies per contact: "
+ String.format("%.2f", getAvgPoliciesPerContact(numberPoliciesPerContact)));

stringList.add("Total Commission: " + centsToDollars(totalCommission));
stringList.add("Total Commission: " + commissionToDollarsStr(totalCommission));
return stringList;
}

private String commissionToDollarsStr(int commission) {
boolean isMaxInt = commission == Integer.MAX_VALUE;
if (isMaxInt) {
return centsToDollars(commission) + " (Max Value reached)";
} else {
return centsToDollars(commission);
}
}

private void writeToTxt(List<String> stringList) throws IOException {
Path pathToFile = Paths.get(".", TXT_FILEPATH);
FileUtil.createIfMissing(pathToFile);
Expand All @@ -94,19 +104,4 @@ private void writeToTxt(List<String> stringList) throws IOException {
}
FileUtil.writeToFile(pathToFile, stats.toString());
}

private String centsToDollars(int priceInCents) {
int cents = priceInCents % 100;
int dollars = (priceInCents - cents) / 100;

String centsStr;
if (cents <= 9) {
centsStr = 0 + "" + cents;
} else {
centsStr = Integer.toString(cents);
}

return "$" + dollars + "." + centsStr;
}

}
2 changes: 1 addition & 1 deletion src/main/java/seedu/siasa/logic/commands/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ExitCommand extends Command {

public static final String COMMAND_WORD = "exit";

public static final String MESSAGE_EXIT_ACKNOWLEDGEMENT = "Exiting Address Book as requested ...";
public static final String MESSAGE_EXIT_ACKNOWLEDGEMENT = "Exiting SIASA as requested ...";

@Override
public CommandResult execute(Model model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import seedu.siasa.model.contact.Contact;

/**
* Adds a contact to the address book.
* Adds a contact to the SIASA.
*/
public class AddContactCommand extends Command {

Expand All @@ -38,8 +38,8 @@ public class AddContactCommand extends Command {
+ PREFIX_TAG + "owesMoney";

public static final String MESSAGE_SUCCESS = "New contact added: %1$s";
public static final String MESSAGE_DUPLICATE_CONTACT = "This contact already exists in the address book";
public static final String MESSAGE_SIMILAR_CONTACT = "A similar contact: %1$s already exists in the address book.";
public static final String MESSAGE_DUPLICATE_CONTACT = "This contact already exists in the SIASA";
public static final String MESSAGE_SIMILAR_CONTACT = "A similar contact: %1$s already exists in the SIASA";

private final Contact toAdd;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import seedu.siasa.model.contact.Contact;

/**
* Deletes a contact identified using it's displayed index from the address book.
* Deletes a contact identified using it's displayed index from the SIASA.
*/
public class DeleteContactCommand extends Command {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import seedu.siasa.model.tag.Tag;

/**
* Edits the details of an existing contact in the address book.
* Edits the details of an existing contact in the SIASA.
*/
public class EditContactCommand extends Command {

Expand All @@ -49,8 +49,8 @@ public class EditContactCommand extends Command {
+ PREFIX_EMAIL + "[email protected]";

public static final String MESSAGE_EDIT_CONTACT_SUCCESS = "Edited Contact: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_CONTACT = "This contact already exists in the address book.";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided";
public static final String MESSAGE_DUPLICATE_CONTACT = "This contact already exists in the SIASA";

private final Index index;
private final EditContactDescriptor editContactDescriptor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package seedu.siasa.logic.commands.contact;

import static java.util.Objects.requireNonNull;
import static seedu.siasa.commons.core.Messages.MESSAGE_CONTACTS_LISTED_OVERVIEW;
import static seedu.siasa.commons.core.Messages.MESSAGE_CONTACTS_LIST_EMPTY;

import seedu.siasa.commons.core.Messages;
import seedu.siasa.logic.commands.Command;
import seedu.siasa.logic.commands.CommandResult;
import seedu.siasa.model.Model;
import seedu.siasa.model.contact.NameContainsKeywordsPredicate;

/**
* Finds and lists all contacts in address book whose name contains any of the argument keywords.
* Finds and lists all contacts in SIASA whose name contains any of the argument keywords.
* Keyword matching is case insensitive.
*/
public class FindContactCommand extends Command {
Expand All @@ -31,8 +32,13 @@ public FindContactCommand(NameContainsKeywordsPredicate predicate) {
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredContactList(predicate);
return new CommandResult(
String.format(Messages.MESSAGE_CONTACTS_LISTED_OVERVIEW, model.getFilteredContactList().size()));

if (model.getFilteredContactList().size() > 0) {
return new CommandResult(
String.format(MESSAGE_CONTACTS_LISTED_OVERVIEW, model.getFilteredContactList().size()));
} else {
return new CommandResult(MESSAGE_CONTACTS_LIST_EMPTY);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
package seedu.siasa.logic.commands.contact;

import static java.util.Objects.requireNonNull;
import static seedu.siasa.commons.core.Messages.MESSAGE_CONTACTS_LISTED_OVERVIEW;
import static seedu.siasa.commons.core.Messages.MESSAGE_CONTACTS_LIST_EMPTY;
import static seedu.siasa.model.Model.PREDICATE_SHOW_ALL_CONTACTS;

import seedu.siasa.logic.commands.Command;
import seedu.siasa.logic.commands.CommandResult;
import seedu.siasa.model.Model;

/**
* Lists all contacts in the address book to the user.
* Lists all contacts in the SIASA to the user.
*/
public class ListContactCommand extends Command {

public static final String COMMAND_WORD = "allcontact";

public static final String MESSAGE_SUCCESS = "Listed all contacts";


@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredContactList(PREDICATE_SHOW_ALL_CONTACTS);
return new CommandResult(MESSAGE_SUCCESS);

if (model.getFilteredContactList().size() > 0) {
return new CommandResult(String.format(MESSAGE_CONTACTS_LISTED_OVERVIEW,
model.getFilteredContactList().size()));
} else {
return new CommandResult(MESSAGE_CONTACTS_LIST_EMPTY);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.siasa.logic.commands.contact;

import static java.util.Objects.requireNonNull;
import static seedu.siasa.commons.core.Messages.MESSAGE_POLICIES_LIST_EMPTY;

import java.util.List;

Expand All @@ -14,7 +15,7 @@
import seedu.siasa.model.policy.PolicyIsOwnedByPredicate;

/**
* Finds and lists all contacts in address book whose name contains any of the argument keywords.
* Finds and lists all contacts in SIASA whose name contains any of the argument keywords.
* Keyword matching is case insensitive.
*/
public class ListContactPolicyCommand extends Command {
Expand Down Expand Up @@ -46,11 +47,16 @@ public CommandResult execute(Model model) throws CommandException {

Contact contact = lastShownList.get(contactId.getZeroBased());
model.updateFilteredPolicyList(new PolicyIsOwnedByPredicate(contact));
return new CommandResult(
String.format(
MESSAGE_LIST_CONTACT_POLICY_SUCCESS,
model.getFilteredPolicyList().size(),
contactId.getOneBased()));

if (model.getFilteredPolicyList().size() > 0) {
return new CommandResult(
String.format(
MESSAGE_LIST_CONTACT_POLICY_SUCCESS,
model.getFilteredPolicyList().size(),
contactId.getOneBased()));
} else {
return new CommandResult(MESSAGE_POLICIES_LIST_EMPTY);
}
}

@Override
Expand Down
Loading

0 comments on commit 8423a8d

Please sign in to comment.