Skip to content

Commit

Permalink
Merge pull request #57 from Barbaracwx/master
Browse files Browse the repository at this point in the history
changed findCommand to add ui class
  • Loading branch information
TeoHaoZhi authored Oct 18, 2023
2 parents 4686880 + 397da38 commit 7d3d198
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/main/java/seedu/stocker/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public Optional<List<Drug>> getRelevantDrugs() {
return Optional.ofNullable(relevantDrugs);
}



}
42 changes: 31 additions & 11 deletions src/main/java/seedu/stocker/commands/FindCommand.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,64 @@
package seedu.stocker.commands;

import seedu.stocker.drugs.Drug;

import java.util.ArrayList;
import java.util.List;
/*import java.util.logging.Level;
import java.util.logging.Logger;*/

import static seedu.stocker.common.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

/**
* Represents a command to find drugs in the inventory that match a given keyword.
*/
public class FindCommand extends Command {

public static final String COMMAND_WORD = "find";

/**
* Usage message for the 'find' command.
*/
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds drug in inventory " + System.lineSeparator() +
"Example: " + COMMAND_WORD + " panadol";

"Example: " + COMMAND_WORD + " panadol";

/**
* Success message displayed after successfully finding drugs in the inventory.
*/
public static final String MESSAGE_SUCCESS = "Listed all drugs with the keyword in the inventory.";

private final String keyword;

/**
* Creates a FindCommand with the specified keyword.
*
* @param keyword The keyword to search for in the inventory.
*/
public FindCommand(String keyword) {
this.keyword = keyword;
}

// Edit the function to give find functionality
/**
* Executes the 'find' command, searching for drugs that match the keyword.
*
* @return A CommandResult containing the outcome of the command execution.
*/
@Override
public CommandResult execute() {
if (keyword == null || keyword.trim().isEmpty()) {
return new CommandResult(String.format(MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE));
}

List<Drug> drugs = inventory.getAllDrugs();
int j = 1;
for (int i = 0; i < drugs.size(); i++) {
Drug drug = drugs.get(i);
List<Drug> foundDrugs = new ArrayList<>();

for (Drug drug : drugs) {
String drugDescription = drug.toString().toLowerCase();
if (drugDescription.contains(keyword.toLowerCase())) {
System.out.println("|| " + (j) + ". " + "Name: " + drug.getName() + ", Expiry Date: "
+ drug.getExpiryDate() + ", Quantity: " + drug.getQuantity());
j++;
foundDrugs.add(drug);

}
}
return new CommandResult(MESSAGE_SUCCESS);

return new CommandResult(MESSAGE_SUCCESS, foundDrugs);
}
}
22 changes: 16 additions & 6 deletions src/test/java/seedu/stocker/commands/FindCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import seedu.stocker.drugs.Inventory;
import seedu.stocker.drugs.Drug;


//test for find
class FindCommandTest {

@Test
public void executeTest() {
// Create an instance of FindCommand with a keyword
FindCommand command = new FindCommand("Panadol");
command.setData(new Inventory());
FindCommand command = new FindCommand("Pa");

// Create a new inventory
Inventory inventory = new Inventory();
Drug drug1 = new Drug("Paracetamol", "12/05/2024", 12L);
inventory.addDrug(drug1);


// Set the modified inventory for the command
command.setData(inventory);


// Define expected output
String expectedOutput = "Listed all drugs with the keyword in the inventory.";
assertEquals( new CommandResult(expectedOutput).feedbackToUser,command.execute().feedbackToUser);
}

CommandResult actualResult = command.execute();

// Test the command's execute method with the modified inventory
assertEquals(expectedOutput, actualResult.feedbackToUser);
}
}

0 comments on commit 7d3d198

Please sign in to comment.