vendredi 20 janvier 2017

Random class with polymorphism android

I am building a fortune teller android app which selects different fortunes from arrays using the random class and it is based on polymorphism. I know that I coded it well but my app wont even open, Any help? Here's my code:

public class fortuneList {
Random good = new Random();
Random bad = new Random();

public String getGood() {
    String goodFortunes[] = new String[20];
    int sel = 0;
    for (int i = 0; i < 9; i++) {
        sel = good.nextInt();
    }
    goodFortunes[0] = "Adel is awesome";
    goodFortunes[1] = "Adel is cool";
    goodFortunes[2] = "Adel is nice";
    goodFortunes[3] = "Adel is gentle";
    goodFortunes[4] = "Adel is smart";
    goodFortunes[5] = "Adel is rich";
    goodFortunes[6] = "Adel is good";
    goodFortunes[7] = "This is 8";
    goodFortunes[8] = "Change the quotes later";
    return goodFortunes[sel];
}

public String getBad() {
    String badFortunes[] = new String[5];
    int sele = 0;
    for (int is = 0; is < 5; is++) {
        sele = bad.nextInt();
    }
    badFortunes[0] = "WTF";
    badFortunes[1] = "Bad 1";
    badFortunes[2] = "Nigga";
    badFortunes[3] = "bish";
    badFortunes[4] = "haha";
    return badFortunes[sele];
}

}

and here is my code on the main activity:

public class MainActivity extends AppCompatActivity {
TextView fortuneText;
Random select = new Random();
ImageView ball;
Button button;
Drawable blue = getResources().getDrawable(R.drawable.blue);
Drawable red = getResources().getDrawable(R.drawable.red);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ball = (ImageView) findViewById(R.id.ball);
    button = (Button) findViewById(R.id.button);
    fortuneText = (TextView) findViewById(R.id.fortuneText);

}

public void getFortune(View view){
    fortuneList obj = new fortuneList();
    String fortuneType[]={obj.getGood(),obj.getBad()};
    int fortuneSel = 0;
    for(int i =0; i<2;i++){
        fortuneSel = select.nextInt();
    }
    fortuneText.setText(fortuneType[fortuneSel]);
    if (fortuneSel == 0) {
        ball.setImageDrawable(blue);
    }
    if(fortuneSel == 1){
        ball.setImageDrawable(red);
    }
}


} 

Please review my code. The app wont run!.




Aucun commentaire:

Enregistrer un commentaire