Custom Constructors in Records (#13515)

This commit is contained in:
hajarrs 2023-03-08 03:22:24 +01:00 committed by GitHub
parent bcd02ae908
commit f1dc5c2d60
5 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,61 @@
package org.example;
import java.util.Objects;
public class StudentClassic {
private String name;
private int rollNo;
private int marks;
public StudentClassic(String name, int rollNo, int marks) {
this.name = name;
this.rollNo = rollNo;
this.marks = marks;
}
public String getName() {
return name;
}
public int getRollNo() {
return rollNo;
}
public int getMarks() {
return marks;
}
public void setName(String name) {
this.name = name;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public void setMarks(int marks) {
this.marks = marks;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StudentClassic that = (StudentClassic) o;
return rollNo == that.rollNo && marks == that.marks && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name, rollNo, marks);
}
@Override
public String toString() {
return "StudentClassic{" +
"name='" + name + '\'' +
", rollNo=" + rollNo +
", marks=" + marks +
'}';
}
}

View File

@ -0,0 +1,19 @@
package org.example;
import java.util.Comparator;
import java.util.List;
public record StudentRecord(String name, int rollNo, int marks) {
public StudentRecord(String name){
this(name, 0,0);
}
public StudentRecord {
if (marks < 0 || marks > 100) {
throw new IllegalArgumentException("Marks should be between 0 and 100.");
}
if (name == null) {
throw new IllegalArgumentException("Name cannot be null");
}
}
}

View File

@ -0,0 +1,22 @@
package org.example;
record StudentRecordV2(String name, int rollNo, int marks, char grade) {
public StudentRecordV2(String name, int rollNo, int marks) {
this(name, rollNo, marks, calculateGrade(marks));
}
private static char calculateGrade(int marks) {
if (marks >= 90) {
return 'A';
} else if (marks >= 80) {
return 'B';
} else if (marks >= 70) {
return 'C';
} else if (marks >= 60) {
return 'D';
} else {
return 'F';
}
}
}

View File

@ -0,0 +1,11 @@
package org.example;
import java.util.UUID;
record StudentRecordV3(String name, int rollNo, int marks, String id) {
public StudentRecordV3(String name, int rollNo, int marks) {
this(name, rollNo, marks, UUID.randomUUID().toString());
}
}

View File

@ -0,0 +1,37 @@
import org.example.StudentRecord;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class StudentRecordJUnitTest {
@Test
public void givenStudentRecordData_whenCreated_thenStudentPropertiesMatch() {
StudentRecord s1 = new StudentRecord("John", 1, 90);
StudentRecord s2 = new StudentRecord("Jane", 2, 80);
assertEquals("John", s1.name());
assertEquals(1, s1.rollNo());
assertEquals(90, s1.marks());
assertEquals("Jane", s2.name());
assertEquals(2, s2.rollNo());
assertEquals(80, s2.marks());
}
@Test
public void givenStudentRecordsList_whenSortingDataWithName_thenStudentsSorted(){
List<StudentRecord> studentRecords = List.of(
new StudentRecord("Dana", 1, 85),
new StudentRecord("Jim", 2, 90),
new StudentRecord("Jane", 3, 80)
);
List<StudentRecord> mutableStudentRecords = new ArrayList<>(studentRecords);
mutableStudentRecords.sort(Comparator.comparing(StudentRecord::name));
List<StudentRecord> sortedStudentRecords = List.copyOf(mutableStudentRecords);
assertEquals("Jane", sortedStudentRecords.get(1).name());
}
}