Skip to content

Commit

Permalink
Draft for View Command
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxunn committed Oct 20, 2023
1 parent 0bab3a7 commit f4aed83
Show file tree
Hide file tree
Showing 14 changed files with 338 additions and 3 deletions.
62 changes: 62 additions & 0 deletions src/main/java/seedu/address/logic/commands/ViewClientCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.function.Predicate;
import java.util.logging.Logger;

import seedu.address.commons.core.LogsCenter;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.commons.core.index.Index;

public class ViewClientCommand extends Command {

public static final String COMMAND_WORD = "viewclient";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Views the client identified by the index number used in the listclient list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_SUCCESS = "Viewed Client Successfully";
public static final String MESSAGE_INVALID_INDEX = "Invalid Client index";
public static final String MESSAGE_NOT_A_CLIENT = "The person at the specified index is not a client.";


public static final Logger logger = LogsCenter.getLogger(ViewClientCommand.class);

private final Index targetIndex;

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

@Override
public CommandResult execute(Model model) {
requireNonNull(model);


if (targetIndex.getZeroBased() < 0) {
return new CommandResult(MESSAGE_INVALID_INDEX);
}

Person clientToView = model.getFilteredPersonList().get(targetIndex.getZeroBased());

Predicate<Person> VIEW_CLIENT_PREDICATE = person -> person.equals(clientToView);

model.updateFilteredPersonList(VIEW_CLIENT_PREDICATE);

if (!VIEW_CLIENT_PREDICATE.test(clientToView)) {
return new CommandResult(MESSAGE_NOT_A_CLIENT);
}

logger.info("Target Index: " + targetIndex.getZeroBased());
logger.info("Client to View: " + clientToView);
return new CommandResult(MESSAGE_SUCCESS);
}

}



31 changes: 31 additions & 0 deletions src/main/java/seedu/address/logic/commands/ViewCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.model.Model;
import seedu.address.model.person.Person;
public class ViewCommand extends Command {
public static final String COMMAND_WORD = "view";

public static final String MESSAGE_SUCCESS = "View person success";

private final int targetIndex;

public ViewCommand(int targetIndex) {
this.targetIndex = targetIndex;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);

Person personToView = model.getPersonByIndex(targetIndex);

if (personToView != null) {
return new CommandResult(MESSAGE_SUCCESS);
} else {
return new CommandResult("Person not found at index " + targetIndex);
}
}
}

62 changes: 62 additions & 0 deletions src/main/java/seedu/address/logic/commands/ViewLeadCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.function.Predicate;
import java.util.logging.Logger;

import seedu.address.commons.core.LogsCenter;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.commons.core.index.Index;

public class ViewLeadCommand extends Command {

public static final String COMMAND_WORD = "viewlead";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Views the client identified by the index number used in the listlead list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_SUCCESS = "Viewed Client Successfully";
public static final String MESSAGE_INVALID_INDEX = "Invalid Client index";
public static final String MESSAGE_NOT_A_LEAD = "The person at the specified index is not a lead.";


public static final Logger logger = LogsCenter.getLogger(ViewClientCommand.class);

private final Index targetIndex;

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

@Override
public CommandResult execute(Model model) {
requireNonNull(model);


if (targetIndex.getZeroBased() < 0 || targetIndex.getZeroBased() >= model.getFilteredPersonList().size()) {
return new CommandResult(MESSAGE_INVALID_INDEX);
}

Person leadToView = model.getFilteredPersonList().get(targetIndex.getZeroBased());

Predicate<Person> VIEW_LEAD_PREDICATE = person -> person.equals(leadToView);

model.updateFilteredPersonList(VIEW_LEAD_PREDICATE);

if (!VIEW_LEAD_PREDICATE.test(leadToView)) {
return new CommandResult(MESSAGE_NOT_A_LEAD);
}

logger.info("Target Index: " + targetIndex.getZeroBased());
logger.info("Client to View: " + leadToView);
return new CommandResult(MESSAGE_SUCCESS);
}

}



Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import seedu.address.logic.commands.ListClientCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.ListLeadCommand;
import seedu.address.logic.commands.ViewClientCommand;
import seedu.address.logic.commands.ViewLeadCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -89,6 +91,12 @@ public Command parseCommand(String userInput) throws ParseException {
case ListLeadCommand.COMMAND_WORD:
return new ListLeadCommand();

case ViewClientCommand.COMMAND_WORD:
return new ViewClientCommandParser().parse(arguments);

case ViewLeadCommand.COMMAND_WORD:
return new ViewLeadCommandParser().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,29 @@
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.ViewClientCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new DeleteCommand object
*/
public class ViewClientCommandParser implements Parser<ViewClientCommand> {

/**
* Parses the given {@code String} of arguments in the context of the DeleteCommand
* and returns a DeleteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ViewClientCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new ViewClientCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewClientCommand.MESSAGE_USAGE), pe);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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.ViewLeadCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new DeleteCommand object
*/
public class ViewLeadCommandParser implements Parser<ViewLeadCommand> {

/**
* Parses the given {@code String} of arguments in the context of the DeleteCommand
* and returns a DeleteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ViewLeadCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new ViewLeadCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewLeadCommand.MESSAGE_USAGE), pe);
}
}

}
25 changes: 22 additions & 3 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
* The API of the Model component.
*/
public interface Model {
/** {@code Predicate} that always evaluate to true */
/**
* {@code Predicate} that always evaluate to true
*/
Predicate<Person> PREDICATE_SHOW_ALL_PERSONS = unused -> true;

/**
Expand Down Expand Up @@ -51,7 +53,9 @@ public interface Model {
*/
void setAddressBook(ReadOnlyAddressBook addressBook);

/** Returns the AddressBook */
/**
* Returns the AddressBook
*/
ReadOnlyAddressBook getAddressBook();

/**
Expand Down Expand Up @@ -82,12 +86,27 @@ public interface Model {
*/
void setPerson(Person target, Person editedPerson);

/** Returns an unmodifiable view of the filtered person list */
/**
* Returns an unmodifiable view of the filtered person list
*/
ObservableList<Person> getFilteredPersonList();

/**
* Returns the person at the specified index in the filtered person list.
*
* @param index The index of the person to retrieve.
* @return The person at the specified index, or null if the index is out of bounds.
*/
Person getPersonByIndex(int index);

/**
* Updates the filter of the filtered person list to filter by the given {@code predicate}.
*
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredPersonList(Predicate<Person> predicate);

void viewClient(Client clientToView);

void viewLead(Lead clientToView);
}
21 changes: 21 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ public void addLead(Lead lead) {
updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
}

@Override
public void viewClient(Client clientToView) {
Predicate<Person> clientPredicate = person -> person.equals(clientToView);
updateFilteredPersonList(clientPredicate);
}

@Override
public void viewLead(Lead clientToView) {
Predicate<Person> leadPredicate = person -> person.equals(clientToView);
updateFilteredPersonList(leadPredicate);
}


@Override
public void setPerson(Person target, Person editedPerson) {
requireAllNonNull(target, editedPerson);
Expand Down Expand Up @@ -158,5 +171,13 @@ public boolean equals(Object other) {
&& userPrefs.equals(otherModelManager.userPrefs)
&& filteredPersons.equals(otherModelManager.filteredPersons);
}
@Override
public Person getPersonByIndex(int index) {
ObservableList<Person> filteredPersons = getFilteredPersonList();
if (index >= 0 && index < filteredPersons.size()) {
return filteredPersons.get(index);
}
return null;
}

}
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.Logic;
import seedu.address.model.person.Person;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -44,6 +45,10 @@ public class MainWindow extends UiPart<Stage> {
@FXML
private StackPane personListPanelPlaceholder;

@FXML
private StackPane clientListPanelPlaceholder;


@FXML
private StackPane resultDisplayPlaceholder;

Expand Down Expand Up @@ -193,4 +198,5 @@ private CommandResult executeCommand(String commandText) throws CommandException
throw e;
}
}

}
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/ui/UiManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.ui;


import java.util.logging.Logger;

import javafx.application.Platform;
Expand All @@ -11,6 +12,8 @@
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.Logic;
import seedu.address.model.person.Client;
import seedu.address.model.person.Person;

/**
* The manager of the UI component.
Expand Down
Loading

0 comments on commit f4aed83

Please sign in to comment.