Skip to content

Commit

Permalink
Merge pull request #86 from marcus-ny/branch-add-analytics-command
Browse files Browse the repository at this point in the history
Add analytics command
  • Loading branch information
Joseph31416 authored Mar 30, 2024
2 parents 5c1b43b + 4c285bb commit 939ccb9
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 3 deletions.
66 changes: 66 additions & 0 deletions src/main/java/seedu/address/logic/commands/AnalyticsCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Analytics;
import seedu.address.model.person.Person;

/**
* Represents a command to view the loans analytics data associated with a contact.
*/
public class AnalyticsCommand extends Command {
public static final String COMMAND_WORD = "analytics";

public static final String MESSAGE_SUCCESS = "Analytics generated";
private final Index targetIndex;

public AnalyticsCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person targetPerson = lastShownList.get(targetIndex.getZeroBased());
model.updateFilteredLoanList(loan -> loan.isAssignedTo(targetPerson) && loan.isActive());
Analytics targetAnalytics = Analytics.getAnalytics(model.getUniqueLoanList());
// TODO: Implement analytics GUI display logic
return new CommandResult(MESSAGE_SUCCESS + targetAnalytics, false, false, true);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof AnalyticsCommand)) {
return false;
}

AnalyticsCommand otherViewLoanCommand = (AnalyticsCommand) other;
return targetIndex.equals(otherViewLoanCommand.targetIndex);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetIndex", targetIndex)
.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AnalyticsCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
Expand Down Expand Up @@ -57,7 +58,6 @@ public Command parseCommand(String userInput) throws ParseException {
logger.fine("Command word: " + commandWord + "; Arguments: " + arguments);

switch (commandWord) {

case AddCommand.COMMAND_WORD:
return new AddCommandParser().parse(arguments);

Expand Down Expand Up @@ -97,6 +97,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ViewLoansCommand.COMMAND_WORD:
return new ViewLoansCommand();

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

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.AnalyticsCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* 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 {
Index index = ParserUtil.parseIndex(args);
return new AnalyticsCommand(index);
} catch (ParseException pe) {
throw new ParseException(
MESSAGE_INVALID_COMMAND_FORMAT, pe);
}
}

}
8 changes: 7 additions & 1 deletion src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ public class AddressBook implements ReadOnlyAddressBook {
* Note that non-static init blocks are not recommended to use. There are other ways to avoid duplication
* among constructors.
*/

{
persons = new UniquePersonList();
loans = new UniqueLoanList();
}

public AddressBook() {}
public AddressBook() {
}

/**
* Creates an AddressBook using the Persons in the {@code toBeCopied}
Expand Down Expand Up @@ -162,6 +164,10 @@ public ObservableList<Loan> getLoanList() {
return loans.asUnmodifiableObservableList();
}

public UniqueLoanList getUniqueLoanList() {
return loans;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.logic.commands.LinkLoanCommand;
import seedu.address.model.person.Loan;
import seedu.address.model.person.Person;
import seedu.address.model.person.UniqueLoanList;

/**
* The API of the Model component.
Expand Down Expand Up @@ -136,4 +137,6 @@ public interface Model {
void setIsLoansTab(Boolean isLoansTab);

void markLoan(Loan loanToMark);

UniqueLoanList getUniqueLoanList();
}
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.logic.commands.LinkLoanCommand;
import seedu.address.model.person.Loan;
import seedu.address.model.person.Person;
import seedu.address.model.person.UniqueLoanList;

/**
* Represents the in-memory model of the address book data.
Expand Down Expand Up @@ -170,6 +171,11 @@ public ObservableList<Loan> getSortedLoanList() {
return sortedLoans;
}

@Override
public UniqueLoanList getUniqueLoanList() {
return addressBook.getUniqueLoanList();
}

@Override
public void updateFilteredLoanList(Predicate<Loan> predicate) {
requireNonNull(predicate);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Address getAddress() {
return address;
}

public Analytics getAnalytics() {
public Analytics getAnalytics() { // This method will likely be deprecated
// TODO
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.person.Loan;
import seedu.address.model.person.Person;
import seedu.address.model.person.UniqueLoanList;
import seedu.address.testutil.PersonBuilder;

public class AddCommandTest {
Expand Down Expand Up @@ -210,6 +211,11 @@ public void addLoan(Loan loan) {
public void addLoan(LinkLoanCommand.LinkLoanDescriptor loanDescription, Person assignee) {
throw new AssertionError("This method should not be called.");
}

@Override
public UniqueLoanList getUniqueLoanList() {
throw new AssertionError("This method should not be called.");
}
}

/**
Expand Down

0 comments on commit 939ccb9

Please sign in to comment.