In: Computer Science
IN JAVACreate a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1.
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
Code to copy:
Student.java:
package persistence;
import java.io.Serializable;
public class Student implements Serializable
{
// Declaration of variable name
private String name;
// Declaration of variable gpa
private double gpa;
// constuctor of Student class
public Student(String name, double gpa)
{
this.name = name;
this.gpa = gpa;
}
// getName method to return name of the student
public String getName()
{
return name;
}
// getGpa method for return gpa
public double getGpa()
{
return gpa;
}
// setName method to set name of the student
public void setName(String name)
{
this.name = name;
}
// setGpa method to set new gpa of the student
public void setGpa(double gpa)
{
this.gpa = gpa;
}
@Override
// equals method for comapre two objects
public boolean equals(Object obj)
{
if (obj instanceof Student)
{
// convert object into student
Student other = (Student) obj;
// return true if two objects are same
return (this.name.equals(other.name) && this.gpa == other.gpa);
} else
return false;
}
@Override
// toString method
public String toString()
{
// return student name and gpa as astring
return "Student name=" + name + ", gpa=" + gpa;
}
}
Course.java:
package persistence;
import java.io.Serializable;
import java.util.ArrayList;
public class Course implements Serializable
{
private String courseName;
private ArrayList<Student> students = new ArrayList<>();
// constructor of course class
public Course(String courseName)
{
this.courseName = courseName;
}
// method to get the name of the course
public String getName()
{
return courseName;
}
// Method to get the students in that course
public ArrayList<Student> getStudents()
{
return students;
}
// Method for set the new name of the course
public void setName(String courseName)
{
this.courseName = courseName;
}
// Method to set the new students in course
public void setStudents(ArrayList<Student> students)
{
this.students = students;
}
// Method to add the students in course
public void addStudent(Student student)
{
students.add(student);
}
@Override
public boolean equals(Object obj)
{
if (obj instanceof Course)
{
// convert object into course type
Course other = (Course) obj;
// return true if two objects are same
return (this.courseName.equals(other.courseName)
&& this.students.equals(other.students));
}
else
return false;
}
@Override
public String toString()
{
// return course name and student list in the course
return "Course Name=" + courseName + ", students=" + students;
}
}
PersistentImp.java:
package persistence;
import java.io.File;
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.io.Serializable;
import java.util.List;
public class PersistentImp<T> implements Persister<T>, Serializable
{
// Declare output stream variable
ObjectOutputStream oos = null;
// Declare input stream variable
ObjectInputStream ois = null;
@Override
public void saveObjectToFile(File f, T ob)
{
// for writing or saving binary data
try
{
// create file output stream object
FileOutputStream fos = new FileOutputStream(f);
// converting java-object to binary-format
oos = new ObjectOutputStream(fos);
// writing or saving object's value to stream
oos.writeObject(ob);
oos.flush();
oos.close(); // closeing output stream
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void saveListToFile(File f, List<T> myList)
{
try
{
FileOutputStream fos = new FileOutputStream(f);
oos = new ObjectOutputStream(fos);
// writing or saving student object's value to stream
oos.writeObject(myList);
oos.flush();
oos.close();// closeing output stream
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public T readObjectFromFile(File f)
{
Object obj = null;
// reading binary data
try
{
FileInputStream fis = new FileInputStream(f);
// converting binary-data to java-object
ois = new ObjectInputStream(fis);
// reading object's value and casting to student class
obj = ois.readObject();
ois.close();// closeing input stream
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
return (T) obj;
}
@SuppressWarnings("unchecked")
@Override
public List<T> readListFromFile(File f)
{
// creating List reference to hold students after de-serialization
List<T> list = null;
try
{
// reading binary data
FileInputStream fis = new FileInputStream(f);
// converting binary-data to java-object
ois = new ObjectInputStream(fis);
// reading object's value and casting ArrayList<String>
list = (List<T>) ois.readObject();
}
catch (FileNotFoundException fnfex)
{
fnfex.printStackTrace();
}
catch (IOException ioex)
{
ioex.printStackTrace();
}
catch (ClassNotFoundException ccex)
{
ccex.printStackTrace();
}
return list;
}
}
TestPersister.java:
package persistence;
import static org.junit.Assert.*;
import org.junit.Test;
import java.io.File;
import java.util.ArrayList;
import org.junit.Before;
public class TestPersister
{
//Declare list variables as student
Student student[];
//Declare list variables
Course course1, course2;
//Declare list variables as student and course
ArrayList<Student> students = new ArrayList<>();
ArrayList<Course> courses = new ArrayList<>();
@Before
public void before() throws Exception
{
//initialise students array
student=new Student[10];
//create 10 student object
student[0] = new Student("Fax", 6.7);
student[1]= new Student("Adam", 5.7);
student[2] = new Student("Even", 7.7);
student[3] = new Student("Samir", 8.7);
student[4] = new Student("Jmith", 8.0);
student[5] = new Student("JohnWesly", 9.1);
student[6] = new Student("Jacob", 5.7);
student[7] = new Student("Adel", 8.7);
student[8] = new Student("Sam", 6.7);
student[9] = new Student("Ellena", 6.2);
//initialise objects of course
course1 = new Course("CSE01");
course2 = new Course("CSE02");
//
students = new ArrayList<>();
courses = new ArrayList<>();
System.out.println("Setting it up!");
//add 10 students into student list
for(int i=0;i<10;i++)
students.add(student[i]);
//add 5 students into course1
for(int i=0;i<5;i++)
course1.addStudent(student[i]);
//add 5 students into course1
for(int i=5;i<10;i++)
course2.addStudent(student[i]);
//add two coureses into coures list
courses.add(course1);
courses.add(course2);
}
@Test
public void testStudentPersistence()
{
//Declare file object paramater with student.ser
File serializableFile = new File("Student.ser");
//Inialise obeject PersistentImp as student type
PersistentImp<Student> obj=new PersistentImp<Student>();
obj.saveObjectToFile(serializableFile, student[0]);
Student restoredstudent =(Student) obj.readObjectFromFile(serializableFile);
assertEquals(student[0], restoredstudent);
}
@Test
public void testStudentListPersistence()
{
//Inialise obeject PersistentImp as student type
PersistentImp<Student> obj=new PersistentImp<Student>();
File serializableFile = new File("StudentList.ser");
//save student data as binary formate in Serializable file
obj.saveListToFile(serializableFile, students);
//restore student data from Serializable file
ArrayList<Student> stus = (ArrayList<Student>) obj.readListFromFile(serializableFile);
assertEquals("Student list is not same", students, stus);
assertEquals("Students list size is not same", students.size(), stus.size());
}
@Test
public void testCoursePersistence()
{
//Inialise obeject PersistentImp as course type
PersistentImp<Course> courseObj=new PersistentImp<Course>();
File serializableFile = new File("Course.ser");
//save course data as binary formate in Serializable file
courseObj.saveObjectToFile(serializableFile, course1);
//restore course data from Serializable file
Course course= courseObj.readObjectFromFile(serializableFile);
assertEquals("Course object is not same", course1, course);
assertEquals("Course's Students object is not same", course1.getStudents(), course.getStudents());
}
@Test
public void testCourseListPersistence()
{
//Inialise obeject PersistentImp as course type
PersistentImp<Course> courseObj=new PersistentImp<Course>();
File serializableFile = new File("CourseList.ser");
//save course data as binary formate in Serializable file
courseObj.saveListToFile(serializableFile, courses);
//restore course data from Serializable file
ArrayList<Course> cours = (ArrayList<Course>)courseObj.readListFromFile(serializableFile);
assertEquals("Course list is not same", courses, cours);
assertEquals("Course list size is not same", courses.size(), cours.size());
}
}
Persister.java:
package persistence;
import java.io.File;
import java.util.List;
public interface Persister < T > {
public void saveObjectToFile(File f, T ob);
public void saveListToFile(File f, List< T > myList);
public T readObjectFromFile(File f);
public List< T > readListFromFile(File f);
}
Kindly revert for any queries
Thanks.