Getters and Setters in Java

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package persistetnce2; import java.io.Serializable; import java.util.ArrayList; /** * * @author Antoun */ public class Course implements Serializable{ private String name; private ArrayList students; Course(){ students = new ArrayList<>(); } public Course(String name) { students = new ArrayList<>(); this.name = name; } public void setName(String name) { this.name = name; } public void setStudents(ArrayList students) { this.students = students; } public String getName() { return name; } public ArrayList getStudents() { return students; } public void addStudent(Student x){ this.students.add(x); } @Override public boolean equals(Object obj) { Course x = (Course)obj; if(x.students.size() != this.students.size()){ return false; } int i =0 ; for (Student student : students) { if(!student.equals(x.students.get(i++))){ return false; } } return name.equals(x.name); } public double meanGPA(){ if(this.students.isEmpty()){ return 0; } double res = 0.0; for(Student s: this.students){ res+=s.getGPA(); } return res / this.students.size(); } } CoursePersister.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package persistetnce2; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Antoun */ public class CoursePersister { private byte[] toStream(Course s) { byte[] stream = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(s); stream = baos.toByteArray(); } catch (IOException ex) { Logger.getLogger(StudentPersister.class.getName()).log(Level.SEVERE, null, ex); return null; } return stream; } private byte[] toStream(ArrayList sArr) { byte[] stream = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(sArr); stream = baos.toByteArray(); } catch (IOException ex) { Logger.getLogger(StudentPersister.class.getName()).log(Level.SEVERE, null, ex); return null; } return stream; } private Course toCourse(byte[] stream) { Course s = null; try { ByteArrayInputStream bais = new ByteArrayInputStream(stream); ObjectInputStream ois = new ObjectInputStream(bais); s = (Course) ois.readObject(); } catch (IOException ex) { return null; } catch (ClassNotFoundException ex) { return null; } return s; } private ArrayList toCourses(byte[] stream) { ArrayList Courses = null; try { ByteArrayInputStream bais = new ByteArrayInputStream(stream); ObjectInputStream ois = new ObjectInputStream(bais); Courses = (ArrayList) ois.readObject(); } catch (IOException ex) { Logger.getLogger(StudentPersister.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (ClassNotFoundException ex) { Logger.getLogger(StudentPersister.class.getName()).log(Level.SEVERE, null, ex); return null; } return Courses; } private boolean toFile(String filename,byte[]arr){ FileOutputStream fos; try { fos = new FileOutputStream(filename); fos.write(arr); fos.close(); } catch (FileNotFoundException ex) { Logger.getLogger(CoursePersister.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (IOException ex) { Logger.getLogger(CoursePersister.class.getName()).log(Level.SEVERE, null, ex); return false; } return true; } private byte[] fromFile(String filename){ try { return Files.readAllBytes(Paths.get(filename)); }catch(EOFException ex){ return null; } catch (IOException ex) { return null; } } public boolean convertCourseToFile(String filename, Course c){ byte[]arr = this.toStream(c); if(arr == null) return false; return this.toFile(filename, arr); } public boolean convertCoursesToFile(String filename, ArrayList c){ byte[]arr = this.toStream(c); if(arr == null){ return false; } return this.toFile(filename,arr); } public Course convertCourseFromFile(String filename){ byte[] arr =this.fromFile(filename); if(arr == null){ return null; } return this.toCourse(arr); } public ArrayList convertCoursesFromFile(String filename){ byte[] arr =this.fromFile(filename); if(arr == null){ return null; } return this.toCourses(arr); } } MyTestRunner.java package persistetnce2; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; /** * * @author Antoun */ public class MyTestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(TestJunit.class); boolean okay = true; for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); okay = false; } if(okay) System.out.println("All tests have run successfully"); } } Student.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package persistetnce2; import java.io.Serializable; /** * * @author Antoun */ public class Student implements Serializable{ private String fname; private String lname; private double GPA; public Student(){} public Student(String fname,String lname, double GPA) { this.fname = fname; this.lname= lname; this.GPA = GPA; } public String getLName() { return lname; } public double getGPA() { return GPA; } public void setLName(String lname) { this.lname = lname; } public void setGPA(double GPA) { this.GPA = GPA; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return lname.equals(s.lname) && fname.equals(s.fname) && GPA == s.GPA; } } StudentPersister.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package persistetnce2; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Antoun */ public class StudentPersister { private byte[] toStream(Student s){ byte[]stream = null; try { ByteArrayOutputStream baos=new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(s); stream = baos.toByteArray(); } catch (IOException ex) { Logger.getLogger(StudentPersister.class.getName()).log(Level.SEVERE, null, ex); } return stream; } private byte[] toStream(ArrayList sArr){ byte[]stream = null; try { ByteArrayOutputStream baos=new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(sArr); stream = baos.toByteArray(); } catch (IOException ex) { Logger.getLogger(StudentPersister.class.getName()).log(Level.SEVERE, null, ex); } return stream; } private Student toStudent(byte[]stream){ Student s = null; try{ ByteArrayInputStream bais = new ByteArrayInputStream(stream); ObjectInputStream ois = new ObjectInputStream(bais); s = (Student)ois.readObject(); } catch (IOException ex) { Logger.getLogger(StudentPersister.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(StudentPersister.class.getName()).log(Level.SEVERE, null, ex); } return s; } private ArrayList toStudents(byte[]stream){ ArrayList stus = null; try{ ByteArrayInputStream bais = new ByteArrayInputStream(stream); ObjectInputStream ois = new ObjectInputStream(bais); stus = (ArrayList)ois.readObject(); } catch (IOException ex) { Logger.getLogger(StudentPersister.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(StudentPersister.class.getName()).log(Level.SEVERE, null, ex); } return stus; } private boolean toFile(String filename,byte[]arr){ FileOutputStream fos; try { fos = new FileOutputStream(filename); fos.write(arr); fos.close(); } catch (FileNotFoundException ex) { Logger.getLogger(CoursePersister.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (IOException ex) { Logger.getLogger(CoursePersister.class.getName()).log(Level.SEVERE, null, ex); return false; } return true; } private byte[] fromFile(String filename){ try { return Files.readAllBytes(Paths.get(filename)); } catch (IOException ex) { Logger.getLogger(CoursePersister.class.getName()).log(Level.SEVERE, null, ex); return null; } } public boolean convertStudentToFile(String filename, Student c){ byte[]arr = this.toStream(c); if(arr == null) return false; return this.toFile(filename, arr); } public boolean convertStudentsToFile(String filename, ArrayList c){ byte[]arr = this.toStream(c); if(arr == null){ return false; } return this.toFile(filename,arr); } public Student convertStudnetFromFile(String filename){ byte[] arr =this.fromFile(filename); if(arr == null){ return null; } return this.toStudent(arr); } public ArrayList convertStudentsFromFile(String filename){ byte[] arr =this.fromFile(filename); if(arr == null){ return null; } return this.toStudents(arr); } } TestJunit.java package persistetnce2; import java.util.ArrayList; import org.junit.Test; import static org.junit.Assert.assertEquals; /* test cases: 1- write student and read it 2- write students and read it 3- write course and read it 4- write courses and read it 5- write garbage and read it with the course function 6- write garbage and read it with student function */ public class TestJunit { @Test public void testWritingStudentAndReadIt() { Student s1 = new Student("james","fred",4.0); StudentPersister sp = new StudentPersister(); sp.convertStudentToFile("test1", s1); assertEquals(s1, sp.convertStudnetFromFile("test1")); } @Test public void testWritingStudentsAndReadIt(){ Student s1 = new Student("Antoun","wagdy", 0); Student s2 = new Student("Andrew", "wagdy",0); Student s3 = new Student("Wagdy", "james",0); ArrayList arr = new ArrayList<>(); arr.add(s1); arr.add(s2); arr.add(s2); StudentPersister sp = new StudentPersister(); sp.convertStudentsToFile("test2", arr); sp.convertStudentsFromFile("test2"); } @Test public void testWritingCourseAndReadIt() { Course c1 = new Course("course1"); Student s1 = new Student("Antoun","wagdy", 0); Student s2 = new Student("Andrew", "wagdy",0); Student s3 = new Student("Wagdy", "james",0); c1.addStudent(s1); c1.addStudent(s2); c1.addStudent(s3); CoursePersister cp = new CoursePersister(); cp.convertCourseToFile("test3",c1); assertEquals(c1, cp.convertCourseFromFile("test3")); } @Test public void testWritingCoursesAndReadIt() { Course c1 = new Course("course1"); Student s1 = new Student("Antoun","wagdy", 0); Student s2 = new Student("Andrew", "wagdy",0); Student s3 = new Student("Wagdy", "james",0); c1.addStudent(s1); c1.addStudent(s2); c1.addStudent(s3); Course c2 = new Course("course2"); c2.addStudent(s1); c2.addStudent(s2); c2.addStudent(s3); ArrayList courses = new ArrayList<>(); courses.add(c1); courses.add(c2); CoursePersister cp = new CoursePersister(); cp.convertCoursesToFile("test4",courses); assertEquals(courses, cp.convertCoursesFromFile("test4")); } @Test public void writingGarbage() { CoursePersister cp = new CoursePersister(); assertEquals(null, cp.convertCourseFromFile("garbage")); } }