Skip to content

Commit

Permalink
Merge pull request AY2324S1-CS2103T-T13-0#94 from ji-just-ji/Add-Command
Browse files Browse the repository at this point in the history
Add command
  • Loading branch information
rocketninja7 authored Oct 12, 2023
2 parents bcf0ea7 + d3f7a15 commit 4f114dc
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class ClearCommand extends Command {

public static final String COMMAND_WORD = "clear";
public static final String MESSAGE_SUCCESS = "Address book has been cleared!";
public static final String MESSAGE_SUCCESS = "Module list has been cleared!";


@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AddCommand parse(String args) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor( PREFIX_CODE, PREFIX_YEAR, PREFIX_SEMESTER, PREFIX_GRADE);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_CODE, PREFIX_YEAR, PREFIX_SEMESTER, PREFIX_GRADE);
ModuleCode code = ParserUtil.parseCode(argMultimap.getValue(PREFIX_CODE).get());
Year year = ParserUtil.parseYear(argMultimap.getValue(PREFIX_YEAR).get());
Semester semester = ParserUtil.parseSem(argMultimap.getValue(PREFIX_SEMESTER).get());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import seedu.address.model.module.Year;
import seedu.address.model.module.Semester;
import seedu.address.model.module.Grade;
import seedu.address.model.module.Name;
import seedu.address.model.module.ModuleName;
import seedu.address.model.module.Description;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public ObservableList<Module> getModuleList() {
return modules.asUnmodifiableObservableList();
}


@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/model/ReadOnlyAddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import seedu.address.model.module.Module;
import seedu.address.model.person.Person;

import java.util.List;

/**
* Unmodifiable view of an address book
*/
Expand All @@ -14,5 +16,4 @@ public interface ReadOnlyAddressBook {
* This list will not contain any duplicate modules.
*/
ObservableList<Module> getModuleList();

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.module.Module;
import seedu.address.model.person.Person;
import seedu.address.testutil.ModuleBuilder;
import seedu.address.testutil.PersonBuilder;

/**
Expand All @@ -27,22 +29,22 @@ public void setUp() {
}

@Test
public void execute_newPerson_success() {
Person validPerson = new PersonBuilder().build();
public void execute_newModule_success() {
Module validModule = new ModuleBuilder().build();

Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
expectedModel.addPerson(validPerson);
Model expectedModel = new ModelManager((model.getAddressBook()), new UserPrefs());
expectedModel.addModule(validModule);

assertCommandSuccess(new AddCommand(validPerson), model,
String.format(AddCommand.MESSAGE_SUCCESS, Messages.format(validPerson)),
assertCommandSuccess(new AddCommand(validModule), model,
String.format(AddCommand.MESSAGE_SUCCESS, Messages.format(validModule)),
expectedModel);
}

@Test
public void execute_duplicatePerson_throwsCommandException() {
Person personInList = model.getAddressBook().getPersonList().get(0);
assertCommandFailure(new AddCommand(personInList), model,
AddCommand.MESSAGE_DUPLICATE_PERSON);
public void execute_duplicateModule_throwsCommandException() {
Module moduleInList = model.getAddressBook().getModuleList().get(0);
assertCommandFailure(new AddCommand(moduleInList), model,
AddCommand.MESSAGE_DUPLICATE_MODULE);
}

}
102 changes: 51 additions & 51 deletions src/test/java/seedu/address/logic/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalModules.CS2030S;
import static seedu.address.testutil.TypicalPersons.ALICE;

import java.nio.file.Path;
Expand All @@ -22,65 +23,64 @@
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.module.Module;
import seedu.address.model.person.Person;
import seedu.address.testutil.ModuleBuilder;
import seedu.address.testutil.PersonBuilder;

public class AddCommandTest {

@Test
public void constructor_nullPerson_throwsNullPointerException() {
public void constructor_nullModule_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new AddCommand(null));
}

@Test
public void execute_personAcceptedByModel_addSuccessful() throws Exception {
ModelStubAcceptingPersonAdded modelStub = new ModelStubAcceptingPersonAdded();
Person validPerson = new PersonBuilder().build();
public void execute_moduleAcceptedByModel_addSuccessful() throws Exception {
ModelStubAcceptingModuleAdded modelStub = new ModelStubAcceptingModuleAdded();
Module validModule = new ModuleBuilder().build();

CommandResult commandResult = new AddCommand(validPerson).execute(modelStub);
CommandResult commandResult = new AddCommand(validModule).execute(modelStub);

assertEquals(String.format(AddCommand.MESSAGE_SUCCESS, Messages.format(validPerson)),
assertEquals(String.format(AddCommand.MESSAGE_SUCCESS, Messages.format(validModule)),
commandResult.getFeedbackToUser());
assertEquals(Arrays.asList(validPerson), modelStub.personsAdded);
assertEquals(Arrays.asList(validModule), modelStub.modulesAdded );
}

@Test
public void execute_duplicatePerson_throwsCommandException() {
Person validPerson = new PersonBuilder().build();
AddCommand addCommand = new AddCommand(validPerson);
ModelStub modelStub = new ModelStubWithPerson(validPerson);
public void execute_duplicateModule_throwsCommandException() {
Module validModule = new ModuleBuilder().build();
AddCommand addCommand = new AddCommand(validModule);
ModelStub modelStub = new ModelStubWithModule(validModule);

assertThrows(CommandException.class, AddCommand.MESSAGE_DUPLICATE_PERSON, () -> addCommand.execute(modelStub));
assertThrows(CommandException.class, AddCommand.MESSAGE_DUPLICATE_MODULE, () -> addCommand.execute(modelStub));
}


@Test
public void equals() {
Person alice = new PersonBuilder().withName("Alice").build();
Person bob = new PersonBuilder().withName("Bob").build();
AddCommand addAliceCommand = new AddCommand(alice);
AddCommand addBobCommand = new AddCommand(bob);
Module cs2030s = new ModuleBuilder().withCode("CS2030S").build();
Module cs2040s = new ModuleBuilder().withCode("CS2040S").build();
AddCommand add2030Command = new AddCommand(cs2030s);
AddCommand add2040Command = new AddCommand(cs2040s);

// same object -> returns true
assertTrue(addAliceCommand.equals(addAliceCommand));
//same -> returns true
assertTrue(add2030Command.equals(add2030Command));

// same values -> returns true
AddCommand addAliceCommandCopy = new AddCommand(alice);
assertTrue(addAliceCommand.equals(addAliceCommandCopy));
AddCommand add2030CommandCopy = new AddCommand(cs2030s);
assertTrue(add2030Command.equals(add2030CommandCopy));

// different types -> returns false
assertFalse(addAliceCommand.equals(1));
assertFalse(add2030Command.equals(1));
assertFalse(add2030Command.equals(null));

// null -> returns false
assertFalse(addAliceCommand.equals(null));
assertFalse(add2030Command.equals(add2040Command));

// different person -> returns false
assertFalse(addAliceCommand.equals(addBobCommand));
}

@Test
public void toStringMethod() {
AddCommand addCommand = new AddCommand(ALICE);
String expected = AddCommand.class.getCanonicalName() + "{toAdd=" + ALICE + "}";
AddCommand addCommand = new AddCommand(CS2030S);
String expected = AddCommand.class.getCanonicalName() + "{toAdd=" + CS2030S + "}";
assertEquals(expected, addCommand.toString());
}

Expand Down Expand Up @@ -119,7 +119,7 @@ public void setAddressBookFilePath(Path addressBookFilePath) {
}

@Override
public void addPerson(Person person) {
public void addModule(Module module) {
throw new AssertionError("This method should not be called.");
}

Expand All @@ -134,65 +134,65 @@ public ReadOnlyAddressBook getAddressBook() {
}

@Override
public boolean hasPerson(Person person) {
public boolean hasModule(Module module) {
throw new AssertionError("This method should not be called.");
}

@Override
public void deletePerson(Person target) {
public void deleteModule(Module module) {
throw new AssertionError("This method should not be called.");
}

@Override
public void setPerson(Person target, Person editedPerson) {
public void setModule(Module target, Module editedModule) {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Person> getFilteredPersonList() {
public ObservableList<Module> getFilteredModuleList() {
throw new AssertionError("This method should not be called.");
}

@Override
public void updateFilteredPersonList(Predicate<Person> predicate) {
public void updateFilteredModuleList(Predicate<Module> predicate) {
throw new AssertionError("This method should not be called.");
}
}

/**
* A Model stub that contains a single person.
*/
private class ModelStubWithPerson extends ModelStub {
private final Person person;
private class ModelStubWithModule extends ModelStub {
private final Module module;

ModelStubWithPerson(Person person) {
requireNonNull(person);
this.person = person;
ModelStubWithModule(Module module) {
requireNonNull(module);
this.module = module;
}

@Override
public boolean hasPerson(Person person) {
requireNonNull(person);
return this.person.isSamePerson(person);
public boolean hasModule(Module module) {
requireNonNull(module);
return this.module.isSameModule(module);
}
}

/**
* A Model stub that always accept the person being added.
*/
private class ModelStubAcceptingPersonAdded extends ModelStub {
final ArrayList<Person> personsAdded = new ArrayList<>();
private class ModelStubAcceptingModuleAdded extends ModelStub {
final ArrayList<Module> modulesAdded = new ArrayList<>();

@Override
public boolean hasPerson(Person person) {
requireNonNull(person);
return personsAdded.stream().anyMatch(person::isSamePerson);
public boolean hasModule(Module module) {
requireNonNull(module);
return modulesAdded.stream().anyMatch(module::isSameModule);
}

@Override
public void addPerson(Person person) {
requireNonNull(person);
personsAdded.add(person);
public void addModule(Module module) {
requireNonNull(module);
modulesAdded.add(module);
}

@Override
Expand Down
79 changes: 79 additions & 0 deletions src/test/java/seedu/address/testutil/ModuleBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package seedu.address.testutil;


import seedu.address.model.module.Grade;
import seedu.address.model.module.Module;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.module.Semester;
import seedu.address.model.module.Year;
import seedu.address.model.person.Name;

/**
* A utility class to help with building Person objects.
*/
public class ModuleBuilder {
public static final String DEFAULT_CODE = "CS2100";
public static final String DEFAULT_YEAR = "2";
public static final String DEFAULT_SEM = "1";
public static final String DEFAULT_GRADE = "A";

private ModuleCode code;
private Year year;
private Semester semester;
private Grade grade;

/**
* Creates a {@code ModuleBuilder} with the default details
*/
public ModuleBuilder() {
code = new ModuleCode(DEFAULT_CODE);
year = new Year(DEFAULT_YEAR);
semester = new Semester(DEFAULT_SEM);
grade = new Grade(DEFAULT_GRADE);
}

/**
* Initializes the ModuleBuilder with the data of {@code moduleToCopy}.
*/
public ModuleBuilder(Module moduleToCopy) {
code = moduleToCopy.getModuleCode();
year = moduleToCopy.getYearTaken();
semester = moduleToCopy.getSemesterTaken();
grade = moduleToCopy.getGrade();
}
/**
* Sets the {@code moduleCode} of the {@code Module} that we are building.
*/
public ModuleBuilder withCode(String code) {
this.code = new ModuleCode(code);
return this;
}

/**
* Sets the {@code Year} of the {@code Module} that we are building.
*/
public ModuleBuilder withYear(String year) {
this.year = new Year(year);
return this;
}
/**
* Sets the {@code sem} of the {@code Module} that we are building.
*/
public ModuleBuilder withSem(String sem) {
this.semester = new Semester(sem);
return this;
}
/**
* Sets the {@code grade} of the {@code Module} that we are building.
*/
public ModuleBuilder withGrade(String grade) {
this.grade = new Grade(grade);
return this;
}

public Module build() {
return new Module(code, year, semester, grade);
}


}
Loading

0 comments on commit 4f114dc

Please sign in to comment.