IN JAVA IN JAVA Define a class named Product that contains 3 data fields: name,
ID: 3580906 • Letter: I
Question
IN JAVA
IN JAVA
Define a class named Product that contains 3 data fields: name, id and description to represent product name, product id and product description. Provide accessor and mutator methods where necessary. Write a program called ProductDB that uses an ArrayList of Product type to simulate a database of products. Your ProductDB program should provide a menu with 4 options that lets users to add a product, display all products, search for a specific product and display it, and search for a specific product with the option to delete it. Given a search keyword, your search methods should return any product where its instance variables contain the search keyword. For example, if the product name, id and description are Mighty Leaf, 23988 and Black Tea respectively then any of the search keywords of "Bla", "988", "Lea", "hty" or "Mighty" should return and display the product. Your program output should look like the sample run of the program below. A sample run of the program is as follow: Quit Add new product List all products Search product by keyword and display Search product by keyword and remove Please choose from the above menu by entering a number: 1 Enter product name: Mighty Leaf Enter product ID: 23978 Enter product description: Black Tea Quit Add new product List all products Search product by keyword and display Search product by keyword and remove Please choose from the above menu by entering a number: 1 Enter product name: HP Pavillion Enter product ID: 67238Explanation / Answer
package raj;
import java.lang.*;
import java.lang.Character;
import java.util.Scanner;
import java. util. ArrayList;
public class First {
public static ArrayList<Product> ProductList = new ArrayList<Product>(100);
public static void main(String args[]){
menu();
}
public static void menu()
{
int ch;
Scanner in = new Scanner(System.in);
String key;
System.out.println("0. Exit");
System.out.println("1. Add new Product");
System.out.println("2. List All Products");
System.out.println("3. Search product by keyword and display");
System.out.println("4. Search product by keyword and remove");
System.out.println("Please choose from the above menu bu entering a number(0-4):");
ch = in.nextInt();
switch(ch){
case 0:
System.exit(0);
case 1:
Addproduct();
System.out.println("Product Details is added");
menu();
break;
case 2:
DisplayAll();
menu();
break;
case 3:
System.out.println("Enter Key for search");
key = in.next();
SearchKey(key);
menu();
break;
case 4:
System.out.println("Enter Key for remove");
key = in.next();
Remove(key);
System.out.println("Product Details is removed");
menu();
break;
default :
System.out.println("Invalid Number");
menu();
}
}
public static void Addproduct(){
Product pt= new Product();
System.out.println("Please Enter Deatils");
System.out.println("Enter Product ID");
Scanner in = new Scanner(System.in);
pt.Product_ID= in.nextInt();
System.out.println("Enter Product Name");
pt.Product_Name = in.next();
System.out.println("Enter Product Description");
pt.Product_Description= in.next();
ProductList.add(pt);
}
public static void DisplayAll()
{
for(Product pt :ProductList)
{
System.out.println("Product ID:"+ pt.Product_ID);
System.out.println("Product Name:"+ pt.Product_Name);
System.out.println("Product ID:"+ pt.Product_Description);
}
}
public static void SearchKey(String key) {
for(Product pt :ProductList)
{
if(pt.Product_Name.startsWith(key)){
System.out.println("Product ID:"+ pt.Product_ID);
System.out.println("Product Name:"+ pt.Product_Name);
System.out.println("Product ID:"+ pt.Product_Description);
}
else
{
System.out.println("Product is not in database");
}
}
}
public static void Remove(String Key){
for(Product pt :ProductList)
{
if(pt.Product_Name.startsWith(Key)){
ProductList.remove(pt);
}
else
{
System.out.println("Product is not in database");
}
}
}
}
========== Class for product-=============
package raj;
public class Product {
public int Product_ID;
public String Product_Name;
public String Product_Description;
}