Question: Linear search: Restaurant menu in Java Now we will adapt our linear search to an array of objects. The objects we will be using are

Linear search: Restaurant menu in Java

Now we will adapt our linear search to an array of objects. The objects we will be using are MenuItem objects as defined by the included MenuItem class (do not change the code in the MenuItem class). MenuItem has three getter methods you will need to use: getName(), which returns the name of the item, getDescription(), which returns a brief description of the item, and getPrice(), which returns the price of the item.

First declare a String that holds the name of something you would like to eat.

Next, write a linear search to find the MenuItem in the menu array with the same Name as your

String. (Hint: use .equalsIgnoreCase().)

When it is found, print the name, description, and price of the item.

Demo.java

public static MenuItem[] loadMenu(){

String inFile = "menu.txt";

Scanner f;

try {

ArrayList m = new ArrayList();

f = new Scanner(new File(inFile));

while(f.hasNext()){

String item = f.nextLine();

String str[] = item.split(";");

if(str.length>2){

MenuItem mi = new MenuItem(str[0],str[1],Double.parseDouble(str[2]));

m.add(mi);

}

}

MenuItem[] menu = new MenuItem[m.size()];

for(int i=0; i

menu[i] = m.get(i);

}

return menu;

} catch (FileNotFoundException e){

System.out.println("Menu file not found.");

}

return null;

}

Can you help me to do it:

MenuItem[] menu = loadMenu(); //Call a helper function to instantiate the menu array with data from a file

//TODO: Declare a String that holds what you want to eat, and use linear search to find it in the menu array

//TODO: When you find it, print the name, description, and price of the item

And this is MenuItem.java

public class MenuItem {

private String name;

private String description;

private double price;

/**Constructor that sets field variables

* @param name

* @param description

* @param price

*/

public MenuItem(String name, String description, double price) {

this.name = name;

this.description = description;

this.price = price;

}

/**Returns the name of the item

* @return

*/

public String getName() {

return name;

}

/**Returns a brief description of the item

* @return

*/

public String getDescription() {

return description;

}

/**Returns the price of the item

* @return

*/

public double getPrice() {

return price;

} }

menu.txt look like:

Pancakes;Three classic buttermilk pancakes topped with butter and syrup.;5.50

French Toast;Three slices of our homemade bread dipped in egg and fried to a golden brown.;6.00

Waffle;Fresh made Belgian waffle topped with strawberries and whipped cream.;4.50

Omelet;Made-to-order two-egg omelet with up to three add-ins. Choose from: cheese, bacon, ham, scallops, tomatoes, mushrooms.;7.25

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!