Skip to Main Content

Help understanding the right code approach/structure to use in Java

16fd4af4-4663-4c79-94f6-f9f5812ee077Oct 21 2017 — edited Oct 31 2017

I'm just getting started learning Java. I'm working on a simple console game to learn as I go, and I'd like to get a better understanding of how I should structure my code (see below).

So far, I've just been writing it in a linear fashion as I logically think through each step, but I suspect this is not the right way to proceed in Java. I get the feeling that I should be splitting my code into various classes, objects, methods, etc?

How would you structure the below code (keeping in mind I'm a beginner, so simple is better.)?

Eg should I create a separate class/object for character and use that to set/store the name, race, & stats?

Then create classes, constructors, and methods for each of the game actions? Eg a class and object for Beer, and a drink method.

import java.util.Scanner;

import java.util.Random;

public class LOTRGame {

   /**

    * @param args the command line arguments

    */

   public static void main(String[] args) {

       System.out.printf("Welcome, traveler, to the Green Dragon Inn! What's your name? %n");

       Scanner scanIn = new Scanner(System.in);

       String characterName = scanIn.nextLine();

       boolean invalidRace;

       do {

       System.out.printf("Come on in, %s. Pardon me for asking, but what race are you? %n", characterName);

       String characterRace = scanIn.nextLine();

       invalidRace = (!characterRace.equalsIgnoreCase("elf") && !characterRace.equalsIgnoreCase("human") && !characterRace.equalsIgnoreCase("hobbit") && !characterRace.equalsIgnoreCase("wizard") && !characterRace.equalsIgnoreCase("dwarf"));

        if (invalidRace) {

          System.out.printf("Never heard of that.%n");

        }

        else {

                   

       System.out.printf("'Tis a windy night out! Step on in by the fireplace, %s. We're always happy to welcome a friendly %s to our company!%n", characterName, characterRace);

        }

       } while (invalidRace);

      

Random rand = new Random();

int coins = rand.nextInt(30) + 10;

int health = 90;

int energy = 50;

String doNext;

boolean goOn;

do {

System.out.printf("---You have %s gold coins, %s health, and %s energy.---%n%nWhat would you like to do?%n---Your options are Beer, Gamble, Dance, Or Sleep---%n", Integer.toString(coins), Integer.toString(health), Integer.toString(energy));

doNext = scanIn.nextLine();  

goOn = (!doNext.equalsIgnoreCase("Sleep"));

if (doNext.equalsIgnoreCase("Beer")) {

  coins = --coins;

  health = health - 5;

  energy = energy + 2;

}

else if (doNext.equalsIgnoreCase("Gamble")) {

   System.out.printf("How many coins do you want to bet?%n");

   String betCoins = scanIn.nextLine();

   int bet = Integer.parseInt(betCoins);

   Random randBet = new Random();

   int randBetResult = randBet.nextInt(2) + 1;

   if (randBetResult == 1) {

  System.out.printf("You lose! -%s coins.%n", betCoins);

  coins = coins - bet;

}

   else {

  System.out.printf("You win! +%s coins.%n", betCoins);

  coins = coins + bet;

}

}

else if (doNext.equalsIgnoreCase("Dance")) {

  System.out.printf("Nice dancing! Here's a coin for a beer on the house.");

  coins = ++coins;

  energy = energy - 2;

}

else if (doNext.equalsIgnoreCase("Sleep")) {

System.out.printf("Good night!%n");

exit(0)

}

} while (goOn);

  scanIn.close(); 

   }

   }

Comments
Post Details
Added on Oct 21 2017
2 comments
304 views