Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • CW1
  • master
  • CW1-submission
3 results

Target

Select target project
  • wmis228/cs2800-lab-2024
1 result
Select Git revision
  • CW1
  • master
  • CW1-submission
3 results
Show changes
Commits on Source (3)
Showing
with 364 additions and 0 deletions
package uk.ac.rhul.cs2800.model;
/**
* This class represents a grade with both a numeric score and a module it belongs to.
*/
public class Grade {
private int score;
private Module module;
/**
* Constructs a Grade with the score and module.
* score of the grade module associated with the grade
*/
public Grade(int score, Module module) {
this.score = score;
this.module = module;
}
public int getScore() {
return score;
}
public Module getModule() {
return module;
}
public void setScore(int score) {
this.score = score;
}
}
package uk.ac.rhul.cs2800.model;
/**
* Represents a module with its code, name, and mandatory status.
*/
public class Module {
private String code;
private String name;
private boolean nmc;
/**
* Defines a module, including its code, name, and whether it's a mandatory module.
*/
public Module(String code, String name, boolean nmc) {
this.code = code;
this.name = name;
this.nmc = nmc;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
public boolean isNmc() {
return nmc;
}
}
package uk.ac.rhul.cs2800.model;
/**
* Registers a student to a specified module.
*/
public class Registration {
private Student student;
private Module module;
/**
* Registers a student to a specified module.
*/
public Registration(Student student, Module module) {
this.student = student;
this.module = module;
}
public Student getStudent() {
return student;
}
public Module getModule() {
return module;
}
}
package uk.ac.rhul.cs2800.model;
import java.util.ArrayList;
import java.util.List;
import uk.ac.rhul.cs2800.model.exception.NoGradeAvailableException;
/**
* Represents a student with personal and academic information.
*/
public class Student {
private Long id;
private String grade;
private String firstName;
private String lastName;
private String username;
private String email;
private List<Grade> grades = new ArrayList<>();
private List<Module> modules = new ArrayList<>();
/**
* Represents a student with basic identifying information. Each student has a unique ID, personal
* details, and contact information.
*/
public Student(Long id, String firstName, String lastName, String username, String email) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.username = username;
this.email = email;
}
/**
* Adds a grade to the student's list of grades.
*
* @param g the grade to add
*/
public void addGrade(Grade g) {
grades.add(g);
}
/**
* Adds a grade to the student's list of grades.
*
*
*/
public Grade getGrade(Module m) throws NoGradeAvailableException {
for (Grade grade : grades) {
if (grade.getModule().equals(m)) {
return grade;
}
}
throw new NoGradeAvailableException("No grade available for module: " + m.getName());
}
/**
* Adds a grade to the student's list of grades.
*
* @param m the grade to add
*/
public void registerModule(Module m) {
modules.add(m);
}
/**
* 0 if grade is empty.
*/
public float computeAverage() {
if (grades.isEmpty()) {
return 0;
}
int total = 0;
for (Grade grade : grades) {
total += grade.getScore();
}
return (float) total / grades.size();
}
public List<Module> getModules() {
return modules;
}
}
package uk.ac.rhul.cs2800.model.exception;
/**
* Custom exception used when a grade is unavailable or missing. Includes a message to explain the
* reason.
*/
public class NoGradeAvailableException extends Exception {
private static final long serialVersionUID = 1L;
/**
* Custom exception to handle cases when registration cannot be completed. The message provides
* further context.
*/
public NoGradeAvailableException(String message) {
super(message);
}
}
package uk.ac.rhul.cs2800.model.exception;
/**
* Creates an association between a student and a module for registration purposes.
*/
public class NoRegistrationException extends Exception {
private static final long serialVersionUID = 1L;
/**
* Registers a student to a specified module.
*/
public NoRegistrationException(String message) {
super(message);
}
}
package uk.ac.rhul.cs2800.model;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class GradeTest {
@Test
void testGradeInitialization() {
Module module = new Module("CS2800", "CS4600", false);
Grade grade = new Grade(50, module);
equals(50);
assertEquals(module, grade.getModule());
}
@Test
void testSetScore() {
Grade grade = new Grade(50, null);
grade.setScore(95);
assertEquals(95, grade.getScore());
}
}
package uk.ac.rhul.cs2800.model;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.jupiter.api.Test;
class ModuleTest {
@Test
void testModuleInitialization() {
Module module = new Module("CS2800", "ComputerScience", false);
assertEquals("CS2800", module.getCode());
assertEquals("ComputerScience", module.getName());
assertFalse(module.isNmc());
}
}
package uk.ac.rhul.cs2800.model.exception;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class NoGradeAvailableExceptionTest {
@Test
void testExceptionMessage() {
NoGradeAvailableException exception = new NoGradeAvailableException("No grade found.");
assertEquals("No grade found.", exception.getMessage());
}
}
package uk.ac.rhul.cs2800.model.exception;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class NoRegistrationExceptionTest {
@Test
void testExceptionMessage() {
NoRegistrationException exception = new NoRegistrationException("No registration found.");
assertEquals("No registration found.", exception.getMessage());
}
}
package uk.ac.rhul.cs2800.model;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class RegistrationTest {
@Test
void testRegistrationInitialization() {
Student student = new Student(1L, "Anna", "Angelina", "Alina", "Izzy");
Module module = new Module("CS2800", "ComputerScience", false);
Registration registration = new Registration(student, module);
assertEquals(student, registration.getStudent());
assertEquals(module, registration.getModule());
}
}
package uk.ac.rhul.cs2800.model;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import uk.ac.rhul.cs2800.model.exception.NoGradeAvailableException;
class StudentTest {
private Student student;
private Module module;
private Grade grade;
@BeforeEach
void setUp() {
student = new Student(1L, "Anna", "Angelina", "Alina", "Izzy");
module = new Module("CS2800", "ComputerScience", false);
grade = new Grade(85, module);
}
@Test
void testAddGrade() throws NoGradeAvailableException {
student.addGrade(grade);
assertEquals(grade, student.getGrade(module));
}
@Test
void testComputeAverage() {
student.addGrade(new Grade(50, module));
student.addGrade(new Grade(90, new Module("CS2800", "ComputerScience", false)));
assertEquals(70.0, student.computeAverage(), 0.01);
}
@Test
void testGetGradeThrowsExceptionWhenNoGrade() {
Module newModule = new Module("CS2800", "ComputerScience", false);
assertThrows(NoGradeAvailableException.class, () -> student.getGrade(newModule));
}
@Test
void testRegisterModule() {
student.registerModule(module);
assertTrue(student.getModules().contains(module));
}
}
This is my project
for CS2800
CW1