Advertisement
StormWingDelta

ShowingHowToUseInterfacesAsListeners

Jan 22nd, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. //Holds all the methods to be used.
  2. //StatusChangeListener.java
  3. public interface StatusChangeListener
  4. {
  5.     void atkChanged();
  6.     void defChanged();
  7.     void levelChanged();
  8.     int getCurrentATK();
  9.     int getCurrentDEF();
  10.     int getCurrentLevel();
  11. }
  12.  
  13. //This next one is just an example of one way to use said methods above
  14. //StatusChecker.java
  15. public class StatusChecker implements StatusChangeListener {
  16.  
  17.     public StatusChecker() {
  18.         // TODO Auto-generated constructor stub
  19.     }
  20.  
  21.     @Override
  22.     public void atkChanged()
  23.     {
  24.         // TODO Auto-generated method stub
  25.         System.out.println("ATK Changed!");
  26.     }
  27.  
  28.     @Override
  29.     public void defChanged()
  30.     {
  31.         // TODO Auto-generated method stub
  32.         System.out.println("DEF Changed!");
  33.     }
  34.  
  35.     @Override
  36.     public void levelChanged()
  37.     {
  38.         // TODO Auto-generated method stub
  39.         System.out.println("Level Changed!");
  40.     }
  41.  
  42.     @Override
  43.     public int getCurrentATK()
  44.     {
  45.         // TODO Auto-generated method stub
  46.         return 0;
  47.     }
  48.  
  49.     @Override
  50.     public int getCurrentDEF()
  51.     {
  52.         // TODO Auto-generated method stub
  53.         return 0;
  54.     }
  55.  
  56.     @Override
  57.     public int getCurrentLevel()
  58.     {
  59.         // TODO Auto-generated method stub
  60.         return 0;
  61.     }
  62.  
  63.  
  64. }
  65.  
  66. //Next is our target the card class
  67. //CardTester.java
  68. public class CardTester
  69. {
  70.  
  71.     private int originalatk, originaldef, originallevel, currentatk, currentdef, currentlevel;
  72.     StatusChangeListener sclcheck;
  73.     /**
  74.      *
  75.      */
  76.     public CardTester(StatusChangeListener checker,int atk, int def, int level)
  77.     {
  78.         //Word of advice: Make sure your personal listeners are up top in constructor so you do get null errors if you're using methods in it. >_>
  79.         sclcheck = checker;
  80.         setOriginalatk(atk);
  81.         setOriginaldef(def);
  82.         setOriginallevel(level);
  83.         setCurrentatk(atk);
  84.         setCurrentdef(def);
  85.         setCurrentlevel(level);
  86.         System.out.println("Card Created!");
  87.     }
  88.     /**
  89.      * @return the originalatk
  90.      */
  91.     public int getOriginalatk()
  92.     {
  93.         return originalatk;
  94.     }
  95.     /**
  96.      * @param originalatk the originalatk to set
  97.      */
  98.     public void setOriginalatk(int oatk)
  99.     {
  100.         if(originalatk != oatk)
  101.         {
  102.             sclcheck.atkChanged();
  103.         }
  104.         this.originalatk = oatk;
  105.        
  106.     }
  107.     /**
  108.      * @return the originaldef
  109.      */
  110.     public int getOriginaldef()
  111.     {
  112.         return originaldef;
  113.     }
  114.     /**
  115.      * @param originaldef the originaldef to set
  116.      */
  117.     public void setOriginaldef(int odef)
  118.     {
  119.         if(originaldef != odef)
  120.         {
  121.             sclcheck.defChanged();
  122.         }
  123.         this.originaldef = odef;
  124.        
  125.     }
  126.     /**
  127.      * @return the originallevel
  128.      */
  129.     public int getOriginallevel()
  130.     {
  131.         return originallevel;
  132.     }
  133.     /**
  134.      * @param originallevel the originallevel to set
  135.      */
  136.     public void setOriginallevel(int olevel)
  137.     {
  138.         if(originallevel != olevel)
  139.         {
  140.             sclcheck.levelChanged();
  141.         }
  142.         this.originallevel = olevel;
  143.        
  144.     }
  145.     /**
  146.      * @return the currentatk
  147.      */
  148.     public int getCurrentatk()
  149.     {
  150.         return currentatk;
  151.     }
  152.     /**
  153.      * @param currentatk the currentatk to set
  154.      */
  155.     public void setCurrentatk(int catk)
  156.     {
  157.         if(currentatk != catk)
  158.         {
  159.             sclcheck.atkChanged();
  160.         }
  161.         this.currentatk = catk;
  162.        
  163.     }
  164.     /**
  165.      * @return the currentdef
  166.      */
  167.     public int getCurrentdef()
  168.     {
  169.         return currentdef;
  170.     }
  171.     /**
  172.      * @param currentdef the currentdef to set
  173.      */
  174.     public void setCurrentdef(int cdef)
  175.     {
  176.         if(currentdef != cdef)
  177.         {
  178.             sclcheck.defChanged();
  179.         }
  180.         this.currentdef = cdef;
  181.     }
  182.     /**
  183.      * @return the currentlevel
  184.      */
  185.     public int getCurrentlevel()
  186.     {
  187.         return currentlevel;
  188.     }
  189.     /**
  190.      * @param currentlevel the currentlevel to set
  191.      */
  192.     public void setCurrentlevel(int clevel)
  193.     {
  194.         if(currentlevel != clevel)
  195.         {
  196.             sclcheck.levelChanged();
  197.         }
  198.         this.currentlevel = clevel;
  199.     }
  200.  
  201. }
  202.  
  203. //Last is our main class.  Found out the hard way that you should never try to use your main as what is going into the listener holder of a class since you get an annoying error to do with static methods.
  204. //MainClass1.java
  205.  
  206. import java.util.Random;
  207.  
  208. public class MainClass1 //implements StatusChangeListener
  209. {
  210.  
  211.    
  212.     /**
  213.      * @param args
  214.      */
  215.     public static void main(String[] args)
  216.     {
  217.         Random rand = new Random();
  218.         StatusChecker sctest = new StatusChecker();
  219.         // TODO Auto-generated method stub
  220.         //System.out.println("Card Created!");
  221.         CardTester changetest = new CardTester(sctest, 1000, 1000, 12);
  222.         System.out.println("Status Checking Test Ready!");
  223.         int atkhold = 0;
  224.         while(changetest.getCurrentatk() <= 10000)
  225.         {
  226.             atkhold += 1000;
  227.             changetest.setCurrentatk(rand.nextInt(atkhold));
  228.             System.out.println("ATK is now: " + changetest.getCurrentatk());
  229.         }
  230.     }
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement