From 493d7033d1720a7faccec80d2d2f65ca30b5b124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Einar=20Sk=C3=B8ien?= Date: Mon, 18 May 2026 22:17:44 +0200 Subject: [PATCH] fix checkstyle and add javadoc to certain model classes --- .../exceptions/StockFileParseException.java | 23 +++++--- .../gruppe42/model/transaction/Purchase.java | 29 +++++++-- .../gruppe42/model/transaction/Sale.java | 27 +++++++-- .../model/transaction/Transaction.java | 42 +++++++------ .../model/transaction/TransactionArchive.java | 59 +++++++++++++++---- 5 files changed, 131 insertions(+), 49 deletions(-) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/exceptions/StockFileParseException.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/exceptions/StockFileParseException.java index 07f4afa..6a3023c 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/exceptions/StockFileParseException.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/exceptions/StockFileParseException.java @@ -1,15 +1,22 @@ package edu.ntnu.idi.idatt2003.gruppe42.model.exceptions; +/** + * Exception thrown when an error occurs while parsing a stock file. + * + *

This exception includes information about the line number and the content + * of the line where the parsing error occurred. + */ public class StockFileParseException extends Exception { - private final int lineNumber; - private final String lineContent; + /** + * Creates a new StockFileParseException with details about the parsing error. + * + * @param lineNumber the line number where the error occurred + * @param lineContent the content of the line that caused the error + * @param reason a description of why the parsing failed + */ public StockFileParseException(int lineNumber, String lineContent, String reason) { - super(String.format("Parse error on line %d: %s%n Content : \"%s\"", lineNumber, reason, lineContent)); - this.lineNumber = lineNumber; - this.lineContent = lineContent; + super(String.format("Parse error on line %d: %s%n Content : \"%s\"", + lineNumber, reason, lineContent)); } - - public int getLineNumber() { return lineNumber; } - public String getLineContent() { return lineContent; } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/Purchase.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/Purchase.java index 30dfc20..8f22e11 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/Purchase.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/Purchase.java @@ -1,20 +1,37 @@ package edu.ntnu.idi.idatt2003.gruppe42.model.transaction; -import edu.ntnu.idi.idatt2003.gruppe42.model.calculator.PurchaseCalculator; import edu.ntnu.idi.idatt2003.gruppe42.model.Player; import edu.ntnu.idi.idatt2003.gruppe42.model.Share; +import edu.ntnu.idi.idatt2003.gruppe42.model.calculator.PurchaseCalculator; -/** Represents a stock purchase. */ +/** + * Represents a stock purchase transaction. + * + *

A purchase withdraws money from a player based on the calculated total cost + * and records the transaction in the player's transaction archive. + */ public final class Purchase extends Transaction { - /** Constructs a purchase. */ + + /** + * Constructs a new purchase transaction for a given share and week. + * + * @param share the share being purchased + * @param week the week in which the purchase occurs + */ public Purchase(final Share share, final int week) { super(share, week, new PurchaseCalculator(share)); } - /** Commits the purchase. */ + /** + * Commits the purchase transaction. + * + *

This method withdraws the total cost from the player and adds the transaction + * to the player's transaction archive. The transaction is marked as committed. + * + * @param player the player executing the purchase + */ @Override - public void commit(final Player player) throws Exception { - setCommitted(true); + public void commit(final Player player) { player.withdrawMoney(getCalculator().calculateTotal()); player.getTransactionArchive().add(this); } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/Sale.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/Sale.java index c509a39..1ccb8aa 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/Sale.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/Sale.java @@ -1,20 +1,37 @@ package edu.ntnu.idi.idatt2003.gruppe42.model.transaction; -import edu.ntnu.idi.idatt2003.gruppe42.model.calculator.SaleCalculator; import edu.ntnu.idi.idatt2003.gruppe42.model.Player; import edu.ntnu.idi.idatt2003.gruppe42.model.Share; +import edu.ntnu.idi.idatt2003.gruppe42.model.calculator.SaleCalculator; -/** Represents a stock sale. */ +/** + * Represents a stock sale transaction. + * + *

A sale adds money to a player based on the calculated total value of the share + * and records the transaction in the player's transaction archive. + */ public final class Sale extends Transaction { - /** Constructs a sale. */ + + /** + * Constructs a new sale transaction for a given share and week. + * + * @param share the share being sold + * @param week the week in which the sale occurs + */ public Sale(final Share share, final int week) { super(share, week, new SaleCalculator(share)); } - /** Commits the sale. */ + /** + * Commits the sale transaction. + * + *

This method adds the total value of the sale to the player and records the + * transaction in the player's transaction archive. The transaction is marked as committed. + * + * @param player the player executing the sale + */ @Override public void commit(final Player player) { - setCommitted(true); player.addMoney(getCalculator().calculateTotal()); player.getTransactionArchive().add(this); } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/Transaction.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/Transaction.java index 196825b..60d72be 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/Transaction.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/Transaction.java @@ -1,17 +1,28 @@ package edu.ntnu.idi.idatt2003.gruppe42.model.transaction; -import edu.ntnu.idi.idatt2003.gruppe42.model.calculator.TransactionCalculator; import edu.ntnu.idi.idatt2003.gruppe42.model.Player; import edu.ntnu.idi.idatt2003.gruppe42.model.Share; +import edu.ntnu.idi.idatt2003.gruppe42.model.calculator.TransactionCalculator; -/** Base class for transactions. */ +/** + * Base class for all stock-related transactions. + * + *

A transaction represents an action involving a {@link Share} performed by a {@link Player} + * at a specific week. Subclasses define the specific behavior of committing the transaction, + * such as buying or selling shares. + */ public abstract class Transaction { private final Share share; private final int week; private final TransactionCalculator calculator; - private boolean committed = false; - /** Constructs a new transaction. */ + /** + * Constructs a new transaction. + * + * @param share the share involved in the transaction + * @param week the week when the transaction occurs + * @param calculator the calculator used to compute transaction values + */ protected Transaction( final Share share, final int week, @@ -20,34 +31,27 @@ protected Transaction( this.share = share; this.week = week; this.calculator = calculator; - this.committed = false; } - /** @return the share. */ public Share getShare() { return share; } - /** @return the week. */ public int getWeek() { return week; } - /** @return the calculator. */ public TransactionCalculator getCalculator() { return calculator; } - /** @return true if committed. */ - public boolean isCommitted() { - return committed; - } - - /** Commits the transaction. */ + /** + * Commits the transaction for the given player. + * + *

Subclasses define the specific behavior (e.g., buying or selling shares). + * + * @param player the player executing the transaction + * @throws Exception if the transaction cannot be completed + */ public abstract void commit(Player player) throws Exception; - - /** Sets committed status. */ - public void setCommitted(final boolean committed) { - this.committed = committed; - } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/TransactionArchive.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/TransactionArchive.java index fd9b15d..647d881 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/TransactionArchive.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/model/transaction/TransactionArchive.java @@ -6,17 +6,29 @@ import java.util.List; import java.util.Set; -/** Archive for past transactions. */ +/** + * Archive for storing and managing past transactions. + * + *

The archive keeps track of all {@link Transaction} objects and provides + * utility methods for filtering and analyzing them by week, type, and + * financial impact. + */ public final class TransactionArchive { - /** The list of stored transactions. */ private final List transactions; - /** Constructs an empty archive. */ + /** + * Constructs an empty transaction archive. + */ public TransactionArchive() { this.transactions = new ArrayList<>(); } - /** Adds a transaction. */ + /** + * Adds a transaction to the archive. + * + * @param transaction the transaction to add + * @return true if the transaction was successfully added, false if null + */ public boolean add(final Transaction transaction) { if (transaction == null) { return false; @@ -25,12 +37,21 @@ public boolean add(final Transaction transaction) { } } - /** @return true if empty. */ + /** + * Checks whether the archive contains no transactions. + * + * @return true if the archive is empty, false otherwise + */ public boolean isEmpty() { return transactions.isEmpty(); } - /** @return transactions for given week. */ + /** + * Returns all transactions that occurred in a specific week. + * + * @param week the week to filter by + * @return a list of transactions in the given week + */ public List getTransactions(final int week) { List transactionsThisWeek = new ArrayList<>(); @@ -43,7 +64,12 @@ public List getTransactions(final int week) { return transactionsThisWeek; } - /** @return purchases for given week. */ + /** + * Returns all purchase transactions for a given week. + * + * @param week the week to filter by + * @return a list of purchases in the given week + */ public List getPurchases(final int week) { List purchasesThisWeek = new ArrayList<>(); @@ -56,7 +82,12 @@ public List getPurchases(final int week) { return purchasesThisWeek; } - /** @return sales for given week. */ + /** + * Returns all sale transactions for a given week. + * + * @param week the week to filter by + * @return a list of sales in the given week + */ public List getSales(final int week) { List salesThisWeek = new ArrayList<>(); @@ -69,7 +100,11 @@ public List getSales(final int week) { return salesThisWeek; } - /** @return distinct weeks count. */ + /** + * Counts the number of distinct weeks that contain transactions. + * + * @return the number of unique weeks with transactions + */ public int countDistinctWeeks() { Set distinctWeeks = new HashSet<>(); @@ -81,10 +116,12 @@ public int countDistinctWeeks() { } /** - * Calculates net income for a given week. + * Calculates the net income for a given week. + * + *

Sales increase income, while purchases decrease it. * * @param week the week to calculate for - * @return net income + * @return the net income as a BigDecimal */ public BigDecimal calculateNetIncome(final int week) { BigDecimal net = BigDecimal.ZERO;