Answer to Question #237736 in Java | JSP | JSF for Eulanda

Question #237736
The user must have the ability to search for a product. The user will select menu item
two (2), which will prompt the user to enter a product code. If a valid product is found in
the application, then display the product details to the user. If no valid product is found,
display an error message to the user that the product cannot be located.
1
Expert's answer
2021-09-16T13:02:03-0400
import java.util.Scanner;
public class Main
{
    public static void Search(int[] arr, int k){    
        for(int i=0;i<arr.length;i++){    
            if(arr[i] == k){    
                System.out.println("The product is found.");
                break;
            }    
        }    
        System.out.println("The product is not found.");    
    }
	public static void main(String[] args) {
	    Scanner in=new Scanner(System.in);
	    System.out.println("Enter number of products: ");
	    int n=in.nextInt();
		Product [] p=new Product[n];
		
		for(int i=0;i<n;i++){
		    p[i].getData();
		}
		for(int i=0;i<n;i++){
		    p[i].display();
		}
		
		int c;
		System.out.println("1. Add an product\n2. Search a product\nEnter your choice: ");
	    c=in.nextInt();
	    int index=n;
	    if (c==1){
	        p[n].getData();
	    }
	    
		else if (c==2){
    		int[] arr= {121,122,123,124,125,126,127};  
    		System.out.println("Enter the product code to search:");
    		int product_code=in.nextInt();
    		
    		Search(arr,product_code);
		} 
	}
}




class Product{
    private int p_code;
    private String p_category;
    private String p_warranty;
    private double p_price;
    
    public void getData(){
        Scanner in=new Scanner(System.in);
        System.out.println("Enter the product code: ");
	    String p_code=in.next();
	    System.out.println("Enter product category: ");
	    String p_category=in.next();
	    System.out.println("Enter the product warranty: ");
	    String p_warranty=in.next();
	    System.out.println("Enter the product price: ");
	    String p_price=in.next();
    }
    public void display(){
        System.out.println("Product code: "+p_code);
	    System.out.println("Product category: "+p_category);
	    System.out.println("Product warranty: "+p_warranty);
	    System.out.println("Product price: "+p_price);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS