Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi! I have two classes , Recipe class for instansiate, RecipeList class to store

ID: 3771547 • Letter: H

Question

Hi! I have two classes , Recipe class for instansiate, RecipeList class to store Recipes in ArrayList.
How to I make compareTo method for RecipeList?

In main method class, I want to sort Recipes for alphabetical order..


public class Recipe {
private String name;
private String ingredients;
private String instructions;


public Recipe (String name, String ingredients, String instructions){
this.name=name;
this.ingredients = ingredients;
this.instructions = instructions;   
  
}
public void setName(String name){
this.name = name;
}

public String getName(){
return name;
}

public void setIngredients(String ingredients){
this.ingredients = ingredients;
}

public String getIngredients(){
return ingredients;
}

public void setInstructions(String instructions){
this.instructions = instructions;
}

public String getInstructions(){
return instructions;
}

public String toString() {
return "Recipe name: " + this.name + "Ingredients: " + this.ingredients +
"Instructions: "+ this.instructions;
}
public int compareTo(Recipe otherRecipe) {
if(this.getName().toUpperCase().compareTo(otherRecipe.getName().toUpperCase())<0){
return -1;
}
else if(this.getName().toUpperCase().compareTo(otherRecipe.getName().toUpperCase())>0){
return 1;
}else {
return 0;
}
}


}

--------------------------------------------------------------------------------------------

import java.util.ArrayList;

public class RecipeList<Recipe> implements Comparable<Recipe>{
   ArrayList<Recipe> recipes;
  
   private Recipe recipe;
  


public RecipeList (){
   this.recipes = new ArrayList<Recipe>();
      
}

@Override

public int compareTo(Recipe otherRecipe) {
//TO-DO
   return 0;
}
}

Explanation / Answer

public int compareTo(Recipe otherRecipe)
{
return this.inputRecipeName.compareTo(otherRecipe.inputRecipeName);
}
public static Collection<Recipe> getRecipes()
{
List<Recipe> recipes = new ArrayList<Recipe>(RECIPE_MAP.values());
Collections.sort(recipes);
return recipes;
}
class RecipeCompare implements Comparator<Recipe>
{
public int compare(Recipe o1, Recipe o2)
{
return o1.getID().compareTo(o2.getID());
}
}
Collections.sort(recipes,new RecipeCompare());
public class RecipeComparator implements Comparator<Recipe>
{
public static final int COMPARE_BY_ID = 0;
public static final int COMPARE_BY_NAME = 1;
private int compare_mode = COMPARE_BY_NAME;
public RecipeComparator()
{
}
public RecipeComparator(int compare_mode)
{
this.compare_mode = compare_mode;
}
public int compare(Recipe o1, Recipe o2)
{
switch (compare_mode)
{
case COMPARE_BY_ID:
return o1.getId().compareTo(o2.getId());
default:
return o1.getInputRecipeName().compareTo(o2.getInputRecipeName());
}
}
public static void main(String[] args)
{
String string1 = "1";
String string2 = "2";
String string11 = "11";
System.out.println(string1.compareTo(string2));
System.out.println(string2.compareTo(string11));
int number2 = Integer.valueOf(string1);
int number11 = Integer.valueOf(string11);
int compareTo = number2 > number11 ? 1 : (number2 < number11 ? -1 : 0) ;
System.out.println(compareTo);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote