Skip to content

Commit

Permalink
Merge pull request #109 from Spyobird/spyobird-v1.3-update-commands
Browse files Browse the repository at this point in the history
Update `Job` related commands to use `Index`
  • Loading branch information
ianfromdover authored Mar 31, 2022
2 parents 1b8d554 + 12d3adf commit 9097989
Show file tree
Hide file tree
Showing 33 changed files with 731 additions and 267 deletions.
1 change: 0 additions & 1 deletion src/main/java/peoplesoft/commons/core/JobIdFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class JobIdFactory {
*
* @return JobId.
*/
// TODO: currently missing functionality with serdes
public static ID nextId() {
return new ID(++id);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/peoplesoft/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public class Messages {

public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_JOB_DISPLAYED_INDEX = "Invalid index for job";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "Invalid index for person";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_JOBS_LISTED_OVERVIEW = "%1$d jobs listed!";

}
1 change: 0 additions & 1 deletion src/main/java/peoplesoft/commons/core/PersonIdFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class PersonIdFactory {
*
* @return JobId.
*/
// TODO: currently missing functionality with serdes
public static ID nextId() {
return new ID(++id);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/peoplesoft/commons/core/index/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* convert it back to an int if the index will not be passed to a different component again.
*/
public class Index {
public static final String MESSAGE_CONSTRAINTS = "Index should be a non-zero positive integer.";

private int zeroBasedIndex;

/**
Expand Down Expand Up @@ -45,6 +47,11 @@ public static Index fromOneBased(int oneBasedIndex) {
return new Index(oneBasedIndex - 1);
}

@Override
public int hashCode() {
return Integer.hashCode(zeroBasedIndex);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/peoplesoft/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class DeleteCommand extends Command {

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

public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/peoplesoft/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class EditCommand extends Command {
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified "
+ "by the index number used in the displayed person list. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "Parameters: PERSON_INDEX (must be a positive integer) "
+ "[" + PREFIX_NAME + "NAME] "
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/peoplesoft/logic/commands/job/JobAddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import peoplesoft.logic.commands.CommandResult;
import peoplesoft.logic.commands.exceptions.CommandException;
import peoplesoft.logic.parser.exceptions.ParseException;
import peoplesoft.logic.parser.job.JobAddCommandParser;
import peoplesoft.model.Model;
import peoplesoft.model.job.Job;

Expand All @@ -18,12 +17,10 @@
*/
public class JobAddCommand extends Command {

// TODO: change if needed
public static final String COMMAND_WORD = "job";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a job to the database. "
+ "Parameters: "
+ "[JOBID] "
+ PREFIX_NAME + "NAME "
+ PREFIX_RATE + "RATE "
+ PREFIX_DURATION + "DURATION ";
Expand All @@ -34,14 +31,14 @@ public class JobAddCommand extends Command {
private final Job toAdd;

/**
* Creates a {@code JobAddCommand} to add a {@code Job} parsed from the arguments.
* Creates a {@code JobAddCommand} to add a {@code Job}.
*
* @param args Arguments.
* @param toAdd Job.
* @throws ParseException Thrown if there is an error with parsing.
*/
public JobAddCommand(String args) throws ParseException {
requireNonNull(args);
toAdd = new JobAddCommandParser().parse(args);
public JobAddCommand(Job toAdd) {
requireNonNull(toAdd);
this.toAdd = toAdd;
}

@Override
Expand Down
134 changes: 80 additions & 54 deletions src/main/java/peoplesoft/logic/commands/job/JobAssignCommand.java
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);
}
}
50 changes: 20 additions & 30 deletions src/main/java/peoplesoft/logic/commands/job/JobDeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,58 @@

import static java.util.Objects.requireNonNull;

import java.time.Duration;
import java.util.List;

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.exceptions.ParseException;
import peoplesoft.logic.parser.job.JobDeleteCommandParser;
import peoplesoft.model.Model;
import peoplesoft.model.employment.Employment;
import peoplesoft.model.job.Job;
import peoplesoft.model.job.Money;
import peoplesoft.model.job.Rate;
import peoplesoft.model.util.ID;

/**
* Deletes a {@code Job} with a given {@code JobId}.
*/
public class JobDeleteCommand extends Command {

//TODO: change if needed
public static final String COMMAND_WORD = "jobdelete";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the job identified by the job ID.\n"
+ "Parameters: JOBID\n"
+ ": Deletes the job identified by the index.\n"
+ "Parameters: JOB_INDEX\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_SUCCESS = "Deleted Job: %s";
public static final String MESSAGE_JOB_NOT_FOUND = "This job does not exist";

private final ID toDelete;
private final Index toDelete;

/**
* Creates a {@code JobDeleteCommand} to delete a {@code Job} by {@code JobId}.
* Creates a {@code JobDeleteCommand} to delete a {@code Job} by {@code Index}.
*
* @param args Arguments.
* @throws ParseException Thrown if there is an error with parsing.
* @param toDelete Index of job to delete.
*/
public JobDeleteCommand(String args) throws ParseException {
requireNonNull(args);
toDelete = new JobDeleteCommandParser().parse(args);
public JobDeleteCommand(Index toDelete) {
requireNonNull(toDelete);
this.toDelete = toDelete;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Job> lastShownList = model.getFilteredJobList();

if (!model.hasJob(toDelete)) {
throw new CommandException(MESSAGE_JOB_NOT_FOUND);
if (toDelete.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_JOB_DISPLAYED_INDEX);
}

try {
Job jobToDelete = new Job(toDelete, "empty",
new Rate(new Money(1), Duration.ofHours(1)), Duration.ofHours(1), false);
// Currently handles deletions in JobList implementation (by jobId)
model.deleteJob(jobToDelete);
Employment.getInstance().deleteJob(jobToDelete);
} catch (IndexOutOfBoundsException e) {
// Asserts that filtered list should always contain exactly the filtered element
assert false;
}
return new CommandResult(String.format(MESSAGE_SUCCESS, toDelete));
Job jobToDelete = lastShownList.get(toDelete.getZeroBased());
model.deleteJob(jobToDelete);
// Deletes employment associations
Employment.getInstance().deleteJob(jobToDelete);

return new CommandResult(String.format(MESSAGE_SUCCESS, jobToDelete.getDesc()));
}

@Override
Expand Down
Loading

0 comments on commit 9097989

Please sign in to comment.