Skip to content

Commit

Permalink
Merge pull request #53 from saiutkarsh33/branch-SetCourseCommand
Browse files Browse the repository at this point in the history
Branch set course command
  • Loading branch information
tituschewxj authored Mar 21, 2024
2 parents 103adaa + 1bdf9e8 commit 71c613d
Show file tree
Hide file tree
Showing 48 changed files with 1,200 additions and 73 deletions.
36 changes: 25 additions & 11 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
import seedu.address.logic.Logic;
import seedu.address.logic.LogicManager;
import seedu.address.model.AddressBook;
import seedu.address.model.CourseName;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.ReadOnlyCourseName;
import seedu.address.model.UserPrefs;
import seedu.address.model.util.SampleDataUtil;
import seedu.address.storage.AddressBookStorage;
import seedu.address.storage.CourseStorageName;
import seedu.address.storage.JsonAddressBookStorage;
import seedu.address.storage.JsonCourseNameStorage;
import seedu.address.storage.JsonUserPrefsStorage;
import seedu.address.storage.Storage;
import seedu.address.storage.StorageManager;
Expand Down Expand Up @@ -58,39 +61,50 @@ public void init() throws Exception {
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
storage = new StorageManager(addressBookStorage, userPrefsStorage);
CourseStorageName courseStorageName = new JsonCourseNameStorage(userPrefs.getCourseNameFilePath());
storage = new StorageManager(addressBookStorage, userPrefsStorage, courseStorageName);

model = initModelManager(storage, userPrefs);

logic = new LogicManager(model, storage);

ui = new UiManager(logic);
ui = new UiManager(logic, model, storage);
}

/**
* Returns a {@code ModelManager} with the data from {@code storage}'s address book and {@code userPrefs}. <br>
* The data from the sample address book will be used instead if {@code storage}'s address book is not found,
* or an empty address book will be used instead if errors occur when reading {@code storage}'s address book.
*/
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
private Model initModelManager(Storage storage, UserPrefs userPrefs) {
logger.info("Using data file : " + storage.getAddressBookFilePath());

Optional<ReadOnlyAddressBook> addressBookOptional;
logger.info("Using course data file : " + storage.getCourseNameFilePath());
ReadOnlyAddressBook initialData;
ReadOnlyCourseName initialCourseNameData;
try {
addressBookOptional = storage.readAddressBook();
if (!addressBookOptional.isPresent()) {
Optional<ReadOnlyAddressBook> addressBookOptional = storage.readAddressBook();
if (addressBookOptional.isEmpty()) {
logger.info("Creating a new data file " + storage.getAddressBookFilePath()
+ " populated with a sample AddressBook.");
}
Optional<ReadOnlyCourseName> courseNameOptional = storage.readCourse();

if (courseNameOptional.isEmpty()) {
logger.info("Creating a new course data file " + storage.getCourseNameFilePath()
+ " populated with a course.");
}

initialCourseNameData = courseNameOptional.orElseGet(SampleDataUtil::getSampleCourseName);
initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
} catch (DataLoadingException e) {
logger.warning("Data file at " + storage.getAddressBookFilePath() + " could not be loaded."
+ " Will be starting with an empty AddressBook.");
// Room to add logging for course name file here too
initialData = new AddressBook();
initialCourseNameData = new CourseName();
}

return new ModelManager(initialData, userPrefs);
return new ModelManager(initialData, userPrefs, initialCourseNameData);
}

private void initLogging(Config config) {
Expand All @@ -117,7 +131,7 @@ protected Config initConfig(Path configFilePath) {

try {
Optional<Config> configOptional = ConfigUtil.readConfig(configFilePathUsed);
if (!configOptional.isPresent()) {
if (configOptional.isEmpty()) {
logger.info("Creating new config file " + configFilePathUsed);
}
initializedConfig = configOptional.orElse(new Config());
Expand Down Expand Up @@ -148,7 +162,7 @@ protected UserPrefs initPrefs(UserPrefsStorage storage) {
UserPrefs initializedPrefs;
try {
Optional<UserPrefs> prefsOptional = storage.readUserPrefs();
if (!prefsOptional.isPresent()) {
if (prefsOptional.isEmpty()) {
logger.info("Creating new preference file " + prefsFilePath);
}
initializedPrefs = prefsOptional.orElse(new UserPrefs());
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyCourseName;
import seedu.address.model.person.Person;

/**
Expand Down Expand Up @@ -47,4 +49,16 @@ public interface Logic {
* Set the user prefs' GUI settings.
*/
void setGuiSettings(GuiSettings guiSettings);

/**
* Returns the CourseName.
*
* @see Model#getCourseName()
*/
ReadOnlyCourseName getCourseName();

/**
* Returns the user prefs' courseName file path.
*/
Path getCourseNameFilePath();
}
16 changes: 14 additions & 2 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyCourseName;
import seedu.address.model.person.Person;
import seedu.address.storage.Storage;

Expand Down Expand Up @@ -46,12 +47,13 @@ public LogicManager(Model model, Storage storage) {
public CommandResult execute(String commandText) throws CommandException, ParseException {
logger.info("----------------[USER COMMAND][" + commandText + "]");

CommandResult commandResult;

Command command = addressBookParser.parseCommand(commandText);
commandResult = command.execute(model);
CommandResult commandResult = command.execute(model);

try {
storage.saveAddressBook(model.getAddressBook());
storage.saveCourse(model.getCourseName());
} catch (AccessDeniedException e) {
throw new CommandException(String.format(FILE_OPS_PERMISSION_ERROR_FORMAT, e.getMessage()), e);
} catch (IOException ioe) {
Expand Down Expand Up @@ -85,4 +87,14 @@ public GuiSettings getGuiSettings() {
public void setGuiSettings(GuiSettings guiSettings) {
model.setGuiSettings(guiSettings);
}

@Override
public ReadOnlyCourseName getCourseName() {
return model.getCourseName();
}

@Override
public Path getCourseNameFilePath() {
return model.getCourseNameFilePath();
}
}
73 changes: 73 additions & 0 deletions src/main/java/seedu/address/logic/commands/SetCourseCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package seedu.address.logic.commands;

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.model.course.Course.isValidCode;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.commands.util.CommandMessageUsageUtil;
import seedu.address.model.Model;
import seedu.address.model.course.Course;



/**
* Represents a command to set the current course in the application.
* This command updates the application's state to reflect the specified course.
* The command follows a specific format for the course code, which should match "XX1234Y",
* where "Y" is optional. It is designed to handle the action triggered by the user input
* corresponding to setting a new course within the application.
*/
public class SetCourseCommand extends Command {
public static final String COMMAND_WORD = "setcrs";

public static final String MESSAGE_ARGUMENTS = "Course: %1$s";

public static final String MESSAGE_CONSTRAINTS =
"Course code should follow the format \"XX1234Y\", Y is optional";

public static final String MESSAGE_USAGE = CommandMessageUsageUtil.generateMessageUsage(COMMAND_WORD,
"Set Course name ", "course code", COMMAND_WORD + " CS2104");

public static final String MESSAGE_SUCCESS = "Successfully updated course name";


private final Course course;

/**
* Creates a SetCourseCommand with the specified course.
* This constructor initializes a new instance of SetCourseCommand with a given Course object.
* It requires the Course object to be non-null and expects it to adhere to the defined course
* code constraints outlined.
*
* @param course The course to be set by this command. Must not be null and should take the courses
*/
public SetCourseCommand(Course course) {
requireAllNonNull(course);
this.course = course;
}

@Override
public CommandResult execute(Model model) throws CommandException {
if (!isValidCode(course.toString())) {
throw new CommandException(MESSAGE_CONSTRAINTS);
}
model.changeCode(course.toString());
return new CommandResult(MESSAGE_SUCCESS);
}

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

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

SetCourseCommand e = (SetCourseCommand) other;
return course.equalsIgnoreCase(e.course);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListPersonCommand;
import seedu.address.logic.commands.MarkAttendanceCommand;
import seedu.address.logic.commands.SetCourseCommand;
import seedu.address.logic.commands.UnmarkAttendanceCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand Down Expand Up @@ -75,10 +76,12 @@ public Command parseCommand(String userInput) throws ParseException {

case ExitCommand.COMMAND_WORD:
return new ExitCommand();

case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case SetCourseCommand.COMMAND_WORD:
return new SetCourseCommandParser().parse(arguments);

case MarkAttendanceCommand.COMMAND_WORD:
return new MarkAttendanceCommandParser().parse(arguments);

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.course.Course;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand Down Expand Up @@ -112,6 +113,21 @@ public static NusNet parseNusNet(String nusNet) throws ParseException {
return new NusNet(trimmedNusNet);
}

/**
* Parses a {@code String code} into a {@code Course}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code code} is invalid.
*/
public static Course parseCourse(String code) throws ParseException {
requireNonNull(code);
String trimmedCode = code.trim().toUpperCase();
if (!Tag.isValidTagName(trimmedCode)) {
throw new ParseException(Course.MESSAGE_CONSTRAINTS);
}
return new Course(trimmedCode);
}

/**
* Parses a {@code String weekNumber} into a {@code WeekNumber}.
* Leading and trailing whitespaces will be trimmed.
Expand Down Expand Up @@ -157,6 +173,8 @@ public static Tag parseTag(String tag) throws ParseException {
return new Tag(trimmedTag);
}



/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.SetCourseCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.course.Course;

/**
* Parses input arguments and creates a new {@code SetCourseCommand} object.
*/
public class SetCourseCommandParser implements Parser<SetCourseCommand> {
/**
* Parses the given {@code String} of arguments in the context of the {@code RemarkCommand}
* and returns a {@code RemarkCommand} object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public SetCourseCommand parse(String args) throws ParseException {
requireNonNull(args);

Course course;
try {
course = ParserUtil.parseCourse(args);
} catch (IllegalValueException ive) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
SetCourseCommand.MESSAGE_CONSTRAINTS), ive);
}

return new SetCourseCommand(course);
}
}
Loading

0 comments on commit 71c613d

Please sign in to comment.