Skip to content

Commit

Permalink
Remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
seraphimstreets committed Nov 14, 2023
1 parent 0b99ee4 commit e96ac98
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import seedu.modulight.logic.Messages;
import seedu.modulight.logic.commands.exceptions.CommandException;
import seedu.modulight.model.Model;
import seedu.modulight.model.gradedcomponent.GcName;
import seedu.modulight.model.gradedcomponent.GradedComponent;
import seedu.modulight.model.gradedcomponent.model.GradedComponentBook;
import seedu.modulight.model.student.Student;
import seedu.modulight.model.student.StudentId;
import seedu.modulight.model.student.model.StudentBook;
import seedu.modulight.model.studentscore.StudentScore;

Expand Down Expand Up @@ -65,14 +67,17 @@ public CommandResult execute(Model model) throws CommandException {
StudentBook studentBook = model.getStudentBook();

float weightageToAdd = toAdd.getWeightage().weightage;
if (ModelUtil.weightageSum(gradedComponentBook) + weightageToAdd > 100) {
float curWeightageSum = ModelUtil.weightageSum(gradedComponentBook);
if (curWeightageSum + weightageToAdd > 100) {
throw new CommandException(MESSAGE_WEIGHTAGES_MORE_THAN_100);
}
gradedComponentBook.addGradedComponent(toAdd);
List<Student> students = studentBook.getStudentList();

for (Student student : students) {
StudentScore sc = new StudentScore(student.getStudentId(), toAdd.getName(), 0);
GcName name = toAdd.getName();
StudentId sid = student.getStudentId();
StudentScore sc = new StudentScore(sid, name, 0);
addCommandUpdateLinks(student, toAdd, sc);
addCommandUpdateBooks(model, student, toAdd, sc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,18 @@ public AddGradedComponentCommand parse(String args) throws ParseException {
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_COMPONENT_NAME, PREFIX_WEIGHTAGE, PREFIX_MAX_MARKS);
GradedComponent gc = null;
try {
GcName name = new GcName(argMultimap.getValue(PREFIX_COMPONENT_NAME).get());
String weightageString = argMultimap.getValue(PREFIX_WEIGHTAGE).get();
String maxMarksString = argMultimap.getValue(PREFIX_MAX_MARKS).get();

checkStringParsableToDouble(weightageString, "Weightage");
checkStringParsableToDouble(maxMarksString, "Maximum marks");
Weightage weightage = new Weightage(Float.parseFloat(weightageString));
MaxMarks maxMarks = new MaxMarks(Float.parseFloat(maxMarksString));
gc = new GradedComponent(name, maxMarks, weightage);
} catch (IllegalArgumentException e) {
throw new ParseException(e.getMessage());
}

String nameString = argMultimap.getValue(PREFIX_COMPONENT_NAME).get();
String weightageString = argMultimap.getValue(PREFIX_WEIGHTAGE).get();
String maxMarksString = argMultimap.getValue(PREFIX_MAX_MARKS).get();
GcName name = ParserUtil.parseGcName(nameString);
Weightage weightage = ParserUtil.parseWeightage(weightageString);
MaxMarks maxMarks = ParserUtil.parseMaxMarks(maxMarksString);

GradedComponent gc = new GradedComponent(name, maxMarks, weightage);
return new AddGradedComponentCommand(gc);
}

private static void checkStringParsableToDouble(String s, String fieldName) throws ParseException {
try {
Double.parseDouble(s);
} catch (NumberFormatException e) {
throw new ParseException(String.format("%s needs to be a number", fieldName));
}
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/seedu/modulight/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public static StudentName parseName(String name) throws ParseException {
public static GcName parseGcName(String name) throws ParseException {
requireNonNull(name);
String trimmedName = name.trim();
if (!StudentName.isValidName(trimmedName)) {
throw new ParseException(StudentName.MESSAGE_CONSTRAINTS);
if (!GcName.isValidName(trimmedName)) {
throw new ParseException(GcName.MESSAGE_CONSTRAINTS);
}
return new GcName(trimmedName);
}
Expand Down Expand Up @@ -160,7 +160,10 @@ public static StudentGrade parseStudentGrade(String studentGrade) throws ParseEx
* If not, throws a parse error.
*/
public static Weightage parseWeightage(String s) throws ParseException {
checkStringParsableToFloat(s);
checkStringParsableToFloat(s, "Weightage");
if (!Weightage.isValidWeightage(Float.parseFloat(s))) {
throw new ParseException(Weightage.MESSAGE_CONSTRAINTS);
}
return new Weightage(Float.parseFloat(s));
}

Expand All @@ -169,7 +172,10 @@ public static Weightage parseWeightage(String s) throws ParseException {
* If not, throws an error with {@code varName}
*/
public static MaxMarks parseMaxMarks(String s) throws ParseException {
checkStringParsableToFloat(s);
checkStringParsableToFloat(s, "Max marks");
if (!MaxMarks.isValidMarks(Float.parseFloat(s))) {
throw new ParseException(MaxMarks.MESSAGE_CONSTRAINTS);
}
return new MaxMarks(Float.parseFloat(s));
}

Expand All @@ -180,23 +186,16 @@ public static MaxMarks parseMaxMarks(String s) throws ParseException {
* @throws ParseException if not Parsable
*/
public static float parseScore(String s) throws ParseException {
checkStringParsableToFloat(s);
checkStringParsableToFloat(s, "Score");
return Float.parseFloat(s);
}

private static void checkStringParsableToDouble(String s) throws ParseException {
try {
Double.parseDouble(s);
} catch (NumberFormatException e) {
throw new ParseException("A value could not be parsed into a number.");
}
}

private static void checkStringParsableToFloat(String s) throws ParseException {
private static void checkStringParsableToFloat(String s, String fieldName) throws ParseException {
try {
Float.parseFloat(s);
} catch (NumberFormatException e) {
throw new ParseException("A value could not be parsed into a number.");
throw new ParseException(String.format("%s needs to be a number.", fieldName));
}
}

Expand Down

0 comments on commit e96ac98

Please sign in to comment.