- package Magic8Ball2;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Random;
- public class Magic8Ball {
- private ArrayList<String> foo;
- public Magic8Ball(){
- }
- public static void main(String[] args){
- }
- /**
- * Adds an array list of answers to the magic8ball.
- * @param al an ArrayList of Strings
- */
- public void addArrayList(ArrayList<String> al){
- foo = al;
- populateArray();
- }
- public ArrayList<String> getArrayList(){
- return foo;
- }
- /**
- * Prints the answer corresponding with a given number.
- * @param number
- */
- public void printAnswer(String answer){
- System.out.println(answer);
- }
- public String getAnswer(int number){
- return foo.get(number);
- }
- /**
- * Prints the answer corresponding with a number, entered as a string.
- * @param input
- */
- public void answerByNumber(String input){
- int number = Integer.parseInt(input);
- printAnswer(number);
- }
- /**
- * Gives a random answer.
- */
- public void randomAnswer(){ Random rnd = new Random();
- int n = rnd.nextInt(8);
- printAnswer(n);
- }
- /**
- * You may not change this method.
- * Populates the Answers with a default answer set.
- */
- private void populateArray(){
- foo.add("Things look rosy from here.");
- foo.add("Only when the sun is shining.");
- foo.add("You have the power of the prophets behind you.");
- foo.add("I would tell you, but then I'd have to kill you.");
- foo.add("I wouldn't if I were you. But you wouldn't if you were an 8 ball.");
- foo.add("On a clear day I can see the answer. Today's forecast is cloudy.");
- foo.add("Snowball's chance in hell.");
- foo.add("I'm ashamed at you for asking.");
- }
- /**
- * You may not change this method.
- * This method accepts user input from the command line.
- */
- private void getInput(){
- System.out.print("Enter a number from 1 to 8:");
- String input = null;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- try {
- input = br.readLine();
- } catch (IOException ioe) {
- System.out.println("IO error trying to read input!");
- System.exit(1);
- }
- answerByNumber(input);
- }
- }