In this project, you will be asked to implement a simple Java program that will maintain its data using a specific NoSQL database called MongoDB. In this program you will be asked to maintain an object called Product, each product will have the following attributes:

33 0

Get full Expert solution in seconds

$1.97 ONLY

Unlock Answer

• Product ID.
• Product Name.
• Product Description.
• Product Price.
You will be asked to implement two types of products.
• Dimensional Products.
• Weighted Products.
For the products of type Dimensional, they will have two extra values that
express the width and the length of the product stored.
For the products of type Weighted, they will have one extra value that
express the weight of the product stored.
When the program runs, the following choices appear.
1- Show all Products.
2- Add Product.
3- Delete Product.
4- Update Product.
5- Replace Product.
6- Search Product.
7- Exit.
Enter your choice

Notes: READ CAREFULLY.
1- When the program starts, it should connect automatically to a
Database that is called “DBCourse” and a Collection called
“Products”.
2- All functions should affect the data stored in the database.
3- This is a SOLO project, means that any two copies will result in a
ZERO grade in this project for all students involved.
4- Each student is required to submit their project in the course
Moodle, a zip file containing the project file and the submitted zip file
name must be in this format: YourName_ID.zip.
5- Any missing functions or Not working properly means zero for that
part of the project.

EXPERT ANSWER

SOLUTION:

import java.util.Scanner;
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;

public class NoSQL
{
static Scanner sc = new Scanner(System.in);
static MongoCollection<Document> collection;


//return user choice from menu
public static int menu()
{
System.out.println(“\n1.Show all Products \n2.Add Product \n3.Delete Product “
+ “\n4.Update Product \n5.Exit: “);

int choice = sc.nextInt();
sc.nextLine();

return choice;
}


// to add a product
public static void addProduct()
{
String productID, name, description, price;

System.out.println(“ProductID: “);
productID = sc.nextLine();

System.out.println(“Product Name: “);
name = sc.nextLine();

System.out.println(“Product Description: “);
description = sc.nextLine();

System.out.println(“Product Price: “);
price = sc.nextLine();

try
{
// Creating the document to be inserted
Document document = new Document(“productID”, productID).append(“name”,name)
.append(“description”, description).append(“price”, price);

// Insert the document
collection.insertOne(document);

System.out.println( “Document inserted Successfully”);
}
catch (Exception e)
{
System.out.println(“Document insertion failed”);
System.out.println(e);
}
}


// to update details of a product, product id cannot be changed
public static void updateProduct()
{
String productID, name, description, price;

System.out.println(“Enter ProductID of product to update: “);
productID = sc.nextLine();

System.out.println(“Enter updated details”);
System.out.println(“Product Name: “);
name = sc.nextLine();

System.out.println(“Product Description: “);
description = sc.nextLine();

System.out.println(“Product Price: “);
price = sc.nextLine();

try
{
collection.updateOne( Filters.eq(“productID”, productID), Updates.set(“name”, name));
collection.updateOne( Filters.eq(“productID”, productID), Updates.set(“description”, description));
collection.updateOne( Filters.eq(“productID”, productID), Updates.set(“price”, price));

System.out.println( “Successfully updated the document”);
}
catch (Exception e)
{
System.out.println(“Update failed”);
System.out.println(e);
}
}


//delete a product
public static void deleteProduct()
{
System.out.println(“Enter ProductID of product to update: “);
String productID = sc.nextLine();

try
{
collection.deleteOne(Filters.eq(“productID”, productID));
System.out.println( “Successfully deleted the document”);
}
catch (Exception e)
{
System.out.println(“Deletion failed”);
System.out.println(e);
}
}


//print all the products
public static void showAll()
{
try
{
System.out.println(“\nDisplaying all products”);

// Get the list of documents from the DB
FindIterable<Document> iterobj = collection.find();

// Print the documents using iterators
Iterator itrator = iterobj.iterator();

// for all the documents
while (itrator.hasNext())
{
System.out.println( itrator.next());
}
}
catch (Exception e)
{
System.out.println( “Could not find the documents or No document exists”);
System.out.println(e);
}
}


//main function
public static void main(String[] args)
{

//establish a connection and create database
try
{
MongoClient db = new MongoClient(“localhost”, 27017);

MongoCredential credential = MongoCredential.createCredential(“localUser”, “mongoDb”,”password”.toCharArray());

System.out.println(“Successfully Connected to the database”);

MongoDatabase database = db.getDatabase(“mongoDb”);
System.out.println(“Credentials are: ” + credential);

// Create the collection
database.createCollection(“productInfo”);
System.out.println( “Collection created Successfully”);

collection = database.getCollection(“productInfo”);
}
catch (Exception e)
{
System.out.println( “Connection establishment failed”);
System.out.println(e);
}


while(true)
{
int choice = menu();

if(choice == 1)
{
showAll();
}
else if(choice == 2)
{
addProduct();
}
else if(choice == 3)
{
deleteProduct();
}
else if(choice == 4)
{
updateProduct();
}
else if(choice == 5)
{
break;
}
else
System.out.println(“Invalid choice”);
}
}
}