Advertisement
Guest User

Same Interface for Classes with some common attributes

a guest
May 18th, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. public class CardContainer {
  2.     List<Card> cards;
  3.  
  4.     public static void main(String argv[]) {
  5.          CardContainer cardContainer = new CardContainer();
  6.          cardContainer.addCard(new CardWithActions());
  7.          cardContainer.addCard(new CardWithBonus());
  8.          // I need to do something like
  9.          String actionReward = cardContainer.getCard(0).getActionReward();        
  10.     }
  11.  
  12.     public CardContainer() {
  13.         cards = new ArrayList<>();
  14.     }
  15.  
  16.     public void addCard(Card card) {
  17.         cards.add(card);
  18.     }
  19.    
  20.     public Card getCard(int position) {
  21.         return cards.get(position);
  22.     }
  23. }
  24.  
  25.  
  26. public abstract class Card {
  27.     protected String cardName;
  28.     protected List<String> cost;
  29.     protected String effect;
  30.  
  31.     public Card() {
  32.     }
  33.  
  34.     public Card(String cardName, List<String> cost, String effect) {
  35.         this.cardName = cardName;
  36.         this.cost = cost;
  37.         this.effect = effect;
  38.     }
  39.  
  40.     public String getCardName() {
  41.         return cardName;
  42.     }
  43.  
  44.     public void setCardName(String cardName) {
  45.         this.cardName = cardName;
  46.     }
  47.  
  48.     public List<String> getCost() {
  49.         return cost;
  50.     }
  51.  
  52.     public void setCost(List<String> cost) {
  53.         this.cost = cost;
  54.     }
  55.  
  56.     public String getEffect() {
  57.         return effect;
  58.     }
  59.  
  60.     public void setEffect(String effect) {
  61.         this.effect = effect;
  62.     }
  63. }
  64.  
  65. public class CardWithActions extends Card {
  66.     private String action;
  67.     private String actionReward;
  68.     private String actionCost;
  69.  
  70.     public CardWithActions() {
  71.         super();
  72.     }
  73.  
  74.     public CardWithActions(String cardName, List<String> cost, String effect,
  75.                            String action, String actionReward, String actionCost) {
  76.         super(cardName, cost, effect);
  77.         this.action = action;
  78.         this.actionReward = actionReward;
  79.         this.actionCost = actionCost;
  80.     }
  81.  
  82.     public String getAction() {
  83.         return action;
  84.     }
  85.  
  86.     public void setAction(String action) {
  87.         this.action = action;
  88.     }
  89.  
  90.     public String getActionReward() {
  91.         return actionReward;
  92.     }
  93.  
  94.     public void setActionReward(String actionReward) {
  95.         this.actionReward = actionReward;
  96.     }
  97.  
  98.     public String getActionCost() {
  99.         return actionCost;
  100.     }
  101.  
  102.     public void setActionCost(String actionCost) {
  103.         this.actionCost = actionCost;
  104.     }
  105. }
  106.  
  107. public class CardWithBonus extends Card{
  108.     private String bonusType;
  109.     private String bonusDescription;
  110.  
  111.     public CardWithBonus() {
  112.     }
  113.  
  114.     public CardWithBonus(String cardName, List<String> cost, String effect,
  115.                          String bonusType, String bonusDescription) {
  116.         super(cardName, cost, effect);
  117.         this.bonusType = bonusType;
  118.         this.bonusDescription = bonusDescription;
  119.     }
  120.  
  121.     public String getBonusType() {
  122.         return bonusType;
  123.     }
  124.  
  125.     public void setBonusType(String bonusType) {
  126.         this.bonusType = bonusType;
  127.     }
  128.  
  129.     public String getBonusDescription() {
  130.         return bonusDescription;
  131.     }
  132.  
  133.     public void setBonusDescription(String bonusDescription) {
  134.         this.bonusDescription = bonusDescription;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement