From 9d113a656fc9375f31505d1e82490317438a1843 Mon Sep 17 00:00:00 2001 From: Solargale Date: Sat, 23 Mar 2019 18:13:07 -0600 Subject: [PATCH] Finished game saves --- src/FileSaving.java | 45 ++++++++++++++++++++++----------------- src/Player.java | 11 +++++----- src/StartGUI.java | 15 +++++++++++++ src/TaipanShopGUI.java | 3 +++ src/saves/playerSave.txt | Bin 0 -> 367 bytes 5 files changed, 49 insertions(+), 25 deletions(-) create mode 100644 src/saves/playerSave.txt diff --git a/src/FileSaving.java b/src/FileSaving.java index d4b4cb7..6fd6a4a 100644 --- a/src/FileSaving.java +++ b/src/FileSaving.java @@ -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 + catch (Exception e) { return false; } } - public void saveFile(){ - try{ - out = new FileOutputStream("src/saves/playerSave.txt"); - } - catch (Exception e){ - - } - - } - } diff --git a/src/Player.java b/src/Player.java index f6e771d..35d7014 100644 --- a/src/Player.java +++ b/src/Player.java @@ -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; @@ -30,8 +32,7 @@ public class Player { private int generalPrice = 8; private int isPriceChanged = 0; - public Player() { - + 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; } /** diff --git a/src/StartGUI.java b/src/StartGUI.java index 8fe02f1..447c4e1 100644 --- a/src/StartGUI.java +++ b/src/StartGUI.java @@ -215,6 +215,21 @@ public class StartGUI extends Player{ } }); + continueButton.setOnAction(new EventHandler() { + @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"); diff --git a/src/TaipanShopGUI.java b/src/TaipanShopGUI.java index 1880bd3..95ea9c2 100644 --- a/src/TaipanShopGUI.java +++ b/src/TaipanShopGUI.java @@ -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); diff --git a/src/saves/playerSave.txt b/src/saves/playerSave.txt new file mode 100644 index 0000000000000000000000000000000000000000..c43bceba387a9b8c34de561953eda3e5be13aa3c GIT binary patch literal 367 zcmX|5K~BRk5FAPqnHF&<5-&jF*ayI+;y^4#B2sRM%Qm%A6WfU#m(cfcMLdW<@CWQT z)Y52X*SoXx^^2spNDDxaHPX?F!Sa^=?8Pn4Qb)9%HQan*2