Skip to content

Commit

Permalink
Merge pull request #135 from einaskoi/einar/checksyleErrors2
Browse files Browse the repository at this point in the history
fix checkstyle and add javadoc to certain model classes
  • Loading branch information
peretr authored May 18, 2026
2 parents 9b523e5 + 493d703 commit e8900d0
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package edu.ntnu.idi.idatt2003.gruppe42.model.exceptions;

/**
* Exception thrown when an error occurs while parsing a stock file.
*
* <p>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; }
}
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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.
*
* <p>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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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.
*
* <p>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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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,
Expand All @@ -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.
*
* <p>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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,29 @@
import java.util.List;
import java.util.Set;

/** Archive for past transactions. */
/**
* Archive for storing and managing past transactions.
*
* <p>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<Transaction> 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;
Expand All @@ -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<Transaction> getTransactions(final int week) {
List<Transaction> transactionsThisWeek = new ArrayList<>();

Expand All @@ -43,7 +64,12 @@ public List<Transaction> 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<Purchase> getPurchases(final int week) {
List<Purchase> purchasesThisWeek = new ArrayList<>();

Expand All @@ -56,7 +82,12 @@ public List<Purchase> 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<Sale> getSales(final int week) {
List<Sale> salesThisWeek = new ArrayList<>();

Expand All @@ -69,7 +100,11 @@ public List<Sale> 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<Integer> distinctWeeks = new HashSet<>();

Expand All @@ -81,10 +116,12 @@ public int countDistinctWeeks() {
}

/**
* Calculates net income for a given week.
* Calculates the net income for a given week.
*
* <p>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;
Expand Down

0 comments on commit e8900d0

Please sign in to comment.