librejack

Blackjack clone written in java
git clone https://github.com/michaelneuper/librejack.git
README | Files | Log | Refs | LICENSE

commit 8425847dab9b34e84f15822178d6546ade33dd73
parent a033840fa43d328c009fc085d2e89342177e8fba
Author: Michael Neuper <michael@michaelneuper.com>
Date:   Sat, 13 May 2023 10:56:24 +0200

Update javadoc

Diffstat:
Msrc/controller/Controller.java | 42+++++++++++++++++-------------------------
Msrc/controller/Hand.java | 35++++++++++++++++++-----------------
Msrc/controller/LoginController.java | 22+++++++++++-----------
Msrc/model/Card.java | 47+++++++++++++++++++++++++----------------------
Msrc/model/Dealer.java | 17++++++++++-------
Msrc/model/Deck.java | 45+++++++++++++++++++++++++--------------------
Msrc/model/Person.java | 42+++++++++++++++++++++++-------------------
Msrc/model/Player.java | 18++++++++----------
Msrc/model/Statistics.java | 52++++++++++++++++++++++++++++------------------------
Msrc/view/MainUI.java | 4----
Msrc/view/StatisticsChart.java | 6+++---
11 files changed, 168 insertions(+), 162 deletions(-)

diff --git a/src/controller/Controller.java b/src/controller/Controller.java @@ -32,9 +32,9 @@ public class Controller { private boolean hasPlayed; /** - * Creates a new instance of the Controller class. Initializes the dealer - * and player, the deck and discarded decks, and the bet and balance of the - * player. The deck is also populated and shuffled. + * Constructs {@link Controller} object. Initializes the {@link Dealer} and + * {@link Player}, the {@link Deck} and discarded decks, and the bet and + * balance of the player. The deck is also populated and shuffled. */ public Controller() { this.STATS = new Statistics(); @@ -58,7 +58,7 @@ public class Controller { /** * Gets the current player's bet. * - * @return the current player's bet + * @return the player's current bet */ public double getBet() { return bet; @@ -81,14 +81,14 @@ public class Controller { /** * Gets the current player's balance. * - * @return the current player's balance + * @return the player's current balance */ public double getBalance() { return balance; } /** - * Sets the current player's balance. + * Sets the player's current balance. * * @param balance player's balance */ @@ -223,73 +223,65 @@ public class Controller { } /** - * Hits the player with a card. + * Hits the player with a {@link model.Card}. */ public void hitPlayer() { PLAYER.hit(DECK, DISCARDED); } /** - * - * @return player hand + * @return the player's current {@link Hand} */ public String displayPlayerHand() { return PLAYER.printHand(); } /** - * - * @return the value of the player's hand + * @return the value of the player's {@link Hand} */ public String displayPlayerHandValue() { return Integer.toString(PLAYER.getHand().calculateValue()); } /** - * - * @return dealer hand + * @return the dealer's current {@link Hand} */ public String displayDealerHand() { return DEALER.printHand(); } /** - * - * @return value of dealer's hand + * @return the value of dealer's {@link Hand} */ public String displayDealerHandValue() { return Integer.toString(DEALER.getHand().calculateValue()); } /** - * - * @return dealer's first card + * @return the dealer's first {@link model.Card} */ public String displayDealerFirstCard() { return DEALER.printFirstHand(); } /** - * - * @return value of the first card + * @return the value of the first {@link model.Card} */ public String displayDealerFirstCardValue() { return DEALER.printFirstCardValue(); } /** - * - * @param i index of the card in the dealer's hand - * @return file path of the dealer's card + * @param i index of the card in the dealer's {@link Hand} + * @return file path of the dealer's {@link model.Card} */ public String getDealerCardFilePath(int i) { return DEALER.printFilePath(i); } /** - * - * @param i index of the card in the player's hand - * @return file path of player's card + * @param i index of the card in the player's {@link Hand} + * @return file path of player's {@link model.Card} */ public String getPlayerCardFilePath(int i) { return PLAYER.printFilePath(i); diff --git a/src/controller/Hand.java b/src/controller/Hand.java @@ -17,25 +17,25 @@ public class Hand { private final ArrayList<Card> HAND; /** - * Creates a new empty Hand object. + * Constructs a new empty {@code Hand} object. */ public Hand() { HAND = new ArrayList<>(); } /** - * Adds a card from the deck to the hand. + * Adds a {@link Card} from the {@link Deck} to the {@link Hand}. * - * @param deck deck to take card from + * @param deck deck to take {@link Card}from */ public void takeCardFromDeck(Deck deck) { HAND.add(deck.takeCard()); } /** - * Calculates the value of a hand. + * Calculates the value of a {@link Hand}. * - * @return value value of the hand + * @return the value of a {@link Hand} */ public int calculateValue() { int value = 0; // current total value @@ -63,19 +63,20 @@ public class Hand { } /** - * Gets the card at a specific index. + * Gets the {@link Card} at a specific index. * - * @param i the index of the card or get - * @return the card at index i + * @param i the index of the {@link Card} or get + * @return the {@link Card} at index i */ public Card getCard(int i) { return HAND.get(i); } /** - * - * @param i index of the card in hand - * @return file path of a card at a specific index, if the index is out of + * Gets the relative file path of a {@link Card}. + * + * @param i index of the {@link Card}in {@link Hand} + * @return file path of a {@link Card} at a specific index, if the index is out of * range, a blank card is returned */ public String getCardFilePath(int i) { @@ -87,10 +88,10 @@ public class Hand { } /** - * Discards the current hand and adds its cards to the specified discard - * deck. + * Discards the current {@link Hand} and adds its cards to the specified discard + * {@link Deck}. * - * @param discardDeck the deck to which the current hand's cards will be + * @param discardDeck the deck to which the current {@link Hand}'s {@link Card} will be * added */ public void discardHandToDeck(Deck discardDeck) { @@ -99,16 +100,16 @@ public class Hand { } /** - * Returns a string representation of the current hand. + * Returns a {@code String} representation of the current {@link Hand}. * - * @return a string representation of the current hand + * @return a {@code String} representation of the current {@link Hand} */ @Override public String toString() { String ret = ""; // holds string to return for (Card card : HAND) { - ret += card + " \n "; // TODO fix newline not displaying in GUI + ret += card + " \n "; } return ret; diff --git a/src/controller/LoginController.java b/src/controller/LoginController.java @@ -26,7 +26,7 @@ public class LoginController { * @param mobileNumber South African number * @param date date of birth * @param password user's password - * @return true if all fields are valid, false otherwise + * @return {@code true} if all fields are valid, {@code false} otherwise */ public boolean areAllFieldsValid(String email, String mobileNumber, Date date, String password) { return isValidEmail(email) @@ -49,7 +49,7 @@ public class LoginController { * </ul> * * @param email the email to be validated - * @return true if the email is a valid email format, false otherwise + * @return {@code true} if the email is a valid email format, {@code false} otherwise */ private boolean isValidEmail(String email) { // check if email is null @@ -79,8 +79,8 @@ public class LoginController { * number. * * @param mobileNumber the mobile number to check - * @return true if the mobile number is a valid South African mobile number, - * false otherwise + * @return {@code true} if the mobile number is a valid South African mobile number, + * {@code false} otherwise */ private boolean isValidSouthAfricanMobileNumber(String mobileNumber) { // remove any non-digit characters from the mobile number @@ -110,11 +110,11 @@ public class LoginController { } /** - * Checks if a given date is valid and the person is at least 18 years old. + * Checks if a given date of birth is valid and the person is at least 18 years old. * - * @param date the date to be checked - * @return true if the date is valid and the person is at least 18 years - * old, false otherwise + * @param date the date of birth to be checked + * @return {@code true} if the date is valid and the person is at least 18 years + * old, {@code false} otherwise */ private static boolean isValidDateAndAdult(Date date) { // check if the date is null @@ -157,8 +157,8 @@ public class LoginController { * <li>has at least 1 uppercase and lowercase letter</li> * </ul> * - * @param password to be validated - * @return true if password is valid, false otherwise + * @param password the password to be validated + * @return {@code true} if password is matches all criteria, {@code false} otherwise */ private boolean isValidPassword(String password) { boolean hasUpperCase = false; @@ -206,7 +206,7 @@ public class LoginController { * following: !@#$%^&*()-+=[]{}|\;:'",<.>/?. * * @param ch the character to check - * @return true if the character is a special character, false otherwise + * @return {@code true} if the character is a special character, {@code false} otherwise */ private boolean isSpecialCharacter(char ch) { String specialCharacters = "!@#$%^&*()-+=[]{}|\\;:'\",<.>/?"; diff --git a/src/model/Card.java b/src/model/Card.java @@ -8,7 +8,6 @@ package model; */ public class Card { - // TODO: change name to image /** * Represents the four possible suits in a standard playing card deck. */ @@ -22,7 +21,7 @@ public class Card { public String suitName; /** - * Creates a new Suit object with the specified name. + * Constructs a new {@link Suit} object with the specified name. * * @param suitName the name of the suit */ @@ -31,9 +30,9 @@ public class Card { } /** - * Returns the name of the suit as a string. + * Returns the name of the {@link Suit} as a string. * - * @return the name of the suit + * @return the name of the {@link Suit} */ @Override public String toString() { @@ -66,7 +65,8 @@ public class Card { public int rankValue; /** - * Creates a new Rank object with the specified name and value. + * Constructs a new {@link Rank} object with the specified name and + * value. * * @param rankName the name of the rank * @param rankValue the value of the rank @@ -77,9 +77,9 @@ public class Card { } /** - * Returns the name of the rank as a string. + * Returns the name of the {@link Rank} as a string. * - * @return the name of the rank + * @return the name of the {@link Rank} */ @Override public String toString() { @@ -91,10 +91,11 @@ public class Card { private Rank rank; /** - * Creates a new Card object with the specified suit and rank. + * Constructs a new {@link Card} object with the specified {@link Suit} and + * {@link Rank}. * - * @param suit Card suit - * @param rank Card rank + * @param suit {@link Card} {@link Suit} + * @param rank {@link Card} {@link Rank} */ public Card(Suit suit, Rank rank) { this.suit = suit; @@ -102,7 +103,8 @@ public class Card { } /** - * Creates a new Card object as a copy of another Card object. + * Constructs a new {@link Card} object as a copy of another {@link Card} + * object. * * @param card card to be added */ @@ -112,46 +114,47 @@ public class Card { } /** - * Gets the value of the card. + * Gets the value of the {@link Card} from its {@link Rank}. * - * @return The value of the card. + * @return the value of the {@link Card} */ public int getValue() { return this.rank.rankValue; } /** - * Gets the suit of the card. + * Gets the {@link Suit} of the {@link Card}. * - * @return The suit of the card. + * @return the {@link Suit} of the {@link Card} */ public Suit getSuit() { return suit; } /** - * Gets the rank of the card. + * Gets the {@link Rank} of the {@link Card}. * - * @return The rank of the card. + * @return the {@link Rank} of the {@link Card} */ public Rank getRank() { return rank; } /** - * Returns a file representation of a card in the form "ace_of_spades.png" + * Returns a file representation of a {@link Card} in the form + * {@code "ace_of_spades.png"} * - * @return file representation of a card + * @return file representation of a {@link Card} */ public String getFilename() { return rank.toString().toLowerCase() + "_of_" + suit.toString().toLowerCase() + ".png"; } /** - * Returns a String representation of the Card object, including the rank - * and suit of the card, as well as its value in parentheses. + * Returns a {@code String} representation of the {@link Card} object, + * including the {@link Rank} and {@link Suit} of the {@link Card}. * - * @return a String representation of the Card object + * @return a {@code String} representation of the {@link Card} object */ @Override public String toString() { diff --git a/src/model/Dealer.java b/src/model/Dealer.java @@ -15,29 +15,32 @@ package model; public class Dealer extends Person { /** - * Creates a new dealer with the default name "Dealer". + * Constructs a new {@link Dealer} with the default name "Dealer". */ public Dealer() { super.setName("Dealer"); } /** - * Returns a string representation of the dealer's first card in their hand. + * Returns a {@code String} representation of the dealer's first + * {@link Card} in their {@link controller.Hand}. * - * @return the first card in the dealer's hand as a String + * @return the first card in the dealer's {@link controller.Hand} as a + * {@code String} */ public String printFirstHand() { return "" + super.getHand().getCard(0); } /** - * Returns a string representation of the value of the dealer's first card - * in their hand. + * Returns a {@code String} representation of the value of the dealer's + * first {@link Card} in their {@link controller.Hand}. * - * @return the value of the first card in the dealer's hand as a String + * @return the value of the first {@link Card} in the dealer's + * {@link controller.Hand} as a {@code String} */ public String printFirstCardValue() { return "" + super.getHand().getCard(0).getValue(); } - + } diff --git a/src/model/Deck.java b/src/model/Deck.java @@ -8,7 +8,8 @@ import model.Card.Suit; /** * <p> - * Holds multiple cards and performs operations such as shuffling the deck.</p> + * Holds multiple {@link Card} objects and performs operations such as shuffling + * the deck.</p> * <p> * This class is responsible for creating a deck, populating it with 52 cards, * shuffling the deck, adding cards to the deck, removing a card from the deck, @@ -23,16 +24,15 @@ public class Deck { private ArrayList<Card> deck; // creates a "deck" of card objects /** - * - * Deck constructor + * Constructs a new empty {@link Deck} object. */ public Deck() { this.deck = new ArrayList<>(); } /** - * - * Populates empty deck with 52 cards by looping over each rank and value. + * Populates empty {@link Deck} with 52 {@link Card} objects by looping over + * each {@link Rank} and the value of its {@link Suit}. */ public void populate() { for (Suit cardSuit : Suit.values()) { @@ -44,7 +44,8 @@ public class Deck { } /** - * Shuffles deck using a temporary deck for storing cards in random order. + * Shuffles {@link Deck} using a temporary {@link Deck} for storing cards in + * random order. */ public void shuffle() { ArrayList<Card> shufDeck = new ArrayList<>(); // temporary deck for storing cards in random order @@ -58,23 +59,23 @@ public class Deck { shufDeck.add(this.deck.get(randomCardIndex)); // add card to random index of temporary deck this.deck.remove(randomCardIndex); // remove card from original deck } - + this.deck = shufDeck; } /** - * Adds a card to the deck. + * Adds a {@link Card} to the {@link Deck}. * - * @param inCard card the be added + * @param inCard the {@link Card} the be added to the deck */ public void addCard(Card inCard) { this.deck.add(inCard); } /** - * Takes copy of first card from deck and removes card from deck. + * Takes copy of first {@link Card} from the {@link Deck} and removes it. * - * @return the card that was removed + * @return the {@link Card} that was removed */ public Card takeCard() { Card cardToTake = new Card(deck.get(0)); @@ -84,34 +85,38 @@ public class Deck { } /** - * Makes sure the deck we're hitting from has cards in it. + * Checks whether the {@link Deck} to hit is populated with {@link Card} + * objects. * - * @return true if the deck has cards in or false if the deck is empty + * @return {@code true} if the {@link Deck} has {@link Card}s in, + * {@code false} otherwise */ public boolean hasCards() { return !deck.isEmpty(); } /** - * Removes all Card objects from the deck. + * Removes all {@link Card} objects from the {@link Deck}. */ public void emptyDeck() { deck.clear(); } /** - * Adds ArrayList of cards to deck. + * Adds an {@link ArrayList} of {@link Card} objects to the {@link Deck}. * - * @param cards ArrayList of cards to be added to deck + * @param cards the {@link ArrayList} of {@link Card} objects to be added to + * {@link Deck} */ public void addCards(ArrayList<Card> cards) { deck.addAll(cards); } /** - * Returns ArrayList containing all the cards in the deck. + * Returns an {@link ArrayList} containing all the {@link Card} objects in + * the {@link Deck}. * - * @return deck ArrayList with the cards + * @return the {@link ArrayList} with the cards in the {@link Deck} */ public ArrayList<Card> getCards() { return deck; @@ -150,8 +155,8 @@ public class Deck { ret += card; ret += "\n"; } - + return ret; } - + } diff --git a/src/model/Person.java b/src/model/Person.java @@ -21,7 +21,8 @@ public abstract class Person { private String name; /** - * Creates a person with an empty hand and no name. + * Constructs a {@link Person} object with an empty {@link Hand} and no + * name. */ public Person() { this.hand = new Hand(); @@ -29,34 +30,34 @@ public abstract class Person { } /** - * Returns the hand of the person. + * Returns the {@link Hand} of the {@link Person}. * - * @return the hand of the person + * @return the {@link Hand} of the {@link Person} */ public Hand getHand() { return hand; } /** - * Sets the hand of the person. + * Sets the {@link Hand} of the {@link Person}. * - * @param hand the hand to set + * @param hand the {@link Hand} to set */ public void setHand(Hand hand) { this.hand = hand; } /** - * Returns the name of the person. + * Returns the name of the {@link Person}. * - * @return the name of the person + * @return the name of the {@link Person} */ public String getName() { return name; } /** - * Sets the name of the person. + * Sets the name of the {@link Person}. * * @param name the name to set */ @@ -65,39 +66,42 @@ public abstract class Person { } /** - * Checks if the person has blackjack. + * Checks if a {@link Person} has blackjack. * - * @return true if the person has a blackjack, false otherwise + * @return {@code true} if the {@link java.util.ArrayList} has a blackjack, + * {@code false} otherwise */ public boolean hasBlackjack() { return this.getHand().calculateValue() == 21; } /** - * Prints a formatted version of the person's hand. + * Prints a formatted version of the person's {@link Hand}. * - * @return a formatted string representing the person's hand + * @return a formatted {@code String} representing the person's {@link Hand} */ public String printHand() { return this.hand + ""; } /** - * Prints the file path of a card at a given index + * Prints the file path of a {@link Card} at a given index * - * @param i index of the card - * @return file path of the card at index i + * @param i index of the {@link Card} + * @return file path of the {@link Card} at index {@code i} */ public String printFilePath(int i) { return hand.getCardFilePath(i); } /** - * Draws a card from the deck and adds it to the person's hand. If the deck - * has no cards, the deck is reloaded from the discard pile. + * Draws a {@link Card} from the {@link Deck} and adds it to the person's + * {@link Hand}. If the {@link Deck} has no cards, the {@link Deck} is + * reloaded from the discard pile. * - * @param deck the deck to draw from - * @param discard the discard pile to reload from if the deck is empty + * @param deck the {@link Deck} to draw from + * @param discard the discard pile to reload from if the {@link Deck} is + * empty */ public void hit(Deck deck, Deck discard) { if (!deck.hasCards()) { diff --git a/src/model/Player.java b/src/model/Player.java @@ -25,24 +25,22 @@ import java.util.Scanner; public class Player extends Person { /** - * o < Hi! Hello!> \o - * /|\ |\ - * /\ /\ + * o < Hi! Hello!> \o /|\ |\ /\ /\ */ private double balance; // player's balance private final String BALANCE_FILE = "db/balance.txt"; // file to store the player's balance /** - * Creates a new player with a default name of "Player". + * Constructs a new {@link Player} object with a default name of "Player". */ public Player() { super.setName("Player"); } /** - * Gets the player's current balance from a file. If a FileNotFoundException - * is thrown during the file reading process, it is caught and printed to - * the standard error stream. + * Gets the player's current balance from a file. If a + * {@link FileNotFoundException} is thrown during the file reading process, + * it is caught and printed to the standard error stream. * * @return balance the player's current balance */ @@ -57,9 +55,9 @@ public class Player extends Person { } /** - * Sets the player's balance and stores it in a file.If a - * FileNotFoundException is thrown during the file reading process, it is - * caught and printed to the standard error stream. + * Sets the player's balance and stores it in a file. If a + * {@link FileNotFoundException} is thrown during the file reading process, + * it is caught and printed to the standard error stream. * * @param balance the new balance to set */ diff --git a/src/model/Statistics.java b/src/model/Statistics.java @@ -34,7 +34,8 @@ public class Statistics { private int pushes; /** - * Constructs a new Statistics object with all statistics initialized to 0. + * Constructs a new {@link Statistics} object with all statistics + * initialized to 0. */ public Statistics() { this.wins = 0; @@ -43,7 +44,7 @@ public class Statistics { } /** - * Constructs a new Statistics object with the given statistics. + * Constructs a new {@link Player} object with the given statistics. * * @param wins the number of wins * @param losses the number of losses @@ -58,7 +59,7 @@ public class Statistics { /** * Gets the number of wins. * - * @return number of wins + * @return the number of wins */ public int getWins() { return wins; @@ -67,7 +68,7 @@ public class Statistics { /** * Gets the number of losses. * - * @return number of losses + * @return the number of losses */ public int getLosses() { return losses; @@ -76,7 +77,7 @@ public class Statistics { /** * Gets the number of pushes (ties). * - * @return number of pushes + * @return the number of pushes */ public int getPushes() { return pushes; @@ -140,12 +141,12 @@ public class Statistics { /** * <p> * This method saves the current statistics (number of wins, losses, and - * pushes) to a CSV_FILE file. The file path is defined by the CSV_FILE - * constant in the Statistics class.</p> + * pushes) to the {@code CSV_FILE} file. The file path is defined by the + * {@code CSV_FILE} constant in the {@link Statistics} class.</p> * <p> - * The method uses a CSVWriter object to write the data to the file. If an - * IOException occurs during the file writing process, it is caught and - * printed to the standard error stream.</p> + * The method uses a {@link CSVWriter} object to write the data to the file. + * If an {@link IOException} occurs during the file writing process, it is + * caught and printed to the standard error stream.</p> */ private void save() { try (CSVWriter writer = new CSVWriter(new FileWriter(CSV_FILE))) { @@ -159,23 +160,26 @@ public class Statistics { /** * <p> - * This static method loads the statistics from the CSV file located at the - * path defined by the CSV_FILE constant in the Statistics class. The method - * uses a CSVReader object to read the data from the file.</p> + * This static method loads the statistics from the {@code CSV_FILE} file + * located at the path defined by the {@code CSV_FILE} constant in the + * {@link Statistics} class. The method uses a {@link CSVReader} object to + * read the data from the file.</p> * <p> - * If the CSV file does not have the expected headers, an IOException is - * thrown. The method returns a Statistics object with the values read from - * the CSV file.</p> + * If the {@code CSV_FILE} file does not have the expected headers, an + * {@link IOException} is thrown. The method returns a {@link Statistics} + * object with the values read from the {@code CSV_FILE} file.</p> * <p> - * If an IOException occurs during the file reading process, it is caught - * and printed to the standard error stream. If a CsvValidationException is - * thrown during the file reading process, it is handled internally and an - * empty Statistics object is returned.</p> + * If an {@link IOException} occurs during the file reading process, it is + * caught and printed to the standard error stream. If a + * {@link CsvValidationException} is thrown during the file reading process, + * it is handled internally and an empty {@link Statistics} object is + * returned.</p> * - * @return a Statistics object with the values read from the CSV file, or an - * empty Statistics object 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 + * @return a {@link Statistics} object with the values read from the + * {@code CSV_FILE} file, or an empty {@link Statistics} object 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 */ public static Statistics load() throws IOException { Statistics stats = new Statistics(); diff --git a/src/view/MainUI.java b/src/view/MainUI.java @@ -3,12 +3,8 @@ package view; import com.formdev.flatlaf.FlatDarkLaf; import controller.Controller; import java.awt.Toolkit; -import java.awt.event.ActionEvent; -import java.awt.event.KeyEvent; import javax.swing.ImageIcon; -import javax.swing.JComponent; import javax.swing.JOptionPane; -import javax.swing.KeyStroke; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; diff --git a/src/view/StatisticsChart.java b/src/view/StatisticsChart.java @@ -69,9 +69,9 @@ public class StatisticsChart extends javax.swing.JPanel { /** * Creates a pie chart based on the specified dataset and title. * - * @param dataset dataset to use for the chart - * @param title title to display on the chart - * @return the chart object + * @param dataset the dataset to use for the chart + * @param title the title to display on the chart + * @return the {@link JFreeChart} object */ private JFreeChart createChart(PieDataset dataset, String title) {