Create a class named RecipeFactory
–public static Recipe makeRecipe(String recipeType, String recipeName, HashMap<String, String> ingredients)
–public static final String (DESSERT and DINNER)
•“Dessert” and “Dinner”
•Create an abstract class named Recipe
–protected HashMap<String, String> ingredients
•Key: ingredient name
•Value: ingredient amount
–protected String name
–HashMap<String, String> getIngredients()
•Can implement here
–String getName()
•Can implement here
•Extend your abstract class in DessertRecipe and DinnerRecipe
–Override toString
•Expected output on next slide
•Use the StringBuilder class
–Implement a constructor that takes String name and HashMap<String, String> ingredients.
–Warning: assigning HashMap from constructor and memory
•new HashMap<String, String>(hashMap)
•Hint: use “\n” to add a newline and “\t” to add the tab
–Since this will go through an autograder, you must exactly match the formatting (do not add an extra \n to the very end)
•Create a singleton class named Cookbook
–private Cookbook() { }
–private ArrayList<Recipe> recipes
–void addRecipe(Recipe r)
–void clearRecipes()
–ArrayList<Recipe> getRecipes()
Cookbook getInstance()
•Create a file named FinalProject.java
•This file will contain all your code when you submit your completed project
•Both methods you work on this week should be in the public FinalProjectclass
•You can add main to this class to help you test your code
•Define a static method that can take a recipe string and convert it into the appropriate Recipe subclass object
–Remember:
•Key: ingredient name
•Value: ingredient amount
–Use your RecipeFactory to create the object
•Practice with this test string and verify your method works.
–String test = "Cake~Dessert~flour@3/4 cups#milk@3/4 cups#sugar@2/3 cups#vanilla@1 tsp";
•Define a static method called connectToServer that takes as input a String ipAddress and integer port.
•Create a socket (client) and connect to the input port
–This will be your connection to the RecipeServer
•Make sure your socket is continually reading in messages from the socket
–Pseudocode: while (input from socket != null) print out input from socket
–Use an InputStreamReader wrapped in a BufferedReader
•If you run the following code from main in FinalProject.java…
–FinalProject.connectToServer(8765);
–System.out.println(“Hello”);
•…Hello will never be printed
•Why?
•Update your connectToServermethod
•Instead of performing the connection and reading on the main thread, perform the work in a new thread.
•To test your changes, run your code from the previous slide. Hello should be print out immediately.
•Also make sure you are still able to receive messages continually from RecipeServer.
•Write the following methods in your FinalProject class.
•public static void serializeRecipe(Recipe r, String path)
–Serialize the given recipe to the path
–You should only store the following fields
•name
•ingredients
–You can serialize the object as a Recipe
•No need to check which child class was input
•public static Recipe deserializeRecipe(String path)
–Deserialize a Recipe at the given path
•Cast the returned object to Recipe
–Note: Even if you treat it as a Recipe, the data from the child class you used will be saved
•i.e. no need to cast it to the child class
•Place all your classes in your FinalProject.java file.
•Test to make sure all the code still works.
•Submit it online to the autograder.
•Here is how testing will be done:
–A server socket will await your connection
–Recipe strings will be sent over the socket
–Your Cookbook should be updated after each recipe
–All of your methods will be tested, including your RecipeFactory and Cookbook methods.
Feel free to choose you own recipe names and ingredients.
•As you receive recipes over the socket, you should create the appropriate Recipe object (using the factory) and add them to the Cookbook list.
•Test your code with the RecipeServercode. You can use 8765 as your testing port.
–Note: You will have to run the server and your code separately. You will need to test it locally.
•You should be able to continually send messages from the server and receive them from your client.
–Do not change the server code – this version will be used to test your program.
Recipe.java
import java.util.HashMap;
public abstract class Recipe {
//private instance variables
protected HashMap<String,String> ingredients;
protected String name;
/**
* @param ingredients
* @param name
*/
public Recipe(HashMap<String, String> ingredients, String name) {
super();
this.ingredients = ingredients;
this.name = name;
}
/**
* @return the ingredients
*/
public HashMap<String, String> getIngredients() {
return ingredients;
}
/**
* @return the name
*/
public String getName() {
return name;
}
}
DessertRecipe.java
import java.util.HashMap;
import java.util.Map;
public class DessertRecipe extends Recipe{
public DessertRecipe(HashMap<String, String> ingredients, String name) {
super(ingredients, name);
}
@Override
public String toString() {
StringBuilder str=new StringBuilder();
str.append("Name: "+super.getName()).append("\n").append("Food Type: Dessert").append("\n");
for (Map.Entry<String, String> entry : ingredients.entrySet()) {
str.append("\t").append(entry.getValue() + " " + entry.getKey()).append("\n");
}
return str.toString();
}
}
DinnerRecipe.java
import java.util.HashMap;
import java.util.Map;
public class DinnerRecipe extends Recipe{
public DinnerRecipe(HashMap<String, String> ingredients, String name) {
super(ingredients, name);
}
@Override
public String toString() {
StringBuilder str=new StringBuilder();
str.append("Name: "+super.getName()).append("\n").append("Food Type: Dinner").append("\n");
for (Map.Entry<String, String> entry : ingredients.entrySet()) {
str.append("\t").append(entry.getValue() + " " + entry.getKey()).append("\n");
}
return str.toString();
}
}
RecipeFactory.java
import java.util.HashMap;
public class RecipeFactory {
//private instance variables
public static final String DESSERT="Dessert";
public static final String DINNER="Dinner";
public static Recipe makeRecipe(String recipeType,String recipeName,HashMap<String, String> ingredients)
{
if(recipeType.equalsIgnoreCase(DESSERT))
return new DessertRecipe(ingredients, recipeName);
else if(recipeType.equalsIgnoreCase(DINNER))
return new DinnerRecipe(ingredients, recipeName);
else return null;
}
}
Cookbook.java
import java.util.ArrayList;
public class Cookbook {
private ArrayList<Recipe> recipes;
// static variable of type Cookbook
private static Cookbook cookbook_instance = null;
private Cookbook()
{
recipes=new ArrayList<Recipe>();
}
void addRecipe(Recipe r)
{
recipes.add(r);
}
void clearRecipes()
{
recipes.clear();
}
ArrayList<Recipe> getRecipes()
{
return recipes;
}
static Cookbook getInstance()
{
if (cookbook_instance == null)
cookbook_instance = new Cookbook();
return cookbook_instance;
}
}
CookBookMain.java
import java.util.HashMap;
public class CookBookMain {
public static void main(String[] args) {
//Create ingredient map
HashMap<String, String> ingredients=new HashMap<String, String>();
//Add ingredients
ingredients.put("milk", "3/4 cups");
ingredients.put("sugar", "2/3 cups");
ingredients.put("flour", "3/4 cups");
ingredients.put("vanilla", "1 tsp");
//Create recipe
Recipe recipe=RecipeFactory.makeRecipe("Dessert", "Cake", ingredients);
Cookbook cb=Cookbook.getInstance();
//Add recipe
cb.addRecipe(recipe);
//Display recipes
for(Recipe c:cb.getRecipes())
System.out.println(c);
}
}
Comments
Leave a comment