I created this program in Java programming to get all recipes details inputs from user and then user can choose to insert, find, remove or display recipes details by selecting a choice from the menu. However, options 2,3,4 seems that they don't work properly and I've been trying to modify and run but still not working well. How to correct the code so that these menu options can work properly.
This is my code
import java.util.Scanner;
class Recipe{
String name;
int number;
String[] instructions;
int numberOfInstructions;
String[] ingredients;
int numberOfIngredients;
public Recipe(String name, String[] instructions, int numberOfInstructions, String[] ingredients, int numberOfIngredients) {
this.name = name;
this.instructions = instructions;
this.numberOfInstructions = numberOfInstructions;
this.ingredients = ingredients;
this.numberOfIngredients = numberOfIngredients;
}
public Recipe() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getInstructions() {
return instructions;
}
public void setInstructions(String[] instructions) {
this.instructions = instructions;
}
public int getNumberOfInstructions() {
return numberOfInstructions;
}
public void setNumberOfInstructions(int numberOfInstructions) {
this.numberOfInstructions = numberOfInstructions;
}
public String[] getIngredients() {
return ingredients;
}
public void setIngredients(String[] ingredients) {
this.ingredients = ingredients;
}
public int getNumberOfIngredients() {
return numberOfIngredients;
}
public void setNumberOfIngredients(int numberOfIngredients) {
this.numberOfIngredients = numberOfIngredients;
}
}
public class Cookbook {
Recipe listOfRecipes[];
int numberOfRecipes;
public Cookbook() {
this(new Recipe [0], 0);
}
public Cookbook(Recipe[] listOfRecipes, int numberOfRecipes) {
this.listOfRecipes = listOfRecipes;
this.numberOfRecipes = numberOfRecipes;
}
public Recipe[] getListOfRecipes() {
return listOfRecipes;
}
public void setListOfRecipes(Recipe[] listOfRecipes) {
this.listOfRecipes = listOfRecipes;
}
public int getNumberOfRecipes() {
return numberOfRecipes;
}
public void setNumberOfRecipes(int numberOfRecipes) {
this.numberOfRecipes = numberOfRecipes;
}
public void addRecipe(Recipe recipe){
Recipe newList[] = new Recipe[this.listOfRecipes.length 1];
for (int i = 0; i < newList.length - 1; i )
{
newList[i] = this.listOfRecipes[i];
}
newList[newList.length - 1] = recipe;
this.listOfRecipes = newList;
}
public Recipe findRecipe(int recipeNo){
for (int i = 0; i < this.listOfRecipes.length; i )
{
if(this.listOfRecipes[i].number == recipeNo)
{
System.out.println(this.listOfRecipes[i]);
return this.listOfRecipes[i];
}
else {
System.out.println("Recipe not found!");
}
}
return null;
}
public void removeRecipe(int recipeNo){
Recipe newList[] = new Recipe[this.listOfRecipes.length];
for (int i = 0; i < newList.length; i )
{
if(this.listOfRecipes[i].number == recipeNo)
{
listOfRecipes[i] = listOfRecipes[this.listOfRecipes.length-1];
listOfRecipes[this.listOfRecipes.length-1] = null;
}
newList[i] = this.listOfRecipes[i];
}
this.listOfRecipes = newList;
System.out.println("Recipe removal successful!");
}
public void display(Recipe recipe){
//Recipe newList[] = new Recipe[this.listOfRecipes.length];
System.out.println("List of recipes: ");
for (int i = 0; i < this.listOfRecipes.length; i )
{
System.out.println(this.listOfRecipes[i].name);
}
}
}
class Testmain{
public static void main(String[] args) {
int i, choose, insNum, ingrNum;
String name, sName, delName;
String[] ingred;
String[] instr;
Scanner ip = new Scanner(System.in);
Cookbook c = new Cookbook();
Recipe r = null;
int n, nn, recNo, recNoo;
do{
System.out.println(" ");
System.out.println("***MENU***");
System.out.println("1. Add Recipe");
System.out.println("2. Find Recipe");
System.out.println("3. Remove Recipe");
System.out.println("4. Display Recipe");
System.out.print("Enter your choice: ");
choose = ip.nextInt();
System.out.println();
if(choose == 1) {
System.out.print("How many recipes would you like to add?: ");
n = ip.nextInt();
for (int j=0; j<n; j ){
System.out.print("\nEnter number of recipe: ");
recNo = ip.nextInt();
System.out.print("Enter recipe name: ");
ip.nextLine();
name = ip.nextLine();
System.out.print("Enter number of ingredients: ");
ingrNum = ip.nextInt();
ingred = new String[ingrNum];
for(i=0; i<ingrNum; i ) {
System.out.print("Ingredient " (i 1) ": ");
ingred[i] = ip.next();
}
System.out.print("Enter number of instructions: ");
insNum = ip.nextInt();
instr = new String[insNum];
for(i=0; i<insNum; i ) {
System.out.print("Instruction " (i 1) ": ");
ip.nextLine();
instr[i] = ip.next();
}
r = new Recipe(name, instr, ingrNum, ingred,insNum);
c.addRecipe(r);
}
} else if(choose == 2) {
System.out.println("Enter number of recipe you want to find: ");
recNo = ip.nextInt();
c.findRecipe(recNo);
} else if(choose == 3) {
System.out.println("Enter name of recipe to be removed: ");
ip.nextLine();
recNoo = ip.nextInt();
c.removeRecipe(recNoo);
}
else if(choose == 4) {
c.display(r);
}
else {
System.out.println("Invalid entry!");
}
System.out.println(" ");
System.out.println("Would you like to continue?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.print("Your choice: ");
nn = ip.nextInt();
} while (nn != 2);
System.out.println("Program has ended.");
}
}
Expected Input/Output:
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 1
How many recipes would you like to add?: 2
Enter number of recipe: 1
Enter recipe name: r
Enter number of ingredients: 1
Ingredient 1: i1
Enter number of instructions: 2
Instruction 1: n1
Instruction 2: n2
Enter number of recipe: 2
Enter recipe name: r2
Enter number of ingredients: 1
Ingredient 1: i1
Enter number of instructions: 1
Instruction 1: n1
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 2
Enter number of recipe you want to find:
1
r
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 4
List of recipes:
r
r2
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 3
Enter name of recipe to be removed:
1
Recipe removed successfully!
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 4
List of recipes:
r2
Would you like to continue?
1. Yes
2. No
Your choice: 2
Program has ended.
Actual Input/Output:
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 1
How many recipes would you like to add?: 2
Enter number of recipe: 1
Enter recipe name: r
Enter number of ingredients: 1
Ingredient 1: i1
Enter number of instructions: 2
Instruction 1: n1
Instruction 2: n2
Enter number of recipe: 2
Enter recipe name: r2
Enter number of ingredients: 1
Ingredient 1: i1
Enter number of instructions: 1
Instruction 1: n1
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 2
Enter number of recipe you want to find:
1
Recipe not found!
Recipe not found!
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 4
List of recipes:
r
r2
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 3
Enter name of recipe to be removed:
1
Recipe removed successfully!
Would you like to continue?
1. Yes
2. No
Your choice: 1
***MENU***
1. Add Recipe
2. Find Recipe
3. Remove Recipe
4. Display Recipe
Enter your choice: 4
List of recipes:
r
r2
Would you like to continue?
1. Yes
2. No
Your choice: 2
Program has ended.
CodePudding user response:
I've debugged your code and you are not saving the "Recipe.number". You should add recNo variable when you are creating the Recipe in this line:
r = new Recipe(name, recNo, instr, ingrNum, ingred, insNum);
And change the Recipe class, adding:
public Recipe(String name, int number, String[] instructions, int numberOfInstructions, String[] ingredients, int numberOfIngredients) {
this.name = name;
this.number = number;
this.instructions = instructions;
this.numberOfInstructions = numberOfInstructions;
this.ingredients = ingredients;
this.numberOfIngredients = numberOfIngredients;
}
I hope this help you.
Edited: I'd recommend you to change your message "Enter name of recipe to be removed:" by "Enter number of recipe to be removed:"