Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1: Connecting to and Retrieving data from MongoDB in a Java Application. Complete the code for the findRestaurants() method so that it finds restaurants

Part 1: Connecting to and Retrieving data from MongoDB in a Java Application.

Complete the code for the findRestaurants() method so that it finds restaurants by cuisine and ZIP code. You should use a regular expression for the cuisine.

Complete the code for the getRestaurantInfo() so that it pulls up the information on the restaurant selected by the user.

Complete the code for the getRestaurantGrades() method so that the program displays reviewer grades for the restaurant.

package u05a1restaurantmongo;

import com.mongodb.BasicDBObject; import com.mongodb.MongoClient; import com.mongodb.MongoClientURI; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; import static com.mongodb.client.model.Projections.fields; import static com.mongodb.client.model.Projections.include; import java.util.ArrayList; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import static javax.swing.UIManager.get; import org.bson.Document;

/** * * @author IT4736 */ public class U05a1RestaurantMongo { // Private class field to provide access to appropriate collection private static MongoCollection collection; public static void main(String[] args) { // Adjust logging level for MongoDB client to avoid too much info Logger mongoLogger = Logger.getLogger( "org.mongodb.driver" ); mongoLogger.setLevel(Level.WARNING); /* ---------------------------------------------------------------------------------------------------- >>> ADD CONNECTION INFORMATION BELOW <<< Add MongoDB Connection information for MongoDB on localhost -----------------------------------------------------------------------------------------------------*/ /* Set mongoURI to address for localhost */ String mongoURI = ""; /* Set database to name of database with restaurant info */ String database = ""; /* Set collectionName to name of collection with restaurant data */ String collectionName = ""; MongoClientURI mongoConnURI = new MongoClientURI(mongoURI); MongoClient mongoClient = new MongoClient(mongoConnURI); MongoDatabase mongoDB = mongoClient.getDatabase(database); collection = mongoDB.getCollection(collectionName); Scanner input = new Scanner(System.in); System.out.print("Enter a ZIP code: "); String zip = input.nextLine(); System.out.print("Enter a cuisine: "); String food = input.nextLine(); MongoCursor cursor = findRestaurants(zip, food); String selection = getRestaurantChoice(cursor); if(!selection.equals("0")) { Restaurant r = getRestaurantInfo(selection); System.out.println(r); System.out.println("Grades for " + r.getName() + ":"); getRestaurantGrades(r); System.out.println(""); } else { System.out.println("Exiting... "); } } /*--------------------------------------------------------------------------------------------------------- ZIP code and cuisine passed as string to findRestaurants() method. This method should return an iterator (MongoCursor -----------------------------------------------------------------------------------------------------------*/ static public MongoCursor findRestaurants(String zip, String food) { /*------------------------------------------------------------------------------------------------------------- >>> COMPLETE THE CODE FOR THIS METHOD <<< Complete this method so that it finds restaurants by ZIP code and cuisine (food). Use a regular expression query for the cuisine/food search. The method should return a MongoCursor collection. ---------------------------------------------------------------------------------------------------------------*/

} /*--------------------------------------------------------------------------------------------------------- Restaurant ID passed as string to getRestaurantInfo() method. This method should return a Restaurant object. -----------------------------------------------------------------------------------------------------------*/ static public Restaurant getRestaurantInfo(String restaurantID) { /*------------------------------------------------------------------------------------------------------------- >>> COMPLETE THE CODE FOR THIS METHOD <<< Complete this method so that it finds the restaurant by the restaurant_id. The method should return a Restaurant object with the information. ---------------------------------------------------------------------------------------------------------------*/ } /*--------------------------------------------------------------------------------------------------------- Restaurant object passed to getRestaurantGrades() method. This method list the grades/ratings, so return type is void. -----------------------------------------------------------------------------------------------------------*/ static public void getRestaurantGrades(Restaurant restaurant) { /*------------------------------------------------------------------------------------------------------------- >>> COMPLETE THE CODE FOR THIS METHOD <<< Complete this method so that it displays the grades for the restaurant passed in as a Restaurant object. ---------------------------------------------------------------------------------------------------------------*/ } static public String getRestaurantChoice(MongoCursor cursor) { int refNumber = 0; // ArrayList of restaurant IDs ArrayList id = new ArrayList(); System.out.println(""); // Print header for list of restaurants System.out.println(String.format("%5s %-50s %-20s %s", "[#]", "Name", "Cuisine", "ZIP Code")); System.out.println(String.format("%3s %50s %20s %11s", " ", " ", " ", " ").replace(" ", "-")); while(cursor.hasNext()) { Document restaurantDoc = cursor.next(); String output = String.format("%5s %-50s %-20s %s", "[" + ++refNumber + "]", restaurantDoc.getString("name"), restaurantDoc.getString("cuisine")); ((Document) restaurantDoc.get("address")).getString("zipcode") ; System.out.println(output); id.add(restaurantDoc.getString("restaurant_id")); } System.out.print(" Enter selection number (0 to quit): "); Scanner input = new Scanner(System.in); int choice = input.nextInt(); String restaurantID; // choice not 0 and not too high if(choice > 0 && choice <= refNumber) { restaurantID = id.get(choice - 1); } else { restaurantID = "0"; } return restaurantID; } }

might need to

In the NetBeans project tree on the left side of the window, right click on the Libraries folder and add the 3 .jar files needed for MongoDB (mongodb-driver-3.6.1.jar, mongodb-driver-core-3.6.1.jar, and bson-3.6.1.jar).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions