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.*; import java.io.*;
public class FileSaving extends Player { public class FileSaving extends Player implements Serializable {
InputStream in = null;
BufferedReader reader = null;
OutputStream out = null;
public Boolean loadFile() { public Player loadFile() {
try { try {
in = new FileInputStream("src/saves/playerSave.txt"); InputStream in = new FileInputStream(new File("src/saves/playerSave.txt"));
reader = new BufferedReader(new InputStreamReader(in)); 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; return true;
} }
catch (Exception e){ catch (Exception e) {
//returns false if there isn't a save file
return false; 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 * 2019-03-10
* Authors: Harkamal, Vikram, Haris, Siddhant, Nathan * Authors: Harkamal, Vikram, Haris, Siddhant, Nathan
@@ -5,7 +7,7 @@
* *
*/ */
public class Player { public class Player implements Serializable {
private String name = "Taipan"; private String name = "Taipan";
private int bank = 0; private int bank = 0;
@@ -30,8 +32,7 @@ public class Player {
private int generalPrice = 8; private int generalPrice = 8;
private int isPriceChanged = 0; private int isPriceChanged = 0;
public Player() { public Player(){
} }
/** /**
@@ -63,13 +64,13 @@ public class Player {
this.wSilk = player.wSilk; this.wSilk = player.wSilk;
this.wGeneral = player.wGeneral; this.wGeneral = player.wGeneral;
this.wArms = player.wArms; this.wArms = player.wArms;
this.retire = player.retire;
this.cargoSpace = player.cargoSpace;
this.opiumPrice = player.opiumPrice; this.opiumPrice = player.opiumPrice;
this.silkPrice = player.silkPrice; this.silkPrice = player.silkPrice;
this.armsPrice = player.armsPrice; this.armsPrice = player.armsPrice;
this.generalPrice = player.generalPrice; this.generalPrice = player.generalPrice;
this.cargoSpace = player.cargoSpace;
this.isPriceChanged = player.isPriceChanged; 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); Scene root = new Scene(borderPane, 600, 480);
root.getStylesheets().add("styleguide.css"); root.getStylesheets().add("styleguide.css");

View File

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

BIN
src/saves/playerSave.txt Normal file

Binary file not shown.