Skip to content

Commit

Permalink
Add sample assignment data if file not found
Browse files Browse the repository at this point in the history
  • Loading branch information
currynia committed Oct 17, 2024
1 parent 585e70b commit 96bda94
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
8 changes: 7 additions & 1 deletion src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import seedu.address.model.UserPrefs;
import seedu.address.model.assignment.PredefinedAssignmentsData;
import seedu.address.model.assignment.ReadOnlyPredefinedAssignmentsData;
import seedu.address.model.util.SampleAssignmentsUtil;
import seedu.address.model.util.SampleDataUtil;
import seedu.address.storage.AddressBookStorage;
import seedu.address.storage.JsonAddressBookStorage;
Expand Down Expand Up @@ -92,8 +93,13 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
logger.info("Creating a new data file " + storage.getAddressBookFilePath()
+ " populated with a sample AddressBook.");
}
if (!readAssignmentOptional.isPresent()) {
logger.info("Creating a new data file " + storage.getAssignmentFilePath()
+ " populated with a sample assignment file.");
}
initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
predefinedAssignments = readAssignmentOptional.orElseGet(PredefinedAssignmentsData::new);
predefinedAssignments = readAssignmentOptional
.orElseGet(SampleAssignmentsUtil::getSamplePredefinedAssignments);
} catch (DataLoadingException e) {
logger.warning("Data file at " + storage.getAddressBookFilePath() + " could not be loaded."
+ " Will be starting with an empty AddressBook.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,14 @@ public String getAssignmentName(String name) {
}
return null;
}

/**
* Adds a predefined assignment to predefinedAssignmentArrayList.
* @param p The predefined assignment to add.
* @return The predefinedAssignmentArrayList.
*/
public ArrayList<PredefinedAssignment> add(PredefinedAssignment p) {
predefinedAssignmentArrayList.add(p);
return predefinedAssignmentArrayList;
}
}
33 changes: 33 additions & 0 deletions src/main/java/seedu/address/model/util/SampleAssignmentsUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.address.model.util;

import seedu.address.model.assignment.PredefinedAssignment;
import seedu.address.model.assignment.PredefinedAssignmentsData;
import seedu.address.model.assignment.ReadOnlyPredefinedAssignmentsData;

/**
* Contains utility methods for populating
* {@code PredefinedAssignmentsData} with sample data.
*/
public class SampleAssignmentsUtil {
public static PredefinedAssignment[] getSampleAssignments() {
return new PredefinedAssignment[]{
new PredefinedAssignment("Ex01", 10f),
new PredefinedAssignment("Ex02", 10f),
new PredefinedAssignment("Ex03", 25f)
};
}

/**
* Returns predefined assignments database populated with the sample assignments.
*
* @return The sample predefined assignment database.
*/
public static ReadOnlyPredefinedAssignmentsData getSamplePredefinedAssignments() {
PredefinedAssignmentsData samplePredefinedAssignment =
new PredefinedAssignmentsData();
for (PredefinedAssignment sample : getSampleAssignments()) {
samplePredefinedAssignment.add(sample);
}
return samplePredefinedAssignment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,29 @@
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.assignment.PredefinedAssignment;
import seedu.address.model.assignment.PredefinedAssignmentsData;
import seedu.address.model.person.Person;
import seedu.address.model.util.SampleAssignmentsUtil;
import seedu.address.testutil.TypicalPersons;

public class AddGradeCommandTest {
private final Model model = new ModelManager(
getTypicalAddressBook(),
new UserPrefs(),
AddGradeCommandTest.getPredefinedAssignmentsData());


public static PredefinedAssignmentsData getPredefinedAssignmentsData() {
PredefinedAssignmentsData predefinedAssignmentsData = new PredefinedAssignmentsData();
predefinedAssignmentsData.addPredefinedAssignment(new PredefinedAssignment("Ex01", 10.0f));
predefinedAssignmentsData.addPredefinedAssignment(new PredefinedAssignment("Ex02", 10.0f));
predefinedAssignmentsData.addPredefinedAssignment(new PredefinedAssignment("Ex09", 10.0f));
return predefinedAssignmentsData;
}

getTypicalAddressBook(),
new UserPrefs(),
SampleAssignmentsUtil.getSamplePredefinedAssignments());


@Test
public void constructor_nullAssignmentFormat_throwsNullPointerException() {

assertThrows(NullPointerException.class, () -> new AddGradeCommand(null, 0f, null));
}

@Test
public void assignment_invalidName() {
AddGradeCommand command = new AddGradeCommand("John Doe", 0f, "ex10");
assertThrows(CommandException.class, () -> command.execute(model));
}

@Test
public void person_invalidName() {
AddGradeCommand command = new AddGradeCommand("John DoeDoedoe", 0f, "ex01");
Expand All @@ -52,14 +43,14 @@ public void person_invalidName() {
@Test
public void assignment_invalidHighScore() {
AddGradeCommand command = new AddGradeCommand("John Doe",
100f, "ex01");
100f, "ex01");
assertThrows(CommandException.class, () -> command.execute(model));
}

@Test
public void assignment_invalidLowScore() {
AddGradeCommand command = new AddGradeCommand("John Doe",
-1f, "ex01");
-1f, "ex01");
assertThrows(CommandException.class, () -> command.execute(model));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package seedu.address.model.assignment;

import static org.junit.jupiter.api.Assertions.assertEquals;

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

import org.junit.jupiter.api.Test;


class PredefinedAssignmentsDataTest {

@Test
public void addTest() {
PredefinedAssignment sample = new PredefinedAssignment("Ex01", 0f);
PredefinedAssignmentsData data = new PredefinedAssignmentsData();
ArrayList<PredefinedAssignment> expected = new ArrayList<>(List.of(sample));
assertEquals(data.add(sample), expected);
}
}

0 comments on commit 96bda94

Please sign in to comment.