Skip to content

Commit

Permalink
WIP solution for loading and saving data to .ser file
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornarhagen committed Apr 3, 2017
1 parent ca210ef commit 724a51c
Showing 1 changed file with 78 additions and 4 deletions.
82 changes: 78 additions & 4 deletions src/app/App.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package app;

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.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import administration.Course;
import administration.Department;
import gui.*;
import hr.Student;
import inventory.Item;
import inventory.Loan;

public class App {
public static void main(String[] args) {
public class App implements Serializable {
public static void main(String[] args) throws IOException {
Department larerStudie = new Department("Avdeling for lærerutdanning", "LA123");
new Course("Engelsk", "LAENG123", larerStudie);

Expand All @@ -36,7 +44,73 @@ public static void main(String[] args) {
new Loan(anja, laptop, new GregorianCalendar());
new Loan(kjetil, mobil, new GregorianCalendar());

System.out.println(saveData());

ArrayList<Object> tempData = (ArrayList<Object>) loadData();

for (int i = 0; i < tempData.size(); i++) {
ArrayList<Object> objectData = (ArrayList<Object>) tempData.get(i);
System.out.println(tempData.get(i).getClass().getSimpleName() + ":");

for (int j = 0; j < objectData.size(); j++) {
System.out.println(" " + objectData.get(j));
}
}

// Start the GUI
MainFrame.main(null);
// MainFrame.main(null);
}

public static boolean saveData() throws IOException {
// Write .ser file
ObjectOutputStream oos = null;
FileOutputStream fout = null;

try{
fout = new FileOutputStream("C:\\Users\\bjorn\\Desktop\\oblig7\\test.ser", false);
oos = new ObjectOutputStream(fout);

List<Object> myObjects = new ArrayList<>();

myObjects.add(Loan.toStrings());
myObjects.add(Item.toStrings());
myObjects.add(Student.toStrings());
myObjects.add(Department.toStrings());
myObjects.add(Course.toStrings());

oos.writeObject(myObjects);
} catch (FileNotFoundException e) {
System.out.println("File not found, make sure the path is correct.");
} catch (Exception e) {
e.printStackTrace();
} finally {
if(oos != null){
oos.close();
}
}

return false;
}

public static Object loadData() throws IOException {
// Read .ser file
ObjectInputStream objectinputstream = null;
FileInputStream streamIn = null;

try {
streamIn = new FileInputStream("C:\\Users\\bjorn\\Desktop\\oblig7\\test.ser");
objectinputstream = new ObjectInputStream(streamIn);
return objectinputstream.readObject();
} catch (FileNotFoundException e) {
System.out.println("File not found, make sure the path is correct.");
} catch(Exception e) {
e.printStackTrace();
} finally {
if(objectinputstream != null){
objectinputstream.close();
}
}

return null;
}
}

0 comments on commit 724a51c

Please sign in to comment.