Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2223S2#102 from hingen/add-add-command
Browse files Browse the repository at this point in the history
Add add commands
  • Loading branch information
jedidiahC authored Mar 16, 2023
2 parents f059574 + a04158b commit b6c755e
Show file tree
Hide file tree
Showing 12 changed files with 846 additions and 571 deletions.
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ public class Messages {
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_DOES_NOT_EXIST = "%1$s does not exist";
public static final String MESSAGE_MODULE_DOES_NOT_EXIST = "Module %1$s does not exist";
public static final String MESSAGE_LECTURE_DOES_NOT_EXIST = "Lecture %1$s of module %2$s does not exist";
public static final String MESSAGE_VIDEO_DOES_NOT_EXIST =
"Video %1$s of lecture %2$s of module %3$s does not exist";

}
78 changes: 22 additions & 56 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
@@ -1,67 +1,33 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_LECTURE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE;
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_TAG;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;

/**
* Adds a person to the address book.
*/
public class AddCommand extends Command {

public abstract class AddCommand extends Command {
public static final String COMMAND_WORD = "add";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book. "
public static final String MESSAGE_USAGE = COMMAND_WORD + ": (1) Adds a module to the tracker. "
+ "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 "
+ PREFIX_TAG + "friends "
+ PREFIX_TAG + "owesMoney";

public static final String MESSAGE_SUCCESS = "New person added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";

private final Person toAdd;

/**
* Creates an AddCommand to add the specified {@code Person}
*/
public AddCommand(Person person) {
requireNonNull(person);
toAdd = person;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (model.hasPerson(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.addPerson(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof AddCommand // instanceof handles nulls
&& toAdd.equals(((AddCommand) other).toAdd));
}
+ "{module_code} "
+ "[" + PREFIX_NAME + " {name}] "
+ "Example: " + COMMAND_WORD + " CS2040S "
+ PREFIX_NAME + " Data Structures and Algorithms | "
+ "(2) Adds a lecture to a module. "
+ "Parameters: "
+ "{lecture_name} "
+ "[" + PREFIX_MODULE + " {module_code}] "
+ "Example: " + COMMAND_WORD + " Lecture 01 "
+ PREFIX_MODULE + " CS2040S | "
+ "(3) Adds a video to a lecture. "
+ "Parameters: "
+ "{video_name} "
+ "[" + PREFIX_MODULE + " {module_code}] "
+ "[" + PREFIX_LECTURE + " {lecture_name}] "
+ "Example: " + COMMAND_WORD + " Video 01 "
+ PREFIX_MODULE + " CS2040S "
+ PREFIX_LECTURE + " Lecture 01";
}
72 changes: 72 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddLectureCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_MODULE_DOES_NOT_EXIST;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.lecture.Lecture;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.module.ReadOnlyModule;

/**
* Adds a lecture to a module.
*/
public class AddLectureCommand extends AddCommand {

public static final String MESSAGE_SUCCESS = "New lecture added to module %s: %s";
public static final String MESSAGE_DUPLICATE_LECTURE = "This lecture already exists in module %s";

private final ModuleCode moduleCode;
private final Lecture toAdd;

/**
* Creates an {@code AddLectureCommand} to add {@code lecture} to the module with code {@code moduleCode}.
*
* @param moduleCode The code of the module to add the lecture to.
* @param lecture The lecture to be added.
*/
public AddLectureCommand(ModuleCode moduleCode, Lecture lecture) {
requireAllNonNull(moduleCode, lecture);

this.moduleCode = moduleCode;
toAdd = lecture;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (!model.hasModule(moduleCode)) {
throw new CommandException(String.format(MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
}

ReadOnlyModule module = model.getTracker().getModule(moduleCode);

if (model.hasLecture(module, toAdd)) {
throw new CommandException(String.format(MESSAGE_DUPLICATE_LECTURE, moduleCode));
}

model.addLecture(module, toAdd);

return new CommandResult(String.format(MESSAGE_SUCCESS, moduleCode, toAdd));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

if (!(other instanceof AddLectureCommand)) {
return false;
}

AddLectureCommand otherCommand = (AddLectureCommand) other;

return moduleCode.equals(otherCommand.moduleCode)
&& toAdd.equals(otherCommand.toAdd);
}

}
48 changes: 48 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddModuleCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.module.Module;

/**
* Adds a module to the tracker.
*/
public class AddModuleCommand extends AddCommand {

public static final String MESSAGE_SUCCESS = "New module added: %1$s";
public static final String MESSAGE_DUPLICATE_MODULE = "This module already exists in the tracker";

private final Module toAdd;

/**
* Creates an {@code AddModuleCommand} to add {@code module} to the tracker.
*
* @param module The module to be added.
*/
public AddModuleCommand(Module module) {
requireNonNull(module);
toAdd = module;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (model.hasModule(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_MODULE);
}

model.addModule(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
}

@Override
public boolean equals(Object other) {
return other == this
|| (other instanceof AddModuleCommand
&& toAdd.equals(((AddModuleCommand) other).toAdd));
}

}
85 changes: 85 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddVideoCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_LECTURE_DOES_NOT_EXIST;
import static seedu.address.commons.core.Messages.MESSAGE_MODULE_DOES_NOT_EXIST;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.lecture.LectureName;
import seedu.address.model.lecture.ReadOnlyLecture;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.module.ReadOnlyModule;
import seedu.address.model.video.Video;

/**
* Adds a video to a lecture.
*/
public class AddVideoCommand extends AddCommand {

public static final String MESSAGE_SUCCESS = "New video added to module %s of lecture %s: %s";
public static final String MESSAGE_DUPLICATE_VIDEO = "This video already exists in lecture %s of module %s";

private final ModuleCode moduleCode;
private final LectureName lectureName;
private final Video toAdd;

/**
* Creates an {@code AddVideoCommand} to add {@code video} to the lecture with name {@code lectureName} which
* belongs to module with code {@code moduleCode}
*
* @param moduleCode The code of the module which the lecture with name {@code lectureName} belongs to.
* @param lectureName The name of the lecture to add the video to.
* @param video The video to be added.
*/
public AddVideoCommand(ModuleCode moduleCode, LectureName lectureName, Video video) {
requireAllNonNull(lectureName, video);

this.moduleCode = moduleCode;
this.lectureName = lectureName;
toAdd = video;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (!model.hasModule(moduleCode)) {
throw new CommandException(String.format(MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
}

ReadOnlyModule module = model.getTracker().getModule(moduleCode);

if (!model.hasLecture(moduleCode, lectureName)) {
throw new CommandException(String.format(MESSAGE_LECTURE_DOES_NOT_EXIST, lectureName, moduleCode));
}

ReadOnlyLecture lecture = module.getLecture(lectureName);

if (model.hasVideo(lecture, toAdd)) {
throw new CommandException(String.format(MESSAGE_DUPLICATE_VIDEO, lectureName, moduleCode));
}

model.addVideo(lecture, toAdd);

return new CommandResult(String.format(MESSAGE_SUCCESS, moduleCode, lectureName, toAdd));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

if (!(other instanceof AddVideoCommand)) {
return false;
}

AddVideoCommand otherCommand = (AddVideoCommand) other;

return lectureName.equals(otherCommand.lectureName)
&& toAdd.equals(otherCommand.toAdd);
}

}
Loading

0 comments on commit b6c755e

Please sign in to comment.