Please open the menu to show more

Saturday, July 8, 2017

Program to show how we use collection in real life applications

In this application we will create multiple objects of Pen and store them inside collection.
Then we will fetch those objects of Pen from the collection and perform some business logic (i mean some operations on those objects).

// COPY THIS CODE AND SAVE IT INSIDE Pen.java

public class Pen {

/*
* lets create some data members
*/
private String type;
private int price;
private String brand;

/*
* lets define setter and getter methods to store and fetch
* the values to/from the data members
*/
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}


}



// COPY THIS CODE AND SAVE IT INSIDE StoreAndFetchPen.java


import java.util.ArrayList;
import java.util.Scanner;

public class StoreAndFetchPen {

/*
* Here i am defining some static methods 
* static methods can be accessed directly inside same class without
* creating objects.

* Inside these methods i will pass object of ArrayList (which was created inside 
* main method) 
*/

/*
* This method will search Pen inside collection on the basis of brand
*/
public static void searchPenByBrand(ArrayList<Pen> pens) {

System.out.println("##Search Pen by brand##");

Scanner s=new Scanner(System.in);
System.out.print("Enter the brand of Pen to search inside collection: ");
String brand=s.nextLine();

int i=1;

System.out.println("Showing the Pen of "+brand+" company");

/*
* Fetch the objects of Pen form the collection using for-each-loop
*/
for(Pen p : pens) {
/*
* Call getter methods in order to fetch the data from object of Pen
*/
String pType=p.getType();
int pPrice=p.getPrice();
String pBrand=p.getBrand();
/*
* brand is taken from keyboard and pBrand is fetched from collection
*/
if(brand.equalsIgnoreCase(pBrand)) {
System.out.println("Pen-"+i+"->");
System.out.println("Type:"+pType);
System.out.println("Price: "+pPrice);
System.out.println("Brand: "+pBrand);
i++;
System.out.println("------------------------------------");
}

}

}

// ---------------------------------------------------------------------

/*
* This method will search Pen inside collection on the basis of price range
*/
public static void searchPenByPriceRange(ArrayList<Pen> pens) {

System.out.println("##Search Pen by price range##");
Scanner s=new Scanner(System.in);
System.out.print("Enter the min price: ");
int minPrice=s.nextInt();

System.out.print("Enter the min price: ");
int maxPrice=s.nextInt();

System.out.println("Showing the Pen of in the range of "+minPrice+" and "+maxPrice+"->");

int i=1;

/*
* Fetch the objects of Pen form the collection using for-each-loop
*/
for(Pen p : pens) {
/*
* Call getter methods in order to fetch the data from object of Pen
*/
String pType=p.getType();
int pPrice=p.getPrice();
String pBrand=p.getBrand();
/*
* minPrice and maxPrice were taken from keyboard and pPrice is fetched from collection
*/
if(pPrice>=minPrice && pPrice<=maxPrice) {
System.out.println("Pen-"+i+"->");
System.out.println("Type:"+pType);
System.out.println("Price: "+pPrice);
System.out.println("Brand: "+pBrand);
i++;
System.out.println("------------------------------------");
}
}
}

// ---------------------------------------------------------------------------

/*
* This method will search Pen inside collection on the basis of type
*/
public static void serachPenByType(ArrayList<Pen> pens) {

System.out.println("##Search Pen by type##");

Scanner s=new Scanner(System.in);
System.out.print("Enter the type of Pen to search inside collection: ");
String type=s.nextLine();

int i=1;

System.out.println("Showing the Pen of "+type+" type");

/*
* Fetch the objects of Pen form the collection using for-each-loop
*/
for(Pen p : pens) {
/*
* Call getter methods in order to fetch the data from object of Pen
*/
String pType=p.getType();
int pPrice=p.getPrice();
String pBrand=p.getBrand();

/*
* type is taken from keyboard and pType is fetched from collection
*/
if(type.equalsIgnoreCase(pType)) {
System.out.println("Pen-"+i+"->");
System.out.println("Type:"+pType);
System.out.println("Price: "+pPrice);
System.out.println("Brand: "+pBrand);
i++;
System.out.println("------------------------------------");
}
}
}


public static void main(String[] args) {

/*
* scanner objects to take input from keyboard
*/
// this scanner will be used to input string with spaces
Scanner sc1 = new Scanner(System.in);
// this scanner will be used to take input int value
Scanner sc2 = new Scanner(System.in);

System.out.print("How many pen's required: ");
int noOfPens = sc1.nextInt();

/*
* lets create a collection to store objects of Pen here i am using
* ArrayList and using generics to ensure type safety
*/
ArrayList<Pen> listOfPens = new ArrayList<Pen>();

/*
* in this loop we will do following things 1. get info of Pen from
* keyboard 2. create object of Pen 3. store info (which was taken from
* keyboard) of Pen using setter methods 4. add object of Pen inside
* ArrayList
*/
for (int i = 1; i <= noOfPens; i++) {

// 1. get info from keyboard
System.out.println("Input info for Pen-" + i + ": ");
System.out.print("Input type (like ball/gell/fountain): ");
String penType = sc2.nextLine();
System.out.print("Input price: ");
int penPrice = sc1.nextInt();
System.out.println("Input brand: ");
String penBrand = sc2.nextLine();

// 2. create object of Pen
Pen p = new Pen();

// 3. store info inside object of Pen using setter methods
p.setType(penType);
p.setPrice(penPrice);
p.setBrand(penBrand);

// 4. store object of Pen inside ArrayList using add() method
listOfPens.add(p);
}

System.out.println("##Choose given options##");
System.out.println("1=Search a Pen by Type: ");
System.out.println("2=Search a Pen by Price: ");
System.out.println("3=Search a Pen by Brand: ");
int choice=sc1.nextInt();

/*
* Call methods on the basis of user input 
*/
if(choice==1) {
serachPenByType(listOfPens);
}
else if(choice==2) {
searchPenByPriceRange(listOfPens);
}
else if(choice==3) {
searchPenByBrand(listOfPens);
}
else {
System.out.println("Invalid choice.......");
}

}
}

1 comment: