Skip to content

Instantly share code, notes, and snippets.

@JoinedSenses
Created February 7, 2018 23:19
Show Gist options
  • Save JoinedSenses/bfdbf8fa59653566bda41469da78cc18 to your computer and use it in GitHub Desktop.
Save JoinedSenses/bfdbf8fa59653566bda41469da78cc18 to your computer and use it in GitHub Desktop.
Make your dog happy.
import java.util.Scanner;
class Dog {
String dogName;
String command;
boolean wanttoplay;
boolean hungry;
Scanner keyboard = new Scanner(System.in);
void setName() {
System.out.print("What is your dogs name?: ");
dogName = keyboard.next();
System.out.print("Your dogs name is " + dogName + ". Is this correct? [y/n] ");
this.checkName();
}
void checkName() {
command = keyboard.next();
if (command.equals("n") || command.equals("N")) {
this.setName();
} else if (!(command.equals("y") || command.equals("Y") || command.equals("yes"))) {
System.out.println("Incorrect input.");
System.out.print("Your dogs name is " + dogName + ". Is this correct? [y/n] ");
this.checkName();
}
}
void stare() {
System.out.println(this.dogName + " sits and stares at you.");
this.command();
}
void command() {
System.out.println();
System.out.println("What would you like to do?");
command = keyboard.next();
if (command.equals("play")) {
this.play();
} else if (command.equals("feed")) {
this.feed();
} else if (command.equals("pet")) {
this.pet();
} else if (command.equals("quit") || command.equals("exit")) {
System.exit(0);
} else if (command.equals("restart") || command.equals("reset")) {
this.restart();
} else {
System.out.println("Unknown command");
this.command();
}
}
void play() {
if (this.wanttoplay) {
System.out.println(dogName + " plays with you!");
this.wanttoplay = false;
this.hungry = true;
} else {
System.out.println(dogName + " doesn't want to play.");
}
this.command();
}
void feed() {
if (this.hungry) {
System.out.println(dogName + " eats the food.");
this.hungry = false;
System.out.println("Congrats! Your dog is happy!");
} else {
System.out.println(dogName + " isn't hungry.");
this.command();
}
}
void pet() {
System.out.println(dogName + " drools all over you.");
this.wanttoplay = true;
this.command();
}
void restart() {
String[] args = new String[0]; // Or String[] args = {};
DogCommand.main(args);
}
}
class DogCommand {
String dogName;
String command;
Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("The goal is to make your dog happy.");
System.out.println("Commands: play, feed, pet");
System.out.println();
Dog dog = new Dog();
dog.setName();
dog.stare();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment