Coverage Summary for Class: BuildingCard (it.polimi.ingsw.gc14.Model.Cards)

Class Class, % Method, % Branch, % Line, %
BuildingCard 100% (1/1) 92.9% (13/14) 100% (25/25) 97.7% (42/43)


 package it.polimi.ingsw.gc14.Model.Cards;
 
 import it.polimi.ingsw.gc14.Model.Cards.Building.BuildingEffect;
 import it.polimi.ingsw.gc14.Model.Cards.Building.EffectType;
 import it.polimi.ingsw.gc14.Model.PlayableCard;
 import it.polimi.ingsw.gc14.Model.Player;
 
 import java.io.Serializable;
 
 /**
  * Represents a generic building card in the game.
  *
  * <p>A building card is a playable card with a price and a specific building
  * effect. Concrete building cards extend this class to define their own
  * behavior.
  */
 public class BuildingCard extends PlayableCard implements Cloneable , BuildingEffect, Serializable {
 
     /**
      * The price of this building card.
      */
     private final int price;
 
     /**
      * Returns the price of this building card.
      *
      * @return the price of this building card.
      */
     public int getPrice() {
         return price;
     }
 
     /**
      * Indicates whether this building card has already been bought.
      */
     private boolean bought;
 
     /**
      * The type of effect associated with this building card.
      */
     protected EffectType effectType;
 
     /**
      * The identifier of the effect associated with this building card.
      */
     protected int effectId;
 
     /**
      * The prestige value of this building card.
      */
     private final int prestigeValue;
 
     /**
      * Returns the prestige value of this building card.
      *
      * @return the prestige value of this building card.
      */
     public int  getPrestigeValue() {
         return prestigeValue;
     }
 
     /**
      * Returns the identifier of the effect associated with this building card.
      *
      * @return the effect identifier of this building card.
      */
     public int getEffectId() {
         return effectId;
     }
 
     /**
      * Returns the type of effect associated with this building card.
      *
      * @return the effect type of this building card.
      */
     public EffectType getEffectType() {
         return effectType;
     }
 
     /**
      * Creates a building card with the specified era, price, and prestige value.
      *
      * @param era the era of the building card.
      * @param price the price of the building card.
      * @param prestigeValue the prestige value of the building card.
      * @throws IllegalArgumentException if {@code price <= 0} or {@code prestigeValue < 0}
      */
     protected BuildingCard(int era,int price,int prestigeValue) throws IllegalArgumentException{
         super(era);
         if (price > 0) {
             this.price = price;
         } else {
             throw new IllegalArgumentException();
         }
         if (prestigeValue >= 0) {
             this.prestigeValue = prestigeValue;
         } else {
             throw new IllegalArgumentException();
         }
 
         bought = false;
     }
 
     /**
      * Creates a building card with the specified image identifier, era, price,
      * and prestige value.
      *
      * @param idIMG the image identifier of the building card.
      * @param era the era of the building card.
      * @param price the price of the building card.
      * @param prestigeValue the prestige value of the building card.
      * @throws IllegalArgumentException if {@code price <= 0} or {@code prestigeValue < 0}
      */
     protected BuildingCard(String idIMG,int era,int price,int prestigeValue) throws IllegalArgumentException{
         super(idIMG,era);
         if (price > 0) {
             this.price = price;
         } else {
             throw new IllegalArgumentException();
         }
         if (prestigeValue >= 0) {
             this.prestigeValue = prestigeValue;
         } else {
             throw new IllegalArgumentException();
         }
 
         bought = false;
     }
 
 
     /**
      * Creates a building card with the specified effect identifier, era, price,
      * and prestige value.
      * The effect type is determined from the given effect identifier.
      *
      * @param effectId the identifier of the effect associated with the building card.
      * @param era the era of the building card.
      * @param price the price of the building card.
      * @param prestigeValue the prestige value of the building card.
      * @throws IllegalArgumentException if the effect identifier is not valid,
      *         if {@code price <= 0}, or if {@code prestigeValue < 0}
      */
     public BuildingCard(int effectId, int era, int price, int prestigeValue) throws IllegalArgumentException {
         this(era, price, prestigeValue);
         this.effectType = effectTypeFromId(effectId);
         this.effectId = effectId;
     }
 
     /**
      * Creates a building card with the specified image id, effect identifier, era,
      * price, and prestige value.
      *
      * <p>The effect type is determined from the given effect identifier.
      *
      * @param idIMG the image identifier of the building card.
      * @param effectId the identifier of the effect associated with the building card.
      * @param era the era of the building card.
      * @param price the price of the building card.
      * @param prestigeValue the prestige value of the building card.
      * @throws IllegalArgumentException if the effect identifier is not valid,
      *                                  if {@code price <= 0}, or if {@code prestigeValue < 0}.
      */
     public BuildingCard(String idIMG, int effectId, int era, int price, int prestigeValue) throws IllegalArgumentException {
         this(idIMG, era, price, prestigeValue);
         this.effectType = effectTypeFromId(effectId);
         this.effectId = effectId;
     }
     /**
      * Maps a building effect identifier to its corresponding {@link EffectType}.
      *
      * @param effectId the effect identifier to resolve.
      * @return the {@link EffectType} for the given identifier.
      * @throws IllegalArgumentException if {@code effectId} does not correspond to any known effect type.
      */
     private static EffectType effectTypeFromId(int effectId) {
         switch (effectId) {
             case 2, 9, 7, 6, 5: return EffectType.ON_EVENT;
             case 3: return EffectType.ON_END_TURN;
             case 12: return EffectType.ON_ROUND_END;
             default: throw new IllegalArgumentException();
         }
     }
 
     /**
      * Creates and returns a copy of this building card.
      *
      * @return a clone of this building card.
      */
     @Override
     public BuildingCard clone()
     {
         return new BuildingCard(getIdIMG(),effectId,getEra(),getPrice(),getPrestigeValue());
     }
 
     /**
      * Attempts to buy this building card for the specified player.
      *
      * <p>The effective cost is reduced by the total reduction value provided
      * by the player's Builder cards, without dropping below zero. The purchase
      * succeeds only if the card has not already been bought and the player can
      * pay the resulting amount of Food. If successful, the card is added to
      * the player's building cards and marked as bought.
      *
      * @param player the player attempting to buy the building card.
      * @return {@code true} if the building card is successfully bought,
      *         {@code false} otherwise.
      */
     public boolean buy(Player player) {
         int discount = player.getBuilders().stream().mapToInt(x -> x.getReductionValue()).sum();
 
         if(discount > this.price){
             discount = this.price;
         }
 
         if(bought || !player.removeFood(this.price - discount))
             return false;
         player.getBuildingCards().add(this);
         bought=true;
         return true;
     }
 
     /**
      * Default implementation of the building effect.
      *
      * <p>This method does not perform any operation and can be overridden
      * by specific building cards that define an active effect.
      *
      * @param player the player to whom the effect may be applied.
      */
     public void applyEffect(Player player) {}
 
     @Override
     public String toStringBoard() {
         return "ID:" + getEffectId() + " \uD83C\uDF56:" + getPrice() + " \uD83C\uDFC5:" + getPrestigeValue();
     }
 
     @Override
     public String toStringPlayer() {
         return "ID:" + getEffectId();
     }
 }