Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views2 pages

Mock

The document shows how to store objects in an array, call methods on the array to find objects by id or name, and find the object with the minimum price.

Uploaded by

liseyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Mock

The document shows how to store objects in an array, call methods on the array to find objects by id or name, and find the object with the minimum price.

Uploaded by

liseyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

//read - how many objects you need to store in the array


int size = s.nextInt();
// Create array - using the size
Product[] parray = new Product[size];
//Read data and create objects and then add the objects in to the array
for(int i=0;i<parray.length;i++ ){
//read data
int id = s.nextInt();
s.nextLine();
String name = s.nextLine();
double price = s.nextDouble();
//create object

Product p = new Product(id,name,price);

//add object into array


parray[i]= p;
}
System.out.println(parray.length);
// call method1
//printAllProduct(parray);
//call method2
int searchId = s.nextInt();
Product prod1 = findById(null,searchId );

Product prod2 = findProdWithMinPrice(parray);


//

}
public static void printAllProduct(Product[] parray){
for(Product p : parray){
System.out.println(p.getName());
}
}

public static Product findById(Product[] parray , int id){

if(parray==null)return null;
//do logic
for(int i=0; i<parray.length;i++){
if(parray[i].getId()==id){
return parray[i];
}
}//end for

return null;
}
public static Product findByName(Product[] parray , String name){

if(parray==null)return null;
//do logic
for(int i=0; i<parray.length;i++){
if(parray[i].getName().equalsIgnoreCase(name)){
return parray[i];
}
}//end for

return null;
}
public static Product findProdWithMinPrice(Product[] parray){
if(parray==null)return null;

//logic
Product minp = parray[0]; // assume first product having the min price
for(int i=0; i<parray.length;i++){
if(parray[i].getPrice() < minp.getPrice()){
minp = parray[i];
}
}//end for

return minp;
}

You might also like