I've just started programming and read a book called Head First Java.
I thought it would be fun to make a training project that I can put all the stuff I learn into. Is the code I've written here just really bad or good code written for a beginner?
I've got 4 classes:
Main class:
public class Main {
Player p = new Player();
p.Decision();
}
Player Class:
public class Player {
int decision = 0;
int exit = 1;
int wd;
int yesorno;
boolean firstTimePlaying = true;
Game g = new Game();
Scanner s = new Scanner(System.in);
public void Decision() {
if (firstTimePlaying == true) {
g.firstTime();
firstTimePlaying = false;
}
while (true) {
System.out.println("Go out for a walk with your dog: 1 | Play with your dog: 2");
decision = s.nextInt();
if (decision == 1) {
g.OutForAWalk();
} else if (decision == 2) {
g.play();
} else if (decision == 425) {
System.out.println("This is a developer option!");
System.out.println("This will make you change your dogs name and size!");
System.out.println("Do you wish to continue?");
yesorno = s.nextInt();
if (yesorno == 1)
g.firstTime();
} else {
System.out.println("Error");
}
System.out.println("Do you want to continue? Yes: 1 No: 2");
exit = s.nextInt();
if (exit > 1)
break;
}
}
Game Class:
public class Game {
int seeingAhydrant = 0;
int wantingToPoop = 0;
int seeingAnotherDog = 0;
int fun = 0;
String dogName;
int size;
Dog dog = new Dog();
Scanner s = new Scanner(System.in);
int chooise = 1;
public void firstTime() {
System.out.println("Welcome to this dog owner sim game!");
System.out.println("Please write the name of your new dog: ");
dogName = s.nextLine();
System.out.println("Please write the size of your new dog: ");
size = s.nextInt();
dog.setName(dogName);
dog.setSize(size);
System.out.println("Now the game starts!");
}
public void OutForAWalk() {
System.out.println("Player takes dog out for a walk");
while (chooise < 2) {
wantingToPoop += 2;
seeingAhydrant = (int) (Math.random() * 11);
seeingAnotherDog = (int) (Math.random() * 11);
if (seeingAhydrant > 4) {
dog.pee();
} else if (wantingToPoop > 4) {
dog.poop();
wantingToPoop = 0;
} else if (seeingAnotherDog > 7) {
System.out.println("Suddenly another dog appear!");
dog.bark();
} else {
System.out.println("Nothing special happend on thoose 100 meters");
}
System.out.println("Walk another 100 meters? 1 = yes/2 = no");
chooise = s.nextInt();
}
chooise = 0;
}
// Bug inside play method, sometimes the dog dosen't show feelings (amuse, bored etc.)
public void play() {
while (chooise < 2) {
System.out.println("You play with your dog.");
fun = (int) (Math.random() * 11);
if (fun < 1) {
dog.verybored();
} else if (fun > 0 && fun < 5) {
dog.bored();
} else if (fun > 4 && fun < 8) {
dog.amused();
dog.wiggleTail();
} else if (fun > 7 && fun < 11) {
dog.veryamused();
dog.bark();
dog.wiggleTail();
}
System.out.println("Do you want to play more with your dog? yes=1/no=2");
chooise = s.nextInt();
}
chooise = 0;
}
}
Dog Class:
public class Dog {
private String name;
private int size;
public void bark() {
if (size > 4)
System.out.println(name + ": Woof Woof");
else
System.out.println(name + ": yip yip");
}
public void pee() {
System.out.println(name + ": *Sees a hydrant* *peeing*");
}
public void poop() {
System.out.println(name + ": *poops*");
}
public void wiggleTail() {
System.out.println(name + ": *Wiggles tail*");
}
public void verybored() {
System.out.println(name + ": *Looks really bored*");
}
public void bored() {
System.out.println(name + ": *Looks bored*");
}
public void amused() {
System.out.println(name + ": Looks amused");
}
public void veryamused() {
System.out.println(name + ": Looks very amused");
}
//Getters and setters
public int getSize() {
return size;
}
public void setSize(int s) {
if (s < 1) {
System.out.println("Size can't be less then one!");
System.out.println("Setting size to one!");
size = 1;
} else if (s > 10) {
System.out.println("Size can't be more then ten!");
System.out.println("Setting size to ten!");
size = 10;
} else {
size = s;
}
}
public String getName() {
return name;
}
public void setName(String n) {
name = n;
}
}