patternjavaMinor
Game about eating fast food
Viewed 0 times
fasteatinggameaboutfood
Problem
This is a very simple text-based "game" I made in Java. The whole idea is to repeatedly type "eat/sip _____" until you eat all the food and win.
(Pertaining to skill level) I have been programming in Java for a little over two years, in high school. I feel I can be considered a beginner, but not quite enough to warrant the use of the
This is my code, please let me know if you have any suggestions, or need clarification.
EatFood.java
```
import java.util.*;
import java.lang.*;
public class EatFood {
private final String TITLE = "Eat Food";
private int minFood = 3;
private int maxFood = 5;
private Scanner in = new Scanner(System.in);
private Random rand = new Random();
private ArrayList foods = new ArrayList<>();
public EatFood() {
play();
}
public void play() {
setupFood();
System.out.println("" + TITLE + "");
System.out.print("Press Enter to continue...");
in.nextLine();
do {
String input;
System.out.println("");
displayFood();
System.out.println("What do you want to do?");
System.out.print(">");
input = in.nextLine();
boolean isValid = false;
for (int i = 0; i 0);
System.out.println("\nYou win!\n");
}
public void displayFood() {
String output = "In front of you there " + foods.get(0).getBeginList() + " ";
for (int i = 0; i < foods.size(); i++) {
output += foods.get(i).display();
output += i != foods.size() - 1 ? ", " : "";
output += i == foods.size() - 2 ? "and " : "";
}
System.out.println(output + ".");
}
public void setupFood() {
foods.add(new Burger("burger", 12, 18, "is", "a", rand));
foods.add(new CheeseBurger("cheeseburger", 12, 18, "is", "a", rand));
foods.add(new ChickenNuggets("chicken nuggets", 12, 18, "are
(Pertaining to skill level) I have been programming in Java for a little over two years, in high school. I feel I can be considered a beginner, but not quite enough to warrant the use of the
beginner tag.This is my code, please let me know if you have any suggestions, or need clarification.
EatFood.java
```
import java.util.*;
import java.lang.*;
public class EatFood {
private final String TITLE = "Eat Food";
private int minFood = 3;
private int maxFood = 5;
private Scanner in = new Scanner(System.in);
private Random rand = new Random();
private ArrayList foods = new ArrayList<>();
public EatFood() {
play();
}
public void play() {
setupFood();
System.out.println("" + TITLE + "");
System.out.print("Press Enter to continue...");
in.nextLine();
do {
String input;
System.out.println("");
displayFood();
System.out.println("What do you want to do?");
System.out.print(">");
input = in.nextLine();
boolean isValid = false;
for (int i = 0; i 0);
System.out.println("\nYou win!\n");
}
public void displayFood() {
String output = "In front of you there " + foods.get(0).getBeginList() + " ";
for (int i = 0; i < foods.size(); i++) {
output += foods.get(i).display();
output += i != foods.size() - 1 ? ", " : "";
output += i == foods.size() - 2 ? "and " : "";
}
System.out.println(output + ".");
}
public void setupFood() {
foods.add(new Burger("burger", 12, 18, "is", "a", rand));
foods.add(new CheeseBurger("cheeseburger", 12, 18, "is", "a", rand));
foods.add(new ChickenNuggets("chicken nuggets", 12, 18, "are
Solution
Structure
It seems that there are a limited number of actions. I would suggest using an enum instead of a string.
How you build your food isn't very clear when seeing a constructor call:
foods.add(new Soda("soda", 12, 18, "is", "a", rand));
The first is obviously the name, but after that, I would need to look at the documentation or code of the food constructor.
There are multiple solutions for this. You could use the builder pattern, you could combine parameters into objects (min and max cap for example might be a range, and beginList and describeAmount also seem to belong together), or you could just reduce the number of parameters by removing unnecessary ones (is there really a reason to pass rand in? Should the name of a burger ever not be burger?).
Is there a reason that the Food class doesn't build the behavior? It seems to be an action that is common to all foods.
Your play method is a bit long for my taste, and does a bit too much. I would move all the printing into their own methods (like displayIntro, displayWin, displayInput). The logic could likely also be improved. Is there a need to check foods for removal for invalid input? It doesn't seem like it, so your code may look like this:
I would move the invalid input loop into the getInput method itself though.
Documentation
You should definitely add JavaDoc comments to your public methods. It's not really clear to me what
Misc
It seems that there are a limited number of actions. I would suggest using an enum instead of a string.
How you build your food isn't very clear when seeing a constructor call:
foods.add(new Soda("soda", 12, 18, "is", "a", rand));
The first is obviously the name, but after that, I would need to look at the documentation or code of the food constructor.
There are multiple solutions for this. You could use the builder pattern, you could combine parameters into objects (min and max cap for example might be a range, and beginList and describeAmount also seem to belong together), or you could just reduce the number of parameters by removing unnecessary ones (is there really a reason to pass rand in? Should the name of a burger ever not be burger?).
Is there a reason that the Food class doesn't build the behavior? It seems to be an action that is common to all foods.
Your play method is a bit long for my taste, and does a bit too much. I would move all the printing into their own methods (like displayIntro, displayWin, displayInput). The logic could likely also be improved. Is there a need to check foods for removal for invalid input? It doesn't seem like it, so your code may look like this:
do {
displayFood();
displayInput();
String input = getInput();
if (!isValidInput(input)) {
displayInvalidInput();
continue;
}
int whichFood = 0;
for (int i = 0; i 0);I would move the invalid input loop into the getInput method itself though.
Documentation
You should definitely add JavaDoc comments to your public methods. It's not really clear to me what
behaviors are, what minCap and maxCap are refering to, what a beginList is, and so on. Better variable names and structure may help here, but in my opinion documentation is a must.Misc
- You have some style and other minor violations in your code, you can use any IDE or lint tool to find and fix them. Example would be importing
*, not making your fields private/public, or not having curly braces around one-line statements.
Code Snippets
do {
displayFood();
displayInput();
String input = getInput();
if (!isValidInput(input)) {
displayInvalidInput();
continue;
}
int whichFood = 0;
for (int i = 0; i < foods.size(); i++) {
if (input.contains(foods.get(i).getName())) {
whichFood = i;
}
}
foods.get(whichFood).doBehavior(input);
for (int i = 0; i < foods.size(); i++)
if (foods.get(i).isFinished())
foods.remove(i);
} while (foods.size() > 0);Context
StackExchange Code Review Q#140464, answer score: 3
Revisions (0)
No revisions yet.