當前位置:菜譜大全網 - 菜譜 - 求java大佬幫忙

求java大佬幫忙

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class Recipe {

private String name; //菜譜名稱

private String style; //菜系 如:川菜、湘菜等

private int time; //烹飪時長 分鐘

private String[] food; //食材

private String[] step; //操作步驟

public Recipe() {

}

public Recipe(String name, String style, int time, String[] food, String[] step) {

this.name = name;

this.style = style;

this.time = time;

this.food = food;

this.step = step;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getStyle() {

return style;

}

public void setStyle(String style) {

this.style = style;

}

public int getTime() {

return time;

}

public void setTime(int time) {

this.time = time;

}

public String[] getFood() {

return food;

}

public void setFood(String[] food) {

this.food = food;

}

public String[] getStep() {

return step;

}

public void setStep(String[] step) {

this.step = step;

}

@Override

public String toString() {

String foods="";

for (String f : food) {

foods+=f+" ";

}

String steps="";

for (int i = 0; i < step.length; i++) {

steps += (i+1)+"."+step[i];

if(i!=step.length-1){

steps+=";";

}

}

return "菜譜名稱:" + name +

"\n菜系:" + style +

"\n時長:" + time +

"\n所需食材:" + foods +

"\n操作步驟:" + steps;

}

public static void print(Recipe[] recipes){

for (Recipe recipe : recipes) {

System.out.println(recipe);

}

}

public static Recipe[] searchRecipesContainName(Recipe[] recipes, String name){

List<Recipe> list=new ArrayList<Recipe>();

for (Recipe recipe : recipes) {

if(recipe.getName().contains(name)){

list.add(recipe);

}

}

return list.toArray(new Recipe[list.size()]);

}

public static Recipe[] searchRecipes(Recipe[] recipes, String style){

List<Recipe> list=new ArrayList<Recipe>();

for (Recipe recipe : recipes) {

if(recipe.getStyle().equals(style)){

list.add(recipe);

}

}

return list.toArray(new Recipe[list.size()]);

}

public static Recipe[] searchRecipeLessThan(Recipe[] recipes, int time){

List<Recipe> list=new ArrayList<Recipe>();

for (Recipe recipe : recipes) {

if(recipe.getTime()<time){

list.add(recipe);

}

}

return list.toArray(new Recipe[list.size()]);

}

public static Recipe[] searchRecipeContainsFood(Recipe[] recipes, String food){

List<Recipe> list=new ArrayList<Recipe>();

for (Recipe recipe : recipes) {

for (String s : recipe.getFood()) {

if(s.equals(food)){

list.add(recipe);

}

}

}

return list.toArray(new Recipe[list.size()]);

}

public static void main(String[] args) {

//存儲5個菜譜的信息

Recipe[] recipes=new Recipe[5];

recipes[0]=new Recipe("醬牛肉","家常菜",120,

new String[]{

"牛腱子",

"黃豆醬油",

"黃酒",

"冰糖"

},

new String[]{

"準備好主要食材",

"加入食材慢燉兩至三小時"

});

recipes[1]=new Recipe("紅燒牛肉","家常菜",120,

new String[]{

"牛腩",

"牛筋",

"生抽",

"冰糖"

},new String[]{

"準備好主要食材",

"加入食材慢燉兩至三小時"

});

recipes[2]=new Recipe("小雞燉蘑菇","湘菜",100,

new String[]{

"小雞",

"蘑菇"

},

new String[]{

"準備好主要食材",

"加入食材慢燉兩至三小時"

});

recipes[3]=new Recipe("地三鮮","川菜",25,

new String[]{

"茄子",

"辣椒"

},

new String[]{

"準備好主要食材",

"加入食材慢燉"

});

recipes[4]=new Recipe("西紅柿","湘菜",20,

new String[]{

"西紅柿",

"味達美醬油"

},

new String[]{

"準備好主要食材",

"加入食材慢燉"

});

System.out.println("找到所有名稱中包含牛肉的菜譜:");

Recipe[] result_1 = searchRecipesContainName(recipes, "牛肉");

print(result_1);

System.out.println("\n希望查找所有湘菜的菜譜:");

Recipe[] result_2=searchRecipes(recipes,"湘菜");

print(result_2);

System.out.println("\n查找烹飪時長小於30分鐘的菜譜:");

Recipe[] result_3=searchRecipeLessThan(recipes,30);

print(result_3);

System.out.println("\n查找包含西紅柿的菜譜:");

Recipe[] result_4=searchRecipeContainsFood(recipes,"西紅柿");

print(result_4);

}

}