commit 18e718ff392f7c4be2fa93178f45c6376af18ce7 parent 335a7ef3a73f63ae8fa49fcb05e4d62d8843936e Author: Michael Neuper <73108749+michaelneuper@users.noreply.github.com> Date: Thu, 27 Apr 2023 22:15:27 +0200 Make balance.txt file final Diffstat:
| M | src/model/Player.java | | | 40 | ++++++++++++++++++++++++---------------- |
1 file changed, 24 insertions(+), 16 deletions(-)
diff --git a/src/model/Player.java b/src/model/Player.java @@ -8,44 +8,52 @@ import java.io.PrintWriter; import java.util.Scanner; /** - * Handles player specific operations, such as deciding to hit or stand. - * - * @author Michael + * Represents a player in a Blackjack game */ public class Player extends Person { - - private double balance; // TODO: make public - Scanner input = new Scanner(System.in); + + private double balance; // Player's balance + private final String BALANCE_FILE = "db/balance.txt"; // File to store the player's balance + private Scanner input = new Scanner(System.in); /** - * Creates a new player + * Creates a new player with a default name of "Player" */ public Player() { super.setName("Player"); } - - public double getBalance(String inFile) { + + /** + * Gets the player's current balance from a file + * + * @return balance the player's current balance + */ + public double getBalance() { try { - Scanner scFile = new Scanner(new File(inFile)); + Scanner scFile = new Scanner(new File(BALANCE_FILE)); this.balance = scFile.nextDouble(); scFile.close(); } catch (FileNotFoundException e) { - System.out.println("Error:\n" + e); + System.out.println("Error: " + BALANCE_FILE + " not found."); } return this.balance; } - public void setBalance(String inFile, double balance) { + /** + * Sets the player's balance and stores it in a file + * + * @param balance the new balance to set + */ + public void setBalance(double balance) { try { - PrintWriter outFile = new PrintWriter(new FileWriter(inFile), false); + PrintWriter outFile = new PrintWriter(new FileWriter(BALANCE_FILE), false); outFile.print(balance); outFile.close(); } catch (IOException e) { - System.out.println("Error:\n" + e); + System.out.println("Error writing to " + BALANCE_FILE); } -// this.balance = balance; // TODO: is neccesary? + this.balance = balance; } - }