forked from nus-cs2113-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from Barbaracwx/master
changed findCommand to add ui class
- Loading branch information
Showing
3 changed files
with
49 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,6 @@ public Optional<List<Drug>> getRelevantDrugs() { | |
return Optional.ofNullable(relevantDrugs); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters