finished initial draft of file io and also added some test cases to disscuss in app
This commit is contained in:
parent
e0b1616edf
commit
e1fdc5deac
|
@ -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<File> 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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<File> 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) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package ch.zhaw.projekt2.turnierverwaltung;
|
||||
|
||||
public class Tournamenttest extends Tournament {
|
||||
public Tournamenttest(String name) {
|
||||
super(name);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue