HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Simple magic 8 ball program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
ballmagicsimpleprogram

Problem

I would just like some input on how I may improve my code(in terms of content not formatting) without changing the output. It's just a simple magic 8 ball program using dialog boxes.

```
import java.security.SecureRandom;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class MagicEightBall {

public static void main(String[] args) {

String answers[] = {"It is certain", "It is decidedly so", "Without a doubt",
"Yes - definitely", "You may rely on it", "As I see it, yes",
"Most likely", "Outlook good", "Signs point to yes",
"Yes", "Reply hazy, try again", "Ask again later",
"Better not tell you now", "Cannot predict now", "Concentrate and ask again",
"Don't count on it", "My reply is no", "My sources say no",
"Outlook not so good", "Very doubtful"};

//constructs a random number generator
SecureRandom randomNumber = new SecureRandom();
ImageIcon image = new ImageIcon("8ball_small.jpg");
int counter = 0;

//while loop allows the user to continue asking questions until they click no
while(counter != 1) {
//prompts the user to enter a question
String answer = JOptionPane.showInputDialog(null,
"PLease enter a yes or no question:",
"WELCOME: Have your questions answered!", JOptionPane.INFORMATION_MESSAGE);

//displays the answer
if(answer != null)
JOptionPane.showMessageDialog(null, answer+ "\n" + answers[randomNumber.nextInt(answers.length)],
"The Magic-8 Ball has responded.", JOptionPane.PLAIN_MESSAGE, image);

//gives the user the option to click yes or no to continue asking questions
counter = JOptionPane.showConfirmDialog(null, "", "Would you like to ask again?",
JOptionPane.YES_NO_OPTION, 0, image);
}

/

Solution

I did some refactoring... I disassembled everything into smaller pieces. Maybe the naming of the methods and vars could be better (I'm sure it can), but I wanted to show you, how to split 'stuff' into smaller pieces of code to make the main method (not meaning the main method in your code explicitly) tell the summary and move the details to separate methods.

public class MagicEightBall {

    private final static SecureRandom randomNumber = new SecureRandom();
    private final static String answers[] = {
            "It is certain",
            "It is decidedly so",
            "Without a doubt",
            "Yes - definitely",
            "You may rely on it",
            "As I see it, yes",
            "Most likely",
            "Outlook good",
            "Signs point to yes",
            "Yes",
            "Reply hazy, try again",
            "Ask again later",
            "Better not tell you now",
            "Cannot predict now",
            "Concentrate and ask again",
            "Don't count on it",
            "My reply is no",
            "My sources say no",
            "Outlook not so good",
            "Very doubtful" };
    private final static ImageIcon image = new ImageIcon("8ball_small.jpg");

    public static void main(String[] args) {
        boolean askQuestion = true;

        while (askQuestion) {
            String question = getUserQuestion();
            String randomAnswer = getRandomAnswer();

            displayAnswer(question, randomAnswer);

            askQuestion = userWantsToAskAnotherQuestion();
        }

        showExitMessage();
    }

    private static String getUserQuestion() {
        return JOptionPane.showInputDialog(null,
                "PLease enter a yes or no question:",
                "WELCOME: Have your questions answered!",
                JOptionPane.INFORMATION_MESSAGE);
    }

    private static String getRandomAnswer() {
        return answers[randomNumber.nextInt(answers.length)];
    }

    private static void displayAnswer(String question, String randomAnswer) {
        JOptionPane.showMessageDialog(null, question + "\n" + randomAnswer, "The Magic-8 Ball has responded.", JOptionPane.PLAIN_MESSAGE, image);
    }

    private static boolean userWantsToAskAnotherQuestion() {
        return 0 == JOptionPane.showConfirmDialog(null, "", "Would you like to ask again?", JOptionPane.YES_NO_OPTION, 0, image);
    }

    private static void showExitMessage() {
        JOptionPane.showMessageDialog(null, "Programmed by my name", "Goodbye! Your answers have been answerd.", JOptionPane.PLAIN_MESSAGE, image);
    }
}

Code Snippets

public class MagicEightBall {

    private final static SecureRandom randomNumber = new SecureRandom();
    private final static String answers[] = {
            "It is certain",
            "It is decidedly so",
            "Without a doubt",
            "Yes - definitely",
            "You may rely on it",
            "As I see it, yes",
            "Most likely",
            "Outlook good",
            "Signs point to yes",
            "Yes",
            "Reply hazy, try again",
            "Ask again later",
            "Better not tell you now",
            "Cannot predict now",
            "Concentrate and ask again",
            "Don't count on it",
            "My reply is no",
            "My sources say no",
            "Outlook not so good",
            "Very doubtful" };
    private final static ImageIcon image = new ImageIcon("8ball_small.jpg");

    public static void main(String[] args) {
        boolean askQuestion = true;

        while (askQuestion) {
            String question = getUserQuestion();
            String randomAnswer = getRandomAnswer();

            displayAnswer(question, randomAnswer);

            askQuestion = userWantsToAskAnotherQuestion();
        }

        showExitMessage();
    }

    private static String getUserQuestion() {
        return JOptionPane.showInputDialog(null,
                "PLease enter a yes or no question:",
                "WELCOME: Have your questions answered!",
                JOptionPane.INFORMATION_MESSAGE);
    }

    private static String getRandomAnswer() {
        return answers[randomNumber.nextInt(answers.length)];
    }

    private static void displayAnswer(String question, String randomAnswer) {
        JOptionPane.showMessageDialog(null, question + "\n" + randomAnswer, "The Magic-8 Ball has responded.", JOptionPane.PLAIN_MESSAGE, image);
    }

    private static boolean userWantsToAskAnotherQuestion() {
        return 0 == JOptionPane.showConfirmDialog(null, "", "Would you like to ask again?", JOptionPane.YES_NO_OPTION, 0, image);
    }

    private static void showExitMessage() {
        JOptionPane.showMessageDialog(null, "Programmed by my name", "Goodbye! Your answers have been answerd.", JOptionPane.PLAIN_MESSAGE, image);
    }
}

Context

StackExchange Code Review Q#147649, answer score: 2

Revisions (0)

No revisions yet.