Finished game saves

This commit is contained in:
2019-03-23 18:13:07 -06:00
parent e6e1f02ea9
commit 9d113a656f
5 changed files with 49 additions and 25 deletions

View File

@@ -1,33 +1,38 @@
import java.io.*;
public class FileSaving extends Player {
InputStream in = null;
BufferedReader reader = null;
OutputStream out = null;
public class FileSaving extends Player implements Serializable {
public Boolean loadFile() {
public Player loadFile() {
try {
in = new FileInputStream("src/saves/playerSave.txt");
reader = new BufferedReader(new InputStreamReader(in));
InputStream in = new FileInputStream(new File("src/saves/playerSave.txt"));
ObjectInputStream inObject = new ObjectInputStream(in);
Player player = (Player) inObject.readObject();
System.out.println(getPlayer().getName());
in.close();
inObject.close();
return player;
}
catch (Exception e) {
return null;
}
}
//Returns true if there is a save file
public boolean saveFile(Player player){
try{
FileOutputStream out = new FileOutputStream(new File("src/saves/playerSave.txt"));
ObjectOutputStream outObject = new ObjectOutputStream(out);
outObject.writeObject(player);
out.close();
outObject.close();
//returns true if program can save file
return true;
}
catch (Exception e) {
//returns false if there isn't a save file
return false;
}
}
public void saveFile(){
try{
out = new FileOutputStream("src/saves/playerSave.txt");
}
catch (Exception e){
}
}
}

View File

@@ -1,3 +1,5 @@
import java.io.Serializable;
/**
* 2019-03-10
* Authors: Harkamal, Vikram, Haris, Siddhant, Nathan
@@ -5,7 +7,7 @@
*
*/
public class Player {
public class Player implements Serializable {
private String name = "Taipan";
private int bank = 0;
@@ -31,7 +33,6 @@ public class Player {
private int isPriceChanged = 0;
public Player(){
}
/**
@@ -63,13 +64,13 @@ public class Player {
this.wSilk = player.wSilk;
this.wGeneral = player.wGeneral;
this.wArms = player.wArms;
this.retire = player.retire;
this.cargoSpace = player.cargoSpace;
this.opiumPrice = player.opiumPrice;
this.silkPrice = player.silkPrice;
this.armsPrice = player.armsPrice;
this.generalPrice = player.generalPrice;
this.cargoSpace = player.cargoSpace;
this.isPriceChanged = player.isPriceChanged;
this.retire = player.retire;
}
/**

View File

@@ -215,6 +215,21 @@ public class StartGUI extends Player{
}
});
continueButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
FileSaving saving = new FileSaving();
if(saving.loadFile() != null){
TaipanShopGUI shop = new TaipanShopGUI(saving.loadFile());
shop.initializeShop(stage);
stage.show();
}
else{
authors.setText("There are no previous saves!");
}
}
});
Scene root = new Scene(borderPane, 600, 480);
root.getStylesheets().add("styleguide.css");

View File

@@ -289,6 +289,9 @@ public class TaipanShopGUI extends Player{
* @param stage
*/
public void initializeShop(Stage stage) {
FileSaving saving = new FileSaving();
saving.saveFile(getPlayer());
Font size14 = new Font(14.0);
Rectangle dialogueRectangle = new Rectangle();
dialogueRectangle.setFill(javafx.scene.paint.Color.WHITE);

BIN
src/saves/playerSave.txt Normal file

Binary file not shown.