From e1fdc5deac098f113d6d6ede743054b75914e823 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Thu, 28 Apr 2022 03:07:51 +0200 Subject: [PATCH] finished initial draft of file io and also added some test cases to disscuss in app --- .../zhaw/projekt2/turnierverwaltung/App.java | 19 +++++++++++- .../projekt2/turnierverwaltung/FileIO.java | 30 ++++++++++++------- .../turnierverwaltung/Tournamenttest.java | 7 +++++ 3 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java index d00a1a6..dc9a2f3 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java @@ -3,6 +3,10 @@ */ package ch.zhaw.projekt2.turnierverwaltung; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + public class App { public String getGreeting() { return "Hello World!"; @@ -10,8 +14,21 @@ public class App { public static void main(String[] args) { System.out.println(new App().getGreeting()); + FileIO io = new FileIO(); - Tournament tournament= new Tournamenttest("hello"); + Tournament tournament= new Tournamenttest("helloo"); io.saveTournament(tournament); + + + List tournaments = io.getList(); + + for (File name: tournaments) { + + System.out.println(name.getName()); + } + + Tournament retrieved = io.loadTournament(tournaments.get(0)); + System.out.println("data retreived:"); + System.out.println(retrieved.getName()); } } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java index ca86b5c..9271812 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -2,6 +2,7 @@ package ch.zhaw.projekt2.turnierverwaltung; import java.io.*; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; @@ -11,21 +12,33 @@ public class FileIO { public FileIO() { - String savePath = System.getProperty("user.dir")+File.separator+"tournierverwaltung_angrynerds"; + String savePath = System.getProperty("user.dir") + File.separator + "tournierverwaltung_angrynerds"; this.saveDir = new File(savePath); + saveDir.mkdir(); saves = new File(saveDir, "saves"); + saves.mkdir(); } - public void getList() { - + public List getList() { + return Arrays.asList(saves.listFiles()); } - public void loadTournament(Tournament tournament) { + public Tournament loadTournament(File tournamentFile) { + Tournament tournament; + try { + ObjectInputStream in = new ObjectInputStream(new FileInputStream(tournamentFile)); + tournament = (Tournament) in.readObject(); + in.close(); + } catch (ClassNotFoundException | IOException e) { + throw new RuntimeException(e); + } + + return tournament; } public void saveTournament(Tournament tournament) { - File newSave = new File(saves, tournament.getName()+".txt"); + File newSave = new File(saves, tournament.getName() + ".txt"); try { newSave.createNewFile(); } catch (IOException e) { @@ -36,16 +49,11 @@ public class FileIO { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(newSave)); out.writeObject(tournament); out.close(); - System.out.println("Save File" + tournament.getName()+".txt being saved to " + saves.getAbsolutePath()); + System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } } - - public void getTournament(String storageSpace) { - - } - } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java new file mode 100644 index 0000000..2b259a9 --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournamenttest.java @@ -0,0 +1,7 @@ +package ch.zhaw.projekt2.turnierverwaltung; + +public class Tournamenttest extends Tournament { + public Tournamenttest(String name) { + super(name); + } +}