diff --git a/oop/code/oop/pom.xml b/oop/code/oop/pom.xml index e231f94..68b2ca2 100644 --- a/oop/code/oop/pom.xml +++ b/oop/code/oop/pom.xml @@ -25,6 +25,12 @@ 4.11 test + + org.projectlombok + lombok + 1.18.24 + provided + diff --git a/oop/code/oop/src/main/java/com/scaler/lld/scaler/Student.java b/oop/code/oop/src/main/java/com/scaler/lld/scaler/Student.java new file mode 100644 index 0000000..b41f93f --- /dev/null +++ b/oop/code/oop/src/main/java/com/scaler/lld/scaler/Student.java @@ -0,0 +1,33 @@ +package com.scaler.lld.scaler; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +public class Student extends User { + + private String batchName; + private Integer psp; + private StudentStatus status = StudentStatus.ACTIVE; // ACTIVE, PAUSED, COMPLETED + + // Define parametrised constructor + + public Student(String name, String email, String batchName, Integer psp) { + super(name, email); + this.batchName = batchName; + + if (psp < 0 || psp > 100) { + throw new IllegalArgumentException("PSP should be between 0 and 100"); + } + this.psp = psp; + } + + public void changeBatch(String batchName) { + this.batchName = batchName; + } + + +} \ No newline at end of file diff --git a/oop/code/oop/src/main/java/com/scaler/lld/scaler/StudentStatus.java b/oop/code/oop/src/main/java/com/scaler/lld/scaler/StudentStatus.java new file mode 100644 index 0000000..4d21c4a --- /dev/null +++ b/oop/code/oop/src/main/java/com/scaler/lld/scaler/StudentStatus.java @@ -0,0 +1,5 @@ +package com.scaler.lld.scaler; + +public enum StudentStatus { + ACTIVE, PAUSED, COMPLETED, EDGE +} diff --git a/oop/code/oop/src/main/java/com/scaler/lld/scaler/User.java b/oop/code/oop/src/main/java/com/scaler/lld/scaler/User.java new file mode 100644 index 0000000..ce6e403 --- /dev/null +++ b/oop/code/oop/src/main/java/com/scaler/lld/scaler/User.java @@ -0,0 +1,19 @@ +package com.scaler.lld.scaler; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class User { + private String name; + private String email; + + public void changeEmail(String email) { + this.email = email; + } +} diff --git a/oop/code/oop/src/test/java/com/scaler/lld/scaler/StudentTest.java b/oop/code/oop/src/test/java/com/scaler/lld/scaler/StudentTest.java new file mode 100644 index 0000000..2b91ad6 --- /dev/null +++ b/oop/code/oop/src/test/java/com/scaler/lld/scaler/StudentTest.java @@ -0,0 +1,50 @@ +package com.scaler.lld.scaler; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +public class StudentTest { + + @Test + public void testDefaultCtor() { + Student student = new Student(); + + assertNotNull("If the ctor is called, then object must be returned", + student); + assertNull("If default ctor is used, name should be null", student.getName()); + assertEquals("If default ctor is used, status should be active", StudentStatus.ACTIVE, student.getStatus()); + } + + @Test + public void testParametrisedCtor() { + Student student = new Student("John Doe", "john@doe.in", "Batch 1", 100); + + assertNotNull("If the ctor is called, then object must be returned", + student); + assertEquals("If name ctor is passed to ctor, name should be set in instance", "John Doe", student.getName()); + assertEquals("If status is not passed to ctor, status should be active", StudentStatus.ACTIVE, + student.getStatus()); + } + + @Test(expected = IllegalArgumentException.class) + public void testParametrisedCtorWithInvalidPsp() { + new Student("John Doe", "john@doe.in", "Batch 1", 101); + } + + @Test + public void testParentFields() { + Student student = new Student("John Doe", "john@doe.in", "Batch 1", 100); + assertEquals("If name ctor is passed to ctor, name should be set in instance", "John Doe", student.getName()); + + String newEmail = "john@doe.com"; + student.changeEmail(newEmail); + assertEquals("If email is changed, then email should be updated", newEmail, student.getEmail()); + } + +} + +// Coverage - % of code your test cases cover - 80% +// pytest > junit