Advertisement
yo2man

Refactoring code part 2

Jun 21st, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. ////Refactoring code Part 2
  2. FunFactsActivity.java
  3.  
  4. private FactBook mFactBook = new FactBook();  
  5.  
  6. public class FunFactsActivity extends Activity {
  7. @Override
  8.     public void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_fun_facts);
  11.  
  12.         final TextView factLabel = (TextView) findViewById(R.id.factTextView);
  13.         Button showFactButton = (Button) findViewById(R.id.showFactButton);
  14.         View.OnClickListener listenerName = new View.onClickListener(){
  15.                
  16.                 @Override
  17.                 public void onClick(View view) {
  18.  
  19.                 String fact = mFactBook.getFact();
  20.  
  21.                 factLabel.setText(fact); //
  22.                                }
  23.         };
  24.                
  25.         showFactButton.setOnClickListener(listenerName);
  26.     }
  27. }
  28.  
  29. //-----------------------------------------------------------------------------------------------------------------------------------------
  30.  
  31. FactBook.java
  32.  
  33. public class FactBook {
  34.         //Member variable (properties about the object)
  35.    
  36.     // /!\convert array of facts into property of object.
  37.     public String[] mFacts = {                                 // refactor>rename 'facts' to 'mFacts'
  38.                         "Ants stretch when they wake up in the morning.",                                
  39.                         "Ostriches can run faster than horses.",                                        
  40.                         "Olympic gold medals are actually made mostly of silver.",                      
  41.                         "You are born with 300 bones; by the time you are an adult you will have 206.",  
  42.                         "It takes about 8 minutes for light from the Sun to reach Earth.",              
  43.                         "Some bamboo plants can grow almost a meter in just one day.",                  
  44.                         "The state of Florida is bigger than England.",                                  
  45.                         "Some penguins can leap 2-3 meters out of the water.",                          
  46.                         "On average, it takes 66 days to form a new habit.",                            
  47.                         "Mammoths still walked the earth when the Great Pyramid was being built." };    
  48.      
  49.      // Method (abilities: things the object can do)
  50.      public String getFact() {
  51.                 String fact = "";
  52.  
  53.                 Random randomGenerator = new Random ();
  54.                 int randomNumber = randomGenerator.nextInt(mFacts.length);  
  55.  
  56.                 fact = mFacts[randomNumber];  
  57.                
  58.                 return fact; //return = I am done with this method, here is the result.
  59.         }
  60. }
  61.  
  62. //-----------------------------------------------------------------------------------------------------------------------------------------
  63. // https://teamtreehouse.com/library/build-a-simple-android-app-new/improving-our-code/simple-refactoring-using-a-class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement