forked from nus-cs2103-AY2122S2/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 #109 from Spyobird/spyobird-v1.3-update-commands
Update `Job` related commands to use `Index`
- Loading branch information
Showing
33 changed files
with
731 additions
and
267 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
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
134 changes: 80 additions & 54 deletions
134
src/main/java/peoplesoft/logic/commands/job/JobAssignCommand.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 |
---|---|---|
@@ -1,102 +1,128 @@ | ||
package peoplesoft.logic.commands.job; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static peoplesoft.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static peoplesoft.commons.util.CollectionUtil.requireAllNonNull; | ||
import static peoplesoft.logic.parser.CliSyntax.PREFIX_INDEX; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import peoplesoft.commons.core.Messages; | ||
import peoplesoft.commons.core.index.Index; | ||
import peoplesoft.logic.commands.Command; | ||
import peoplesoft.logic.commands.CommandResult; | ||
import peoplesoft.logic.commands.exceptions.CommandException; | ||
import peoplesoft.logic.parser.ArgumentMultimap; | ||
import peoplesoft.logic.parser.ParserUtil; | ||
import peoplesoft.logic.parser.exceptions.ParseException; | ||
import peoplesoft.logic.parser.job.JobAssignCommandParser; | ||
import peoplesoft.model.Model; | ||
import peoplesoft.model.employment.Employment; | ||
import peoplesoft.model.employment.exceptions.DuplicateEmploymentException; | ||
import peoplesoft.model.job.Job; | ||
import peoplesoft.model.person.Person; | ||
import peoplesoft.model.util.ID; | ||
|
||
/** | ||
* Assigns a {@code Job} to a {@code Person}. | ||
*/ | ||
public class JobAssignCommand extends Command { | ||
|
||
//TODO: change if needed | ||
public static final String COMMAND_WORD = "assign"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Assigns a job to a person. " | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Assigns a job to one or more person(s). " | ||
+ "Parameters: " | ||
+ "JOBID " | ||
+ PREFIX_INDEX + "INDEX "; | ||
+ "JOB_INDEX " | ||
+ PREFIX_INDEX + "PERSON_INDEX [PERSON_INDEX]..."; | ||
|
||
public static final String MESSAGE_SUCCESS = "Assigned Job %s to %s\n%s has the following jobs: %s"; | ||
public static final String MESSAGE_JOB_NOT_FOUND = "This job does not exist"; | ||
public static final String MESSAGE_DUPLICATE_EMPLOYMENT = "This person is already assigned to this job"; | ||
public static final String MESSAGE_SUCCESS = "Assigned Job %s to %s."; | ||
public static final String MESSAGE_DUPLICATE_EMPLOYMENT = | ||
"%s person(s) have already been assigned to this job."; | ||
|
||
private ID jobId; | ||
private Index personIndex; | ||
private Index jobIndex; | ||
private Set<Index> personIndexes; | ||
private Employment instance; | ||
|
||
/** | ||
* Creates a {@code JobAssignCommand} to assign a {@code Job} to a {@code Person}. | ||
* Creates a {@code JobAssignCommand} to assign a {@code Job} to a set of {@code Person}s. | ||
* | ||
* @param args Arguments. | ||
* @throws ParseException Thrown if there is an error with parsing. | ||
* @param jobIndex Index of the job. | ||
* @param personIndexes Set of indexes of persons | ||
* @param employment Employment to use (for easier testing) | ||
*/ | ||
public JobAssignCommand(String args) throws ParseException { | ||
requireNonNull(args); | ||
parseArgs(args); | ||
public JobAssignCommand(Index jobIndex, Set<Index> personIndexes, Employment employment) { | ||
requireAllNonNull(jobIndex, personIndexes, employment); | ||
this.jobIndex = jobIndex; | ||
this.personIndexes = personIndexes; | ||
this.instance = employment; | ||
} | ||
|
||
private void parseArgs(String args) throws ParseException { | ||
// TODO: Arguably does not follow SRP/LoD | ||
ArgumentMultimap argumentMultimap = new JobAssignCommandParser().parse(args); | ||
try { | ||
jobId = new ID(ParserUtil.parseString(argumentMultimap.getPreamble())); | ||
} catch (IllegalArgumentException e) { // from ID constructor | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
JobAssignCommand.MESSAGE_USAGE)); | ||
} | ||
personIndex = ParserUtil.parseIndex(argumentMultimap.getValue(PREFIX_INDEX).get()); | ||
} | ||
|
||
// TODO: Change implementation if needed. | ||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Job> lastShownJobs = model.getFilteredJobList(); | ||
List<Person> lastShownPersons = model.getFilteredPersonList(); | ||
|
||
if (jobIndex.getZeroBased() >= lastShownJobs.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_JOB_DISPLAYED_INDEX); | ||
} | ||
|
||
if (!model.hasJob(jobId)) { | ||
throw new CommandException(MESSAGE_JOB_NOT_FOUND); | ||
for (Index i : personIndexes) { | ||
if (i.getZeroBased() >= lastShownPersons.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
} | ||
|
||
List<Person> lastShownList = model.getFilteredPersonList(); | ||
Job job = lastShownJobs.get(jobIndex.getZeroBased()); | ||
Set<Person> persons = getPersons(personIndexes, lastShownPersons); | ||
|
||
int dupeCount = 0; | ||
StringBuilder stringPersons = new StringBuilder(); | ||
|
||
if (personIndex.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
for (Person p : persons) { | ||
try { | ||
instance.associate(job, p); | ||
stringPersons.append(p.getName()).append(", "); | ||
} catch (DuplicateEmploymentException e) { | ||
dupeCount++; | ||
} | ||
} | ||
if (stringPersons.length() > 0) { | ||
stringPersons.delete(stringPersons.length() - 2, stringPersons.length()); | ||
} | ||
String successPersons = stringPersons.toString(); | ||
|
||
if (dupeCount > 0) { | ||
if (successPersons.isBlank()) { | ||
throw new CommandException(String.format(MESSAGE_DUPLICATE_EMPLOYMENT, dupeCount)); | ||
} else { | ||
throw new CommandException(String.format(MESSAGE_DUPLICATE_EMPLOYMENT, dupeCount) + "\n" | ||
+ String.format(MESSAGE_SUCCESS, job.getDesc(), successPersons)); | ||
} | ||
} | ||
|
||
Person person = lastShownList.get(personIndex.getZeroBased()); | ||
try { | ||
// TODO: This code breaks LoD. | ||
Job assignedJob = model.getAddressBook().getJobList() | ||
.filtered(job -> job.getJobId().equals(jobId)).get(0); | ||
Employment.getInstance().associate(assignedJob, person); | ||
} catch (IndexOutOfBoundsException e) { | ||
// Asserts that filtered list should always contain exactly the filtered element | ||
assert false; | ||
} catch (DuplicateEmploymentException e) { | ||
throw new CommandException(MESSAGE_DUPLICATE_EMPLOYMENT); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, job.getDesc(), successPersons)); | ||
} | ||
|
||
private Set<Person> getPersons(Set<Index> personIndexes, List<Person> personList) { | ||
Set<Person> persons = new HashSet<>(); | ||
for (Index i : personIndexes) { | ||
persons.add(personList.get(i.getZeroBased())); | ||
} | ||
return persons; | ||
} | ||
|
||
List<Job> jobs = Employment.getInstance().getJobs(person, model); | ||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof JobAssignCommand)) { | ||
return false; | ||
} | ||
|
||
// TODO: For now just prints a list of the jobs associated with a person to console. | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, jobId, person.getName(), | ||
person.getName(), jobs)); | ||
// state check | ||
JobAssignCommand c = (JobAssignCommand) other; | ||
return jobIndex.equals(c.jobIndex) | ||
&& personIndexes.equals(c.personIndexes); | ||
} | ||
} |
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.