Tuesday 20 June 2017

Java Concepts

 Association Concept In Java

Association is an important concept in Object oriented Programming study.When we developed big  java application there is a need to associate one class to anther and one module to another module.So association concept in java gives you that flexibility to create coupling between multiple classes and module.

So in this article we are going to see the how associate represent in your code as well as real world problems so basically Associate concept states that it establish relation between two modules and classes through there object and methods.

From Wikipedia :-  In object-oriented programming, association defines a relationship between classes of objects that allows one object instance to cause another to perform an action on its behalf. This relationship is structural, because it specifies that objects of one kind are connected to objects of another and does not represent behaviour.
In generic terms, the causation is usually called "sending a message", "invoking a method" or "calling a member function" to the controlled object. Concrete implementation usually requires the requesting object to invoke a method or member function using a reference or pointer to the memory location of the controlled object.
The objects that are related via the association are considered to act in a role with respect to the association, if object's current state in the active situation allows the other associated objects to use the object in the manner specified by the role. A role can be used to distinguish two objects of the same class when describing its use in the context of the association. A role describes the public aspects of an object with respect to an association.


Lets see one brief example which gives you exact representation and use of association of multiple module and classe.Lets go to code area.

1.Customer Class :
import java.util.ArrayList;

public class Customer {
     private String name;
     ArrayList<Order> orders = new ArrayList<Order>();

     public Customer(String name) {
           this.name = name;
     }

     public String getName() {
           return name;
     }

     public ArrayList<Order> getOrders() {
           return orders;
     }

     public void addOrder(Order order) {
           orders.add(order);
     }

}

2.LineItem  Class :

public class LineItem {
     private int quantity;
     Product products;

     public LineItem(int quantity, Product products) {
           this.quantity = quantity;
           this.products = products;
     }

     public double calculateLineItemCost() {
           return (quantity * products.calculatePriceAfterDiscount());
     }

     public int getQuantity() {
           return quantity;
     }

     public Product getProducts() {
           return products;
     }

}

3.Order  Class :
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class Order {
     private String no;
     private String date;
     ArrayList<LineItem> lineItem = new ArrayList<LineItem>();
     SimpleDateFormat sf = new SimpleDateFormat("dd-mm-yyyy");

     public String getNo() {
           return no;
     }

     public Order(String no, String date) {
           super();
           this.no = no;
           this.date = date;
     }

     public String getDate() {
           return date;
     }

     public ArrayList<LineItem> getLineItem() {
           return lineItem;
     }

     public void addLineItem(LineItem placeOrder) {
           lineItem.add(placeOrder);
     }

}


4. Product Class :
public class Product {
     private int id;
     private String name;
     private double price;
     private float discount;

     public Product(int id, String name, double price, float discount) {
           this.id = id;
           this.name = name;
           this.price = price;
           this.discount = discount;
     }

     public double calculatePriceAfterDiscount() {
           double finalAmount = (this.price) - (this.discount);
           return finalAmount;
     }

     public int getId() {
           return id;
     }

     public String getName() {
           return name;
     }

     public double getPrice() {
           return price;
     }

     public float getDiscount() {
           return discount;
     }

}


5. Test Class :
public class AsssociationTest {
     public static void main(String[] args) throws ParseException {
           Product book = new Product(1, "OOD", 500, 100);
           Product pen = new Product(2, "Hero", 20, 5);
           Product laptop = new Product(3, "lenovo", 50000, 10000);
           Product pencil = new Product(4, "Apsara", 5, 1);
           Product shirt = new Product(5, "Cotton", 1000, 300);

           LineItem bookItem = new LineItem(5, book);
           LineItem penItem = new LineItem(5, pen);
           LineItem laptopItem = new LineItem(2, laptop);
           LineItem pencilItem = new LineItem(5, pencil);
           LineItem shirtItem = new LineItem(2, shirt);

           Order order1 = new Order("101", "21-03-2017");
           order1.addLineItem(bookItem);
           order1.addLineItem(penItem);
           order1.addLineItem(laptopItem);

           Order order2 = new Order("102", "22-03-2017");
           order2.addLineItem(pencilItem);
           order2.addLineItem(shirtItem);

           Order order3 = new Order("103", "25-04-2016");
           order1.addLineItem(bookItem);
           order1.addLineItem(penItem);
           order1.addLineItem(laptopItem);

           Order order4 = new Order("104", "26-04-2016");
           order2.addLineItem(pencilItem);
           order2.addLineItem(shirtItem);

           Order order5 = new Order("105", "27-04-2016");
           order1.addLineItem(bookItem);
           order1.addLineItem(penItem);
           order1.addLineItem(laptopItem);

           Customer sachin = new Customer("Sachin");

           sachin.addOrder(order1);
           sachin.addOrder(order2);
           sachin.addOrder(order3);
           sachin.addOrder(order4);
           sachin.addOrder(order5);

           printInfo(sachin);

           Customer sehwagh = new Customer("Sehwagh");
           sehwagh.addOrder(order4);
           sehwagh.addOrder(order5);

           printInfo(sehwagh);

     }

     public static void printInfo(Customer c) throws ParseException {
           SimpleDateFormat sf = new SimpleDateFormat("dd-mm-yyyy");
           System.err.println("Name of customer is " + c.getName());
           for(Order or:c.getOrders())
           {
                System.err.println("Order id is"+or.getNo()+" order date is "+sf.parse(or.getDate()));
           for(LineItem li:or.getLineItem() )
           {
                System.out.println("Product id is "+li.getProducts().getId());
                System.out.println("Product name is "+li.getProducts().getName());
                System.out.println("price of product is "+li.getProducts().getPrice());
                System.out.println("discount on each product is"+li.getProducts().getDiscount());
                System.out.println("Quantity of product is "+li.getQuantity());
                System.err.println("Line item cost is "+li.calculateLineItemCost());
           }
     }
     }
}

6.OUTPUT :

Product name is lenovo
price of product is 50000.0
discount on each product is10000.0
Quantity of product is 2
Line item cost is 80000.0
Order id is102 order date is Sun Jan 22 00:03:00 IST 2017
Product id is 4

Product name is Apsara
price of product is 5.0
discount on each product is1.0
Quantity of product is 5
Line item cost is 20.0
Product id is 5

Product name is Cotton
price of product is 1000.0
discount on each product is300.0
Quantity of product is 2
Line item cost is 1400.0
Product id is 4

Product name is Apsara
price of product is 5.0
discount on each product is1.0
Quantity of product is 5
Line item cost is 20.0
Product id is 5

Product name is Cotton
price of product is 1000.0
discount on each product is300.0
Quantity of product is 2

Line item cost is 1400.0

Order id is103 order date is Mon Jan 25 00:04:00 IST 2016

Order id is104 order date is Tue Jan 26 00:04:00 IST 2016

Order id is105 order date is Wed Jan 27 00:04:00 IST 2016

Name of customer is Sehwagh

Order id is104 order date is Tue Jan 26 00:04:00 IST 2016

Order id is105 order date is Wed Jan 27 00:04:00 IST 2016


No comments:

Post a Comment

Spring-App

Application In Spring Spring Framework is an open source Java Platform that provides comprehensive infrastructure support for developi...