Record Class PurchaseResult

java.lang.Object
java.lang.Record
com.csse3200.game.components.shop.PurchaseResult
Record Components:
ok - true if the purchase was successful; false otherwise.
error - The error type if the purchase failed, or PurchaseError.NONE if successful.
entry - The purchased catalog entry, or null if the purchase failed.
qty - The quantity purchased. 0 if the purchase failed.

public record PurchaseResult(boolean ok, PurchaseError error, CatalogEntry entry, int qty) extends Record
Represents the outcome of a purchase attempt in the shop system.

A PurchaseResult indicates whether the purchase succeeded, the type of error (if any), the catalog entry involved, and the quantity purchased.

  • Constructor Details

    • PurchaseResult

      public PurchaseResult(boolean ok, PurchaseError error, CatalogEntry entry, int qty)
      Creates an instance of a PurchaseResult record class.
      Parameters:
      ok - the value for the ok record component
      error - the value for the error record component
      entry - the value for the entry record component
      qty - the value for the qty record component
  • Method Details

    • ok

      public static PurchaseResult ok(CatalogEntry entry, int qty)
      Creates a successful purchase result.
      Parameters:
      entry - The catalog entry that was purchased (must not be null).
      qty - The number of units purchased (must be > 0).
      Returns:
      A PurchaseResult indicating success with the specified entry and quantity.
    • fail

      public static PurchaseResult fail(PurchaseError e)
      Creates a failed purchase result.
      Parameters:
      e - The error indicating why the purchase failed.
      Returns:
      A PurchaseResult indicating failure with the specified error. The entry will be null and qty will be 0.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • ok

      public boolean ok()
      Returns the value of the ok record component.
      Returns:
      the value of the ok record component
    • error

      public PurchaseError error()
      Returns the value of the error record component.
      Returns:
      the value of the error record component
    • entry

      public CatalogEntry entry()
      Returns the value of the entry record component.
      Returns:
      the value of the entry record component
    • qty

      public int qty()
      Returns the value of the qty record component.
      Returns:
      the value of the qty record component