Finished game saves
This commit is contained in:
@@ -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){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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");
|
||||
|
||||
|
||||
@@ -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
BIN
src/saves/playerSave.txt
Normal file
Binary file not shown.
Reference in New Issue
Block a user