Skip to content

Commit

Permalink
Merge pull request #95 from marcus-ny/branch-add-analytics-gui
Browse files Browse the repository at this point in the history
Implement Analytics GUI (Tab switching)
  • Loading branch information
kjw142857 authored Apr 2, 2024
2 parents 9e73732 + 6e4437e commit 9e01d6e
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 40 deletions.
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import java.nio.file.Path;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.person.Analytics;
import seedu.address.model.person.Loan;
import seedu.address.model.person.Person;

Expand Down Expand Up @@ -58,4 +60,12 @@ public interface Logic {
void setIsLoansTab(boolean isLoansTab);

BooleanProperty getIsLoansTab();

void setIsAnalyticsTab(boolean isAnalyticsTab);

BooleanProperty getIsAnalyticsTab();

void setToPersonTab();

ObjectProperty<Analytics> getAnalytics();
}
22 changes: 22 additions & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.logging.Logger;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
Expand All @@ -16,6 +17,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.person.Analytics;
import seedu.address.model.person.Loan;
import seedu.address.model.person.Person;
import seedu.address.storage.Storage;
Expand Down Expand Up @@ -102,4 +104,24 @@ public void setIsLoansTab(boolean isLoansTab) {
public BooleanProperty getIsLoansTab() {
return model.getIsLoansTab();
}

@Override
public ObjectProperty<Analytics> getAnalytics() {
return model.getAnalytics();
}

@Override
public BooleanProperty getIsAnalyticsTab() {
return model.getIsAnalyticsTab();
}

@Override
public void setIsAnalyticsTab(boolean isAnalyticsTab) {
model.setIsAnalyticsTab(isAnalyticsTab);
}

@Override
public void setToPersonTab() {
model.setToPersonTab();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ public CommandResult execute(Model model) throws CommandException {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ClearCommand extends Command {
public CommandResult execute(Model model) {
requireNonNull(model);
model.setAddressBook(new AddressBook());
model.setToPersonTab();
return new CommandResult(MESSAGE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public FindCommand(NameContainsKeywordsPredicate predicate) {
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(predicate);
model.setToPersonTab();
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ListCommand extends Command {
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
model.setToPersonTab();
return new CommandResult(MESSAGE_SUCCESS);
}
}
15 changes: 13 additions & 2 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import java.util.function.Predicate;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.logic.commands.LinkLoanCommand;
import seedu.address.model.person.Analytics;
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 @@ -141,7 +142,17 @@ public interface Model {

void setIsLoansTab(Boolean isLoansTab);

BooleanProperty getIsAnalyticsTab();

void setIsAnalyticsTab(Boolean isAnalyticsTab);

void setToPersonTab();

void markLoan(Loan loanToMark);

UniqueLoanList getUniqueLoanList();
void setAnalytics(Analytics analytics);

ObjectProperty<Analytics> getAnalytics();


}
44 changes: 38 additions & 6 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
import java.util.logging.Logger;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.LinkLoanCommand;
import seedu.address.model.person.Analytics;
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 All @@ -32,6 +34,8 @@ public class ModelManager implements Model {
private final FilteredList<Loan> filteredLoans;
private final SortedList<Loan> sortedLoans;
private final BooleanProperty isLoansTab = new SimpleBooleanProperty(false);
private final BooleanProperty isAnalyticsTab = new SimpleBooleanProperty(false);
private final ObjectProperty<Analytics> targetAnalytics = new SimpleObjectProperty<>();

/**
* Initializes a ModelManager with the given addressBook and userPrefs.
Expand All @@ -46,6 +50,7 @@ public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs
filteredPersons = new FilteredList<>(this.addressBook.getPersonList());
filteredLoans = new FilteredList<>(this.addressBook.getLoanList());
sortedLoans = new SortedList<>(filteredLoans, Loan::compareTo);
targetAnalytics.setValue(null);
}

public ModelManager() {
Expand Down Expand Up @@ -171,11 +176,6 @@ public ObservableList<Loan> getSortedLoanList() {
return sortedLoans;
}

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

@Override
public void updateFilteredLoanList(Predicate<Loan> predicate) {
requireNonNull(predicate);
Expand Down Expand Up @@ -213,7 +213,39 @@ public BooleanProperty getIsLoansTab() {

@Override
public void setIsLoansTab(Boolean isLoansTab) {
if (isLoansTab) {
this.isAnalyticsTab.setValue(false);
}
this.isLoansTab.setValue(isLoansTab);
}

@Override
public BooleanProperty getIsAnalyticsTab() {
return this.isAnalyticsTab;
}

@Override
public void setToPersonTab() {
this.isLoansTab.setValue(false);
this.isAnalyticsTab.setValue(false);
}

@Override
public void setIsAnalyticsTab(Boolean isAnalyticsTab) {
if (isAnalyticsTab) {
this.isLoansTab.setValue(false);
}
this.isAnalyticsTab.setValue(isAnalyticsTab);
}

@Override
public ObjectProperty<Analytics> getAnalytics() {
return targetAnalytics;
}

@Override
public void setAnalytics(Analytics analytics) {
targetAnalytics.setValue(analytics);
}

}
12 changes: 10 additions & 2 deletions src/main/java/seedu/address/model/person/Analytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Date;

import javafx.collections.ObservableList;

/**
* Represents the analytics of a LoanRecords object.
*/
Expand Down Expand Up @@ -51,6 +53,7 @@ private Analytics() {

/**
* Updates the fields that count the number of various loans.
*
* @param loan The loan to update the fields with.
*/
private void updateNumFields(Loan loan) {
Expand All @@ -76,6 +79,7 @@ private void updatePropFields() {

/**
* Updates the fields that calculate the total value of various loans.
*
* @param loan The loan to update the fields with.
*/
private void updateValueFields(Loan loan) {
Expand Down Expand Up @@ -106,6 +110,7 @@ private void updateAverageFields() {

/**
* Updates the fields that calculate the earliest and latest dates of various loans.
*
* @param loan The loan to update the fields with.
*/
private void updateDateFields(Loan loan) {
Expand All @@ -127,10 +132,13 @@ private void updateDateFields(Loan loan) {

/**
* Returns an Analytics object that represents the analytics of a LoanRecords object.
* @param uniqueLoanList The LoanRecords object to get the analytics from.
*
* @param loanList The list of loans to calculate the analytics from.
* @return The Analytics object that represents the analytics of the LoanRecords object.
*/
public static Analytics getAnalytics(UniqueLoanList uniqueLoanList) {
public static Analytics getAnalytics(ObservableList<Loan> loanList) {
UniqueLoanList uniqueLoanList = new UniqueLoanList();
uniqueLoanList.setLoans(loanList);
Analytics analytics = new Analytics();
for (int i = 0; i < uniqueLoanList.size(); i++) {
Loan loan = uniqueLoanList.getLoan(i);
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/seedu/address/ui/AnalyticsPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package seedu.address.ui;

import javafx.beans.property.ObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.chart.PieChart;
import javafx.scene.layout.Region;
import seedu.address.model.person.Analytics;

/**
* Panel containing the analytics of the loan records.
*/
public class AnalyticsPanel extends UiPart<Region> {
private static final String FXML = "AnalyticsPanel.fxml";

@FXML
private PieChart pieChart;

/**
* Creates a {@code AnalyticsPanel} with the given {@code ObjectProperty}.
*/
public AnalyticsPanel(ObjectProperty<Analytics> analytics) {
super(FXML);
pieChart.setData(FXCollections.observableArrayList());
analytics.addListener((observable, oldValue, newValue) -> {
updateChart(newValue);
});
}

private void updateChart(Analytics analytics) {
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Active Loans", analytics.getNumActiveLoans()),
new PieChart.Data("Overdue Loans", analytics.getNumOverdueLoans())
);
pieChart.setData(pieChartData);
}
}
Loading

0 comments on commit 9e01d6e

Please sign in to comment.