forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
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 #107 from ReganChoy/branch-Set_Command
Add feature Set command
- Loading branch information
Showing
12 changed files
with
364 additions
and
6 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
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
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
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
81 changes: 81 additions & 0 deletions
81
src/main/java/seedu/address/logic/commands/SetCommand.java
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.LastMet; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.storage.SetStorage; | ||
|
||
/** | ||
* Sets the LastMet Overdue time period for all Clients. | ||
*/ | ||
public class SetCommand extends Command { | ||
public static final String COMMAND_WORD = "set"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": sets the LastMet overdue time period. " | ||
+ "Parameters: " | ||
+ "NUMBER_OF_DAYS " | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ "90.\n " | ||
+ "NUMBER_OF_DAYS must be an integer and be at least 0."; | ||
|
||
public static final String MESSAGE_SUCCESS = "LastMet Overdue time period has been set to %1$s days."; | ||
/** | ||
* Creates a LastMetCommand to update last mete date of the specified {@code Person} | ||
*/ | ||
private int overdueTimePeriod; | ||
|
||
/** | ||
* Creates a SetCommand to update last met overdue duration of the LastMet class | ||
*/ | ||
public SetCommand(Integer numberOfDays) { | ||
requireNonNull(numberOfDays); | ||
|
||
this.overdueTimePeriod = numberOfDays; | ||
} | ||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
LastMet.setLastMetDuration(this.overdueTimePeriod); | ||
List<Person> lastShownList = model.getSortedFilteredPersonList(); | ||
|
||
int numberOfClients = lastShownList.size(); | ||
|
||
for (int i = 0; i < numberOfClients; i++) { | ||
//Index index = Index.fromZeroBased(i); | ||
Person personToCheck = lastShownList.get(i); | ||
personToCheck.getLastMet().checkOverdue(); | ||
} | ||
|
||
SetStorage.setData(this.overdueTimePeriod); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, this.overdueTimePeriod)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof SetCommand)) { | ||
return false; | ||
} | ||
|
||
SetCommand otherSetCommand = (SetCommand) other; | ||
return overdueTimePeriod == (otherSetCommand.overdueTimePeriod); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("overdueTimePeriod", overdueTimePeriod) | ||
.toString(); | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
src/main/java/seedu/address/logic/parser/SetCommandParser.java
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.SetCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new SetCommand object | ||
*/ | ||
public class SetCommandParser { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the SetCommand | ||
* and returns an SetCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public SetCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args); | ||
|
||
if (!arePrefixesPresent(argMultimap) | ||
|| argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SetCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
int numberOfDays; | ||
try { | ||
numberOfDays = Integer.parseInt(argMultimap.getPreamble()); | ||
|
||
checkIfNegativeNumber(numberOfDays); | ||
} catch (NumberFormatException ive) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SetCommand.MESSAGE_USAGE), ive); | ||
} catch (ParseException ive) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SetCommand.MESSAGE_USAGE)); | ||
} | ||
return new SetCommand(numberOfDays); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
|
||
private void checkIfNegativeNumber(int days) throws ParseException { | ||
if (days < 0) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SetCommand.MESSAGE_USAGE)); | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package seedu.address.storage; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Scanner; | ||
|
||
import seedu.address.model.person.LastMet; | ||
|
||
/** | ||
* This class is responsible for loading and storing the LastMet Overdue value | ||
*/ | ||
public class SetStorage { | ||
private static int value; | ||
private static final String PATH = "data/setvalue.txt"; | ||
|
||
private SetStorage(int day) { | ||
value = day; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
|
||
public static void setData(int days) { | ||
value = days; | ||
storeData(); | ||
} | ||
|
||
/** | ||
* Loads the LastMet Overdue value from setvalue.txt | ||
* @throws IOException | ||
*/ | ||
public static void loadData() throws IOException { | ||
File file = new File(PATH); | ||
|
||
file.getParentFile().mkdirs(); | ||
|
||
if (!file.exists()) { | ||
file.createNewFile(); | ||
value = 90; | ||
storeData(); | ||
} else { | ||
try { | ||
checkContents(file); | ||
} catch (NumberFormatException e) { | ||
createNewFile(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Stores the LastMet Overdue value into setvalue.txt | ||
*/ | ||
public static void storeData() { | ||
try { | ||
FileWriter writer = new FileWriter(PATH); | ||
String setValue = Integer.toString(value); | ||
|
||
writer.write(setValue); | ||
writer.close(); | ||
} catch (IOException e) { | ||
System.out.println("Error"); | ||
} | ||
} | ||
|
||
/** | ||
* Reset the setvalue.txt file if value is invalid | ||
*/ | ||
public static void createNewFile() { | ||
value = 90; | ||
storeData(); | ||
} | ||
|
||
/** | ||
* Checks the contents of setvalue.txt | ||
*/ | ||
public static void checkContents(File file) throws IOException { | ||
Scanner scanner = new Scanner(file); | ||
|
||
if (file.length() == 0) { | ||
createNewFile(); | ||
} else { | ||
String input = scanner.nextLine(); | ||
Integer inputConverted = Integer.valueOf(input); | ||
|
||
if (inputConverted instanceof Integer) { | ||
if (inputConverted < 0) { | ||
createNewFile(); | ||
} else { | ||
value = inputConverted; | ||
} | ||
LastMet.setLastMetDuration(value); | ||
} else { | ||
createNewFile(); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.