A Chinese restaurant wants to have a computer-based search system that will display a recipe for each dish. The owner of the restaurant wants the system to be flexible in that its recipes are stored in a text file. This way, the number of dishes can be expanded by adding more entries to the text file without changing any program code.
After analyzing the coding the restaurant realizes that Current coding only outputs first two line of the recipe.
Goal: Correct/debug “RecipeFinder” class to have the full recipe description in the output console in eclipse rather than having only first two lines. Other features should remain the same.
Requirements
To get full credit, you must follow the requirements specified below:
1. Particularly, you cannot use the Scanner class to read data from the keyboard or from a file. Instead, you should use BufferedReader, FileReader, InputStreamReader.
2. Your code must match your design (the class diagram). This includes the number of classes; the methods inside classes; and the relationships among these classes.
3. Put the unreliable code into Java’s Exception management system (proper use of try-catch construct).
4. Must use automatic file closing feature.
Implementation
Create the text “recipefile.txt” and save it to “C:/tmp/” directory.
Edit the work class “RecipeFinder” inside the project. This is the most difficult class in this program. One thing you need to figure out is how to output the whole body of a recipe based on the specific knowledge of when there is an empty line between the current recipe and the one following it. To help you, I offer the following segment of code (you are not required to use it) for you to use:
…
do {
info = br.readLine();
if(info != null) System.out.println(info);
} while((info != null) && (info.trim().compareTo(“ ”) != 0));
return true;
…
DO THE FOLLOWING TO SCORE YOUR POINTS
- Draw a class diagram in the space below (name your work class “RecipeFinder”; the application class “TestRecipeFinder”):
- Paste your source code in the space below (make sure that your code works appropriately, the source code format conforms to Java conventions (with appropriate indentations, you may lose points for not indent appropriately)):
-Test your code
Start afresh.
Type the recipe name “roast fish”.
Type “stop”.
Do a screen capture of the output.
_________________________________________________________
Here is the content of the recipefile.txt in “C:/tmp/” directory.
#appetizers
vegetable, egg roll, mixed with cream cheese, add some Teriyaki chicken, half gallon of water. Cook 15 minutes.
#chow mein
white meat chicken, pork beef, shrimp, vegetable include greens, mushroom, carrots, banana, apple source, half gallon of water. Cook 15 minutes.
#fried rice
plain rice, pork, chicken, beef, shrimp, vegetable, vegetable, include greens, mushroom, carrots, banana, apple source, one ounce of vegetable oil. Add the oil to a wok first, using hot fire to cook the oil to hot and able to see slight smoke. Then add all the ingredients and mix them. Cook 15 minutes.
#poultry dish
Moo Goo Gai Pan, Chicken with Mushroom, Chicken with Black Bean Source, Chicken with Oyster Source, Curry Chicken with onion, Chicken with Pepper & Tomato, Chicken with Broccoli, Chicken with Mixed Vegetable, Sweet & Sour Chicken, Chicken with Garlic Source, Hunan Chicken, Szechuan Chicken.
__________________________________________________________
class TestRecipeFinder {
public static void main(String[] args) {
RecipeFinder mysobj = new RecipeFinder("C:/tmp/recipefile.txt");
String sWrd;
System.out.print("Welcome to variable search program.\n "
+ "Enter 'stop' to end.\n");
do {
sWrd = mysobj.getSWd();
if(mysobj.searchWd(sWrd)) {
System.out.println(mysobj.getLines());
}else if(sWrd.equals("stop")){
System.out.println("Bye!");
}
else {
System.out.println("Not found.");
}
} while(sWrd.compareTo("stop") != 0);
}
}
_________________________________________________________________________________________________
// The file to be searched is located in C:/tmp.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
class RecipeFinder {
String fName;
String line1, line2;
public RecipeFinder(String s) {
fName = s;
line1 = " ";
line2 = " ";
}
// Get a search word from keyboard.
String getSWd() {
String info = " ";
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("\nEnter a recipe name: ");
try {
info = br.readLine();
} catch (IOException exc) {
System.out.println("Error reading console.");
}
return info;
}
// Search the file for variable name using the string passed in
boolean searchWd(String tWrd) {
int ch;
String info = " ";
File file = new File(fName);
try (BufferedReader br = new BufferedReader(
new FileReader(file))) {
do {
// read characters until a # is found
ch = br.read(); // read() reads a byte and returns it as an
// integer
if (ch == '#') {
info = br.readLine();
info = info.trim(); // remove space on both ends
if (tWrd.compareTo(info) == 0) { // found variable
line1 = info;
info = br.readLine();
line2 = info;
return true;
}
}
} while (ch != -1);
} catch (IOException exc) {
System.out.println("Error accessing file.");
return false;
}
return false; // variable not found.
}
String getLines() {
String rStr = " ";
rStr = line1 + "\n" + line2;
return rStr;
}
}
--------------------------------------------------------------RecipeFinder.java------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
class RecipeFinder {
String fName;
String line1, line2;
public RecipeFinder(String s) {
fName = s;
line1 = " ";
line2 = " ";
}
// Get a search word from keyboard.
String getSWd() {
String info = " ";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter a recipe name: ");
try {
info = br.readLine();
} catch (IOException exc) {
System.out.println("Error reading console.");
}
return info;
}
// Search the file for variable name using the string passed in
boolean searchWd(String tWrd) {
int ch;
String info = " ";
File file = new File(fName);
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
do {
// read characters until a # is found
ch = br.read(); // read() reads a byte and returns it as an
// integer
if (ch == '#') {
info = br.readLine();
info = info.trim(); // remove space on both ends
if (tWrd.compareTo(info) == 0) { // found variable
line1 = info;
info = br.readLine();
// read the next line after finding the entered recipe
name
// if next line is empty and empty line is found by string length
method
// if the line length is 0 then next line will be assigned to
line2
// then it comes out of loop and output will be
displayed
for (int i = 0; info.trim().length() == 0; i++) {
info = br.readLine();
line2 = info;
}
return true;
}
}
} while (ch != -1);
} catch (IOException exc) {
System.out.println("Error accessing file.");
return false;
} finally {
// for the automatic closing of file
if (file != null) {
try {
br.close();
} catch (IOException e) {
}
}
}
return false; // variable not found.
}
String getLines() {
String rStr = " ";
rStr = line1 + "\n" + line2;
return rStr;
}
}
------------------------------------------------------------CLASS DIAGRAM--------------------------------------------------------------------
There is no relatioship between the driver class (TestRecipeFinder.java) and the other class (RecipeFinder) so no relationship is added.
OUTPUT SCREENSHOT:
Note:
In RecipeFinder.java I have kept a comment over the code which I have added. For anyu doubts kindly comment I will respond and fix the issue if any found
A Chinese restaurant wants to have a computer-based search system that will display a recipe for...
Java Project Draw a class diagram for the below class (using UML notation) import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class myData { public static void main(String[] args) { String str; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter text (‘stop’ to quit)."); try (FileWriter fw = new FileWriter("test.txt")) { do { System.out.print(": "); str = br.readLine(); if (str.compareTo("stop") == 0) break; str = str + "\r\n"; // add newline fw.write(str); } while (str.compareTo("stop") != 0); } catch (IOException...
There is a problem in the update code. I need to be able to update one attribute of the students without updating the other attributes (keeping them empty). package dbaccessinjava; import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; import java.sql.*; import java.io.*; import java.util.Scanner; public class DBAccessInJava { public static void main(String[] argv) { System.out.println("-------- Oracle JDBC Connection Testing ------"); Connection connection = null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con= DriverManager.getConnection("jdbc:oracle:thin:@MF057PC16:1521:ORCL", "u201303908","pmu"); String sql="select * from student"; Statement st; PreparedStatement ps; ResultSet rs;...
How to solve this problem by using reverse String and Binary search? Read the input one line at a time and output the current line if and only if it is not a suffix of some previous line. For example, if the some line is "0xdeadbeef" and some subsequent line is "beef", then the subsequent line should not be output. import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet;...
I have my assignment printing, but I can find why the flowers is printing null and not the type of flower. Instructions Make sure the source code file named Flowers.java is open. Declare the variables you will need. Write the Java statements that will open the input file, flowers.dat, for reading. Write a while loop to read the input until EOF is reached. In the body of the loop, print the name of each flower and where it can be...
Exception handling is a powerful tool used to help programmers understand exception errors. This tool separates the error handling routine from the rest of the code. In this application, you practice handling exception errors. You use sample code that was purposefully designed to generate an exception error, and then you modify the code so that it handles the errors more gracefully. For this Assignment, submit the following program: The following code causes an exception error: import java.io.BufferedReader; import java.io.IOException; import...
I'm working with Java and I have a error message 1 error Error: Could not find or load main class Flowers. This is the problem: Instructions Make sure the source code file named Flowers.java is open. Declare the variables you will need. Write the Java statements that will open the input file, flowers.dat, for reading. Write a while loop to read the input until EOF is reached. In the body of the loop, print the name of each flower and where...
composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main { public static void main(String[] args) { System.out.print("Enter the...
Please complete the give code (where it says "insert code here") in Java, efficiently. Read the entire input one line at a time and then output a subsequence of the lines that appear in the same order they do in the file and that are also in non-decreasing or non-increasing sorted order. If the file contains n lines, then the length of this subsequence should be at least sqrt(n). For example, if the input contains 9 lines with the numbers...
Java only. Thanks These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to EFFICIENTLY accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each of them. Unless specified otherwise, sorted order refers to the natural sorted order...
Please answer question correctly and show the result of the working code. The actual and demo code is provided .must take command line arguments of text files for ex : input.txt output.txt from a filetext(any kind of input for the text file should work as it is for testing the question) This assignment is about using the Java Collections Framework to accomplish some basic text-processing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map,...