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

Improving my Java object-oriented? Review!

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

Problem

Did I improve since then?

I have been writing this program today, and tried my best to improve my object-oriented understanding.

Mains.java:

import games.GameHandler;
import java.util.Scanner;
import java.io.*;

    public class Mains {

        public static void main (String[] args) {
            //Start the game
            startGame();

        }

        private static void startGame() {

            //Declares
            GameHandler handler = new GameHandler();
            Scanner console = new Scanner(System.in);   
            boolean game = true;
            String input = "";  

            //Print program welcome text
            handler.printStart();

            //While in game...
            while (game) {
                //Getting input ready for new commands from the player
                input = console.nextLine();

                //Checking if input was set.
                if (input != null) {
                    //Selecting the game you want to play.
                    handler.selectGame(input);

                    //If game was selected.. then.. let's start playing.
                    while (handler.inGame) {
                        //Use will say something.
                        input = console.nextLine();

                        //If it was "exit", it will go back and select another game.
                        if (input.equals("exit")) {
                            handler.exitGame();
                        } else {
                            //Play again.
                            handler.continueGame(input);
                        }
                    }
                }
            }
        }
    }


GameHandler.java:

```
package games;
import java.io.*;

public class GameHandler {

private String[] games = {"Spin", "Tof"};
private String[] navigation = {"Back", "Start"};
private Spin spin = new Spin();
private boolean spinGame = false;
private boolean tofGame = false;

Solution

i would suggest to change

private boolean inArray(String value) {
        int returning = 0;
        for (String s : games) {
            if (value.equalsIgnoreCase(s)) {
                returning = 1;
            }
        }
        if (returning == 1) {
            return true;
        } else {
            return false;
        }
    }


to

private boolean inArray(String value) {
        boolean doesExist = false;
        for (String s : games) {
            if (value.equalsIgnoreCase(s)) {
                doesExist = true;
                break;
            }
        }
        return doesExist;
    }


To OP : Pretty impressive improvement since then.

Put this

public class GameHandler {

    private String[] games = {"Spin", "Tof"};
    private String[] navigation = {"Back", "Start"};
    private Spin spin = new Spin();
    private boolean spinGame = false;
    private boolean tofGame = false;
    public boolean inGame = false;


into the GameHandler constructor, it is a convention to define non-static variable in the class constructor.

You have a massive error in your Spin class. you have declared private int auth = this.rand(1000) / 5;. So do you know how this keyword works? i don't think so, after running your new code and judge it by the previous i think that you only try break your huge code into smaller part without thinking the logic how it will be executed. I will recommend you to at first do some basic Java programming with small classes and methods and you should start with the official tutorial. Good Luck.

Code Snippets

private boolean inArray(String value) {
        int returning = 0;
        for (String s : games) {
            if (value.equalsIgnoreCase(s)) {
                returning = 1;
            }
        }
        if (returning == 1) {
            return true;
        } else {
            return false;
        }
    }
private boolean inArray(String value) {
        boolean doesExist = false;
        for (String s : games) {
            if (value.equalsIgnoreCase(s)) {
                doesExist = true;
                break;
            }
        }
        return doesExist;
    }
public class GameHandler {

    private String[] games = {"Spin", "Tof"};
    private String[] navigation = {"Back", "Start"};
    private Spin spin = new Spin();
    private boolean spinGame = false;
    private boolean tofGame = false;
    public boolean inGame = false;

Context

StackExchange Code Review Q#28197, answer score: 4

Revisions (0)

No revisions yet.