从txt文件中调用随机名称

我正在尝试一个项目,以对使用RMI产生3个不同汉堡的汉堡栏进行编码。目前,我在编写要选择要创建的汉堡的类的编码时遇到了很大的麻烦。

我有以下代码:

RecipeReader:

    package burgerbar.model.mediator;

import burgerbar.model.domain.Recipe;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;


public class RecipeReader implements RecipeProvider
{
  private String filename;


  public RecipeReader(String filename)
  {
    this.filename = filename;
  }


  @Override public synchronized Recipe getRecipeById(String id)
      throws FileNotFoundException,IllegalStateException
  {
    String line = null;
    try
    {
      Recipe recipe = null;
      File file = new File("C:\\Users\\Aagaard\\Desktop\\recipes.txt");
      Scanner in = new Scanner(file);
      while (in.hasnext())
      {
        line = in.nextLine(); // Read a line
        String[] token = line.split(";");
        String recipeId = token[0].trim();
        if (recipeId.equalsIgnoreCase(id))
        {
          recipe = convertToRecipe(line);
          break;
        }
      }
      in.close();
      return recipe;
    }
    catch (FileNotFoundException e)
    {
      throw e;
    }
    catch (Exception e)
    {
      throw new IllegalStateException("Wrong formatted file: " + line);
    }
  }


  @Override public synchronized Recipe getRecipeByName(String name)
      throws FileNotFoundException,IllegalStateException
  {
    String line = null;
    try
    {
      Recipe recipe = null;
      File file = new File(filename);
      Scanner in = new Scanner(file);
      while (in.hasnext())
      {
        line = in.nextLine(); // Read a line
        String[] token = line.split(";");
        String recipeName = token[1].trim();
        if (recipeName.equalsIgnoreCase(name))
        {
          recipe = convertToRecipe(line);
          break;
        }
      }
      in.close();
      return recipe;
    }
    catch (FileNotFoundException e)
    {
      throw e;
    }
    catch (Exception e)
    {
      throw new IllegalStateException("Wrong formatted file: " + line);
    }
  }

    @Override public synchronized void updateRecipe(Recipe recipe)
      throws FileNotFoundException,IllegalStateException
  {
    String line = null;
    try
    {
      ArrayList<Recipe> recipes = new ArrayList<>();
      File file = new File(filename);
      Scanner in = new Scanner(file);
      boolean updated = false;
      while (in.hasnext())
      {
        line = in.nextLine(); // Read a line
        String[] token = line.split(";");
        String recipeId = token[0].trim();
        if (recipeId.equalsIgnoreCase(recipe.getId()))
        {
          recipes.add(recipe);
          updated = true;
        }
        else
        {
          recipes.add(convertToRecipe(line));
        }
      }
      if (!updated)
      {
        recipes.add(recipe);
      }
      in.close(); // Close the file

      PrintWriter out = new PrintWriter(file);
      for (int i = 0; i < recipes.size(); i++)
      {
        out.println(convertFromRecipe(recipes.get(i)));
      }
      out.close();
    }
    catch (FileNotFoundException e)
    {
      throw e;
    }
    catch (Exception e)
    {
      throw new IllegalStateException("Wrong formatted file: " + line);
    }
  }

  private String convertFromRecipe(Recipe recipe)
  {
    String recipeId = recipe.getId();
    String name = recipe.getName();
    String ingredientsString = "";
    String[] ingredients = recipe.getIngredients();
    for (int j = 0; j < ingredients.length; j++)
    {
      ingredientsString += ingredients[j];
      if (j < ingredients.length - 1)
      {
        ingredientsString += "; ";
      }
    }
    return (recipeId + "; " + name + "; " + ingredientsString);
  }

  private Recipe convertToRecipe(String recipeString)
  {
    String[] token = recipeString.split(";");
    String recipeId = token[0].trim();
    String name = token[1].trim();
    String[] ingredients = new String[token.length - 2];
    for (int i = 2; i < token.length; i++)
    {
      ingredients[i - 2] = token[i].trim();
    }
    return new Recipe(recipeId,name,ingredients);
  }
}

RecipeProvider:

package burgerbar.model.mediator;
import burgerbar.model.domain.Recipe;

public interface RecipeProvider
{

  public Recipe getRecipeById(String id) throws Exception;

  public Recipe getRecipeByName(String name) throws Exception;

  public void updateRecipe(Recipe recipe) throws Exception;
}

汉堡:     软件包burgerbar.model.domain;

import java.io.Serializable;

public class Burger implements Serializable
{
   private static final long serialVersionUID = 1L;
   private String name;

   public Burger(String name)
   {
      this.name = name;
   }

   public String getName()
   {
      return name;
   }

   @Override
   public String toString()
   {
      return "Burger: " + name;
   }
}

食谱:

package burgerbar.model.domain;

import java.util.Arrays;

public class Recipe
{
  private String id;
  private String name;
  private String[] ingredients;

  public Recipe(String id,String name,String... ingredients)
  {
    this.id = id;
    this.name = name;
    this.ingredients = ingredients;
  }

  public String getId()
  {
    return id;
  }

  public String getName()
  {
    return name;
  }

  public String[] getIngredients()
  {
    return ingredients;
  }

  public Burger createBurger()
  {
    return new Burger(name);
  }

  @Override public String toString()
  {
    return "Recipe{" + "id='" + id + '\'' + ",name='" + name + '\''
        + ",ingrediences=" + Arrays.toString(ingredients) + '}';
  }
}

我的txt文件:

1; Hamburger; 4 inch bun; Tomato; Onion; Beef; Lettuce; Cucumber; Ketchup
2; Cheese burger; 5 inch bun; Tomato; Onion; Cheese; Beef; Lettuce; Cucumber; Ketchup
3; Veggie burger; 5 inch bun; Green asparagus; Tomato; Grilled Portobello Mushroom; Onion; Veggie Patty; Lettuce; Cucumber; Vegan mayo
j5899965 回答:从txt文件中调用随机名称

我猜您想从文件中的三个汉堡中随机选择一个。

要实现此目的,一旦您从文件中读取了配方,则首先需要创建List<Recipe>,然后将配方对象放入此列表中。有了食谱清单后,您可以使用下面的方法获取随机食谱。

listOfRecipe.get(new Random().nextInt(listOfRecipe.size()));

希望这会有所帮助。

本文链接:https://www.f2er.com/3167076.html

大家都在问