Package model
Class Statistics
java.lang.Object
model.Statistics
The Statistics class represents a set of statistics for a blackjack game.
Statistics are stored in a CSV file with the following format:
- Column 1: number of wins
- Column 2: number of losses
- Column 3: number of pushes(ties)
The file is named statistics.csv and is stored in the
db/ directory.
- Author:
- Michael Neuper michael@michaelneuper.com
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newStatisticsobject with all statistics initialized to 0.Statistics(int wins, int losses, int pushes) Constructs a newPlayerobject with the given statistics. -
Method Summary
Modifier and TypeMethodDescriptionintGets the number of losses.intGets the number of pushes (ties).intgetWins()Gets the number of wins.voidIncrements the number of losses by 1 and saves the new statistics to the CSV file.voidIncrements the number of pushes (ties) by 1 and saves the new statistics to the CSV file.voidIncrements the number of wins by 1 and saves the new statistics to the CSV file.static Statisticsload()This static method loads the statistics from theCSV_FILEfile located at the path defined by theCSV_FILEconstant in theStatisticsclass.voidsetLosses(int losses) Sets the number of losses.voidsetPushes(int pushes) Sets the number of pushes (ties).voidsetWins(int wins) Sets the number of wins.
-
Constructor Details
-
Statistics
public Statistics()Constructs a newStatisticsobject with all statistics initialized to 0. -
Statistics
public Statistics(int wins, int losses, int pushes) Constructs a newPlayerobject with the given statistics.- Parameters:
wins- the number of winslosses- the number of lossespushes- the number of pushes (ties)
-
-
Method Details
-
getWins
public int getWins()Gets the number of wins.- Returns:
- the number of wins
-
getLosses
public int getLosses()Gets the number of losses.- Returns:
- the number of losses
-
getPushes
public int getPushes()Gets the number of pushes (ties).- Returns:
- the number of pushes
-
setWins
public void setWins(int wins) Sets the number of wins.- Parameters:
wins- the new number of wins
-
setLosses
public void setLosses(int losses) Sets the number of losses.- Parameters:
losses- the new number of losses
-
setPushes
public void setPushes(int pushes) Sets the number of pushes (ties).- Parameters:
pushes- the new number of pushes
-
incrementWins
public void incrementWins()Increments the number of wins by 1 and saves the new statistics to the CSV file. -
incrementLosses
public void incrementLosses()Increments the number of losses by 1 and saves the new statistics to the CSV file. -
incrementPushes
public void incrementPushes()Increments the number of pushes (ties) by 1 and saves the new statistics to the CSV file. -
load
This static method loads the statistics from the
CSV_FILEfile located at the path defined by theCSV_FILEconstant in theStatisticsclass. The method uses aCSVReaderobject to read the data from the file.If the
CSV_FILEfile does not have the expected headers, anIOExceptionis thrown. The method returns aStatisticsobject with the values read from theCSV_FILEfile.If an
IOExceptionoccurs during the file reading process, it is caught and printed to the standard error stream. If aCsvValidationExceptionis thrown during the file reading process, it is handled internally and an emptyStatisticsobject is returned.- Returns:
- a
Statisticsobject with the values read from theCSV_FILEfile, or an emptyStatisticsobject if there was a problem with the CSV file format - Throws:
IOException- if there is a problem with the file reading process or the CSV file headers are invalid
-