Question
Create a class named RecipeFactory public static Recipe makeRecipe(String recipeType, String recipeName, HashMap ingredients) public static final String (DESSERT and DINNER) Dessert and Dinner Create
Create a class named RecipeFactory
public static Recipe makeRecipe(String recipeType, String recipeName, HashMap
public static final String (DESSERT and DINNER)
Dessert and Dinner
Create an abstract class named Recipe
protected HashMap
Key: ingredient name
Value: ingredient amount
protected String name
HashMap
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
Warning: assigning HashMap from constructor and memory
new HashMap
Hint: use 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 to the very end)
Create a singleton class named Cookbook
private Cookbook() { }
private ArrayList
void addRecipe(Recipe r)
void clearRecipes()
ArrayList
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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started