Skip to content

Commit

Permalink
Update application to accept job adding command (nus-cs2103-AY1718S2#73)
Browse files Browse the repository at this point in the history
* Create command classes for job management features

* Update Model API to support addJob and closeJob

* Add new prefixes in CliSyntax to support adding of job

* Rename command for remark

* Add closeJob and addJob methods in ModelManager and AddCommandTest class

* Rename attribute in Job class

* Update AddJobCommand class

* Update message in AddJobCommand class

* UserGuide.adoc: update job adding feature format

* Add parse method in ParserUtil class for VehicleNumber

* Add new class  AddJobCommandParser (incomplete)

* Update authorship to classes

* Update Logic and Model component to support job management

* Remove all address parameter for add employee

* Fix checkstyle error

Fix checkstyle error

* Update ui classes to remove address field

* Rename message constant

* Add toSet method in RemarkList class

* Update CarviciM to accept jobs (non-persistent)

* Update toString method of Job class

* Fix checkstyle error

* Fix checkstyle error

* Update job adding to show on UI

* Add test for AddJobCommand

* Update authorship

* Fix checkstyle errors

Fix checkstyle error

Fix checkstyle error

Remove trailing whitespace

Fix error

* Rename attributes in classes and add new constructors

* Update toSet method in RemarkList

* Add two new storage classes for Job

* Fix checkstyle error

* Add new attribute in XmlSerializableAddressBook class for persitent data for job

* Add method in ModelManager to support initialization of running job number

* Fix checkstyle error

* Fix missing methods
  • Loading branch information
whenzei authored and yuhongherald committed Mar 26, 2018
1 parent a0873b2 commit 44e0109
Show file tree
Hide file tree
Showing 34 changed files with 808 additions and 87 deletions.
5 changes: 1 addition & 4 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import seedu.address.model.ModelManager;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.UserPrefs;
import seedu.address.model.job.JobNumber;
import seedu.address.model.util.SampleDataUtil;
import seedu.address.storage.AddressBookStorage;
import seedu.address.storage.JsonUserPrefsStorage;
Expand Down Expand Up @@ -68,15 +67,13 @@ public void init() throws Exception {
initLogging(config);

model = initModelManager(storage, userPrefs);
model.initJobNumber();

logic = new LogicManager(model);

ui = new UiManager(logic, config, userPrefs);

initEventsCenter();

//Initialize the job number
JobNumber.initialize("0");
}

private String getApplicationParameter(String parameterName) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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 employee index provided is invalid";
public static final String MESSAGE_INVALID_EMPLOYEE_DISPLAYED_INDEX = "The employee index provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_INVALID_THEME_INDEX = "The theme index provided is invalid";
public static final String MESSAGE_INVALID_FILE_PATH = "The file path is invalid";
Expand Down
56 changes: 52 additions & 4 deletions src/main/java/seedu/address/logic/commands/AddJobCommand.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ASSIGNED_EMPLOYEE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_VEHICLE_NUMBER;

import java.util.ArrayList;
import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.job.Date;
import seedu.address.model.job.Job;
import seedu.address.model.job.JobNumber;
import seedu.address.model.job.Status;
import seedu.address.model.job.VehicleNumber;
import seedu.address.model.person.Employee;
import seedu.address.model.person.Person;
import seedu.address.model.person.UniqueEmployeeList;
import seedu.address.model.person.exceptions.DuplicateEmployeeException;
import seedu.address.model.remark.RemarkList;

//@@author whenzei
/**
* Adds a job to CarviciM
*/
Expand All @@ -33,14 +50,45 @@ public class AddJobCommand extends UndoableCommand {

public static final String MESSAGE_SUCCESS = "New job added: %1$s";

private final Job toAdd;
private final Person client;
private final VehicleNumber vehicleNumber;
private final ArrayList<Index> targetIndices;
private final UniqueEmployeeList assignedEmployees;

private Job toAdd;

/**
* Creates an AddJobCommand to add the specified {@code Job}
*/
public AddJobCommand(Job job) {
requireNonNull(job);
toAdd = job;
public AddJobCommand(Person client, VehicleNumber vehicleNumber, ArrayList<Index> targetIndices) {
requireAllNonNull(client, vehicleNumber, targetIndices);
this.client = client;
this.vehicleNumber = vehicleNumber;
this.targetIndices = targetIndices;
assignedEmployees = new UniqueEmployeeList();
}

@Override
protected void preprocessUndoableCommand() throws CommandException {
List<Employee> lastShownList = model.getFilteredPersonList();

//Check for valid employee indices
for (Index targetIndex : targetIndices) {
if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_EMPLOYEE_DISPLAYED_INDEX);
}
}

try {
for (Index targetIndex : targetIndices) {
assignedEmployees.add(lastShownList.get(targetIndex.getZeroBased()));
}
toAdd = new Job(client, vehicleNumber, new JobNumber(), new Date(), assignedEmployees,
new Status(Status.STATUS_ONGOING), new RemarkList());
} catch (DuplicateEmployeeException e) {
throw new CommandException("Duplicate employee index");
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected void preprocessUndoableCommand() throws CommandException {
List<Employee> lastShownList = model.getFilteredPersonList();

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
throw new CommandException(Messages.MESSAGE_INVALID_EMPLOYEE_DISPLAYED_INDEX);
}

employeeToDelete = lastShownList.get(targetIndex.getZeroBased());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void preprocessUndoableCommand() throws CommandException {
List<Employee> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
throw new CommandException(Messages.MESSAGE_INVALID_EMPLOYEE_DISPLAYED_INDEX);
}

employeeToEdit = lastShownList.get(index.getZeroBased());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public CommandResult execute() throws CommandException {
List<Employee> lastShownList = model.getFilteredPersonList();

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
throw new CommandException(Messages.MESSAGE_INVALID_EMPLOYEE_DISPLAYED_INDEX);
}

EventsCenter.getInstance().post(new JumpToListRequestEvent(targetIndex));
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/seedu/address/logic/parser/AddJobCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ASSIGNED_EMPLOYEE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_VEHICLE_NUMBER;

import java.util.ArrayList;
import java.util.stream.Stream;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.AddJobCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.job.VehicleNumber;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;

//@@author whenzei
/**
Expand All @@ -29,10 +38,24 @@ public AddJobCommand parse(String args) throws ParseException {

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE,
PREFIX_EMAIL, PREFIX_VEHICLE_NUMBER, PREFIX_ASSIGNED_EMPLOYEE)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddJobCommand.MESSAGE_USAGE));
}

//More to be added
return null; //Stub
try {
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME)).get();
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE)).get();
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL)).get();
VehicleNumber vehicleNumber =
ParserUtil.parseVehicleNumber(argMultimap.getValue(PREFIX_VEHICLE_NUMBER)).get();
ArrayList<Index> assignedEmployeeIndices =
ParserUtil.parseIndices(argMultimap.getAllValues(PREFIX_ASSIGNED_EMPLOYEE));

Person client = new Person(name, phone, email);
return new AddJobCommand(client, vehicleNumber, assignedEmployeeIndices);

} catch (IllegalValueException ive) {
throw new ParseException(ive.getMessage(), ive);
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
Expand Down Expand Up @@ -62,6 +63,18 @@ public static Index parseIndex(String oneBasedIndex) throws IllegalValueExceptio
return Index.fromOneBased(Integer.parseInt(trimmedIndex));
}

/**
* Parses {@code Collection<String> indices} into a {@code Set<Index>}.
*/
public static ArrayList<Index> parseIndices(Collection<String> indices) throws IllegalValueException {
requireNonNull(indices);
final ArrayList<Index> indexList = new ArrayList<>();
for (String index : indices) {
indexList.add(parseIndex(index));
}
return indexList;
}

/**
* Parses a {@code String name} into a {@code Name}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public interface Model {
/** {@code Predicate} that always evaluate to true */
Predicate<Employee> PREDICATE_SHOW_ALL_PERSONS = unused -> true;

/** {@code Predicate} that always evaluate to true */
Predicate<Job> PREDICATE_SHOW_ALL_JOBS = unused -> true;

/** Clears existing backing model and replaces with the provided new data. */
void resetData(ReadOnlyAddressBook newData, CommandWords newCommandWords);

Expand All @@ -30,6 +33,9 @@ public interface Model {
/** Returns the AddressBook */
ReadOnlyAddressBook getAddressBook();

/** Initializes the job number based on the list of jobs */
void initJobNumber();

/** Adds the given job */
void addJob(Job job);

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import seedu.address.commons.events.model.AddressBookChangedEvent;
import seedu.address.logic.commands.CommandWords;
import seedu.address.model.job.Job;
import seedu.address.model.job.JobNumber;
import seedu.address.model.job.exceptions.JobNotFoundException;
import seedu.address.model.person.Employee;
import seedu.address.model.person.exceptions.DuplicateEmployeeException;
Expand All @@ -25,6 +26,7 @@
*/
public class ModelManager extends ComponentManager implements Model {
private static final Logger logger = LogsCenter.getLogger(ModelManager.class);
private static final String ONE_AS_STRING = "1";

private final AddressBook addressBook;
private final FilteredList<Employee> filteredEmployees;
Expand All @@ -50,6 +52,25 @@ public ModelManager() {
this(new AddressBook(), new UserPrefs());
}

//@@author whenzei
/**
* Initializes the running job number based on the past job numbers.
*/
@Override
public void initJobNumber() {
if (filteredJobs.isEmpty()) {
JobNumber.initialize(ONE_AS_STRING);
return;
}
int largest = filteredJobs.get(0).getJobNumber().asInteger();
for (Job job : filteredJobs) {
if ( job.getJobNumber().asInteger() > largest) {
largest = job.getJobNumber().asInteger();
}
}
JobNumber.initialize(largest + 1);
}

@Override
public void resetData(ReadOnlyAddressBook newData, CommandWords newCommandWords) {
addressBook.resetData(newData);
Expand Down Expand Up @@ -82,6 +103,9 @@ private void indicateAddressBookChanged() {

@Override
public synchronized void addJob(Job job) {
addressBook.addJob(job);
updateFilteredJobList(PREDICATE_SHOW_ALL_JOBS);
indicateAddressBookChanged();
}

@Override
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/seedu/address/model/job/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
public class Date {
private static final String DATE_FORMATTER_PATTERN = "MMM d yyy";

public final String date;
public final String value;

public Date() {
date = generateDate();
value = generateDate();
}

public Date(String date) {
value = date;
}

/**
Expand All @@ -27,18 +31,18 @@ private String generateDate() {

@Override
public String toString() {
return date;
return value;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Date // instanceof handles nulls
&& this.date.equals(((Date) other).date)); // state check
&& this.value.equals(((Date) other).value)); // state check
}

@Override
public int hashCode() {
return date.hashCode();
return value.hashCode();
}
}
25 changes: 15 additions & 10 deletions src/main/java/seedu/address/model/job/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public Set<Employee> getAssignedEmployees() {
* if modification is attempted.
*/
public Set<Remark> getRemarks() {
//Stub
return null;
return Collections.unmodifiableSet(remarks.toSet());
}

@Override
Expand Down Expand Up @@ -106,20 +105,26 @@ public int hashCode() {
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Job Number: ")
builder.append("\nJob Number: ")
.append(getJobNumber())
.append(" Status: ")
.append(getStatus())
.append("[" + getStatus() + "]")
.append(" Start Date: ")
.append(getDate())
.append(" Vehicle ID: ")
.append(" \nVehicle ID: ")
.append(getVehicleNumber())
.append(" Client: ")
.append(getClient())
.append(" Remarks: ");
getRemarks().forEach(builder::append);
builder.append(" Assigned Employees: ");
getAssignedEmployees().forEach(builder::append);
.append(" \nRemarks: ");

for (Remark remark : remarks) {
builder.append("\n" + remark);
}

builder.append(" \nAssigned Employees:");
for (Employee assignedEmployee : assignedEmployees) {
builder.append("\n" + assignedEmployee);
}

return builder.toString();
}
}
Loading

0 comments on commit 44e0109

Please sign in to comment.