Sunday, February 13, 2022

Find second and third maximum salary from employee

//Second highest salary

 SELECT name, MAX(salary) AS salary FROM employee WHERE salary <> (SELECT MAX(salary) FROM employee);

//Third highest salary

SELECT name, MAX(salary) AS salary FROM employee WHERE salary < (SELECT MAX(salary) FROM employee WHERE salary < (SELECT MAX(salary)FROM employee)); 

Composite design pattern

 Composite design pattern is the part of structural design pattern .

When we need a composite object what will maintain a tree structure then we can use composite design pattern .

let's see an example . We want to develop our company organogram -

Project managers and sales managers works under the CTO.  Developer works under the Project managers. Sales persons works under the sales manager.

public class Employee {

private String name;

private String department;

private int salary;

List<Employee> suborddinates;

public Employee(String name, String department, int salary) {

this.name = name;

this.department = department;

this.salary = salary;

suborddinates = new ArrayList<Employee>();

}

public void addSubordinates(Employee employee) {

suborddinates.add(employee);

}

public List<Employee> getSubordinates() {

return suborddinates;

}

@Override

public String toString() {

return "Employee [name=" + name + ", department=" + department + ", salary=" + salary + "]";

}


//Main class

public class MainApp {

public static void main(String[] args) {

Employee cto = new Employee("msr", "CTO", 100000);

Employee salesManager = new Employee("Hera", "Sales Manager", 80000);

Employee productManager = new Employee("Shahin", "Product Manager", 80000);

Employee sales1 = new Employee("makku", "Sales", 10000);

Employee sales2 = new Employee("Rumana", "Sales", 20000);

Employee dev1 = new Employee("lokman", "Developer", 60000);

Employee dev2 = new Employee("Sahabur", "Developer", 60000);


cto.addSubordinates(productManager);

cto.addSubordinates(salesManager);


productManager.addSubordinates(dev1);

productManager.addSubordinates(dev2);


salesManager.addSubordinates(sales2);

salesManager.addSubordinates(sales1);


System.out.println(cto.toString());

for (Employee manager : cto.getSubordinates()) {

System.out.println(manager.toString());

for (Employee junior : manager.getSubordinates()) {

System.out.println(junior.toString());

}

}

//output

Employee [name=msr, department=CTO, salary=100000]

Employee [name=Shahin, department=Product Manager, salary=80000]

Employee [name=lokman, department=Developer, salary=60000]

Employee [name=Sahabur, department=Developer, salary=60000]

Employee [name=Hera, department=Sales Manager, salary=80000]

Employee [name=Rumana, department=Sales, salary=20000]

Employee [name=makku, department=Sales, salary=10000]

Prototype design pattern

 Prototype design pattern is a creational design pattern. It is used when object creation is costly and we want to use same object . So we can create prototype of the current object and can use it . let's see the example,

Book class-

public class Book {

private int bid;

private String bname;

public Book(int bid, String bname) {

super();

this.bid = bid;

this.bname = bname;

}


public int getBid() {

return bid;

}

public void setBid(int bid) {

this.bid = bid;

}

public String getBname() {

return bname;

}

public void setBname(String bname) {

this.bname = bname;

}

@Override

public String toString() {

return "Book [bid=" + bid + ", bname=" + bname + "]";

}

//BookShop class--

public class BookShop implements Cloneable {// must implement cloneable interface to copy of the                                                                                      object

private String shopName;

private List<Book> books;

public BookShop(String shopName) {

super();

this.shopName = shopName;

books = new ArrayList<Book>();

}

public String getShopName() {

return shopName;

}

public void setShopName(String shopName) {

this.shopName = shopName;

}

public List<Book> getBooks() {

return books;

}

public void setBooks(List<Book> books) {

this.books = books;

}

public void loadBooks() {

for (int i = 0; i < 10; i++) {

Book book = new Book(i, "book" + i);

books.add(book);

}

}

@Override

protected Object clone() throws CloneNotSupportedException { //should override clone() method

return super.clone();

}

@Override

public String toString() {

return "BookShop [shopName=" + shopName + ", books=" + books + "]";

}

}

//Main class

public class MainApp {

public static void main(String[] args) {

BookShop shop1 = new BookShop("shop 1");

shop1.loadBooks();

System.out.println(shop1);

try {

BookShop shop2 =(BookShop) shop1.clone();

shop2.setShopName("shop2");

System.out.println(shop2);

} catch (CloneNotSupportedException e) {

e.printStackTrace();

}

}

}



Saturday, February 12, 2022

Factory design pattern

The factory design pattern is used when we have a superclass with multiple sub-classes and based on input, we need to return one of the sub-class. 

Let's see an example.

//parent class

public interface Phone{

void makeCall();

}

//subclass 1

public class AndroidPhone implements Phone {

@Override

public void makeCall() {

System.out.println("calling using android phone");

}

}

//subclass 2

public class Iphone implements Phone {

@Override

public void makeCall() {

System.out.println("Calling using iphone.");

}

}

//subclass 3

public class Windows implements Phone {

@Override

public void makeCall() {

System.out.println("calling using windows phone.");

}

}

//factory

public PhoneFactory{

public Phone getPhone(String phoneName){

if(phoneName.equals("Android")){

return new AndroidPhone();

}

else if(phoneName.equals("lphone")){

return new Lphone();

}

else{

return new Windows();

}

}

//MainClass

public static void main(String[] args){

PhoneFactory phoneFactory = new PhoneFactory();

Phone phone = phoneFactory.getPhone("Android");

phone.makeCall();

}


Wednesday, February 2, 2022

Remove duplicate element from array

                // remove duplicate from an array using set

int[] ar = { 1, 1, 2, 3, 4, 5, 6, 6, 7, 8 };

Set<Integer> uniqueElements = new HashSet<>();

for (int i = 0; i < ar.length; i++) {

uniqueElements.add(ar[i]);

}

for (Integer i : uniqueElements) {

System.out.print(i+" ");

}

                  //Using HashMap

Map<Integer, Integer> uniqueElements = new HashMap<>();

for (int i = 0; i < ar.length; i++) {

if (uniqueElements.get(ar[i]) == null) {

System.out.print(ar[i] + " ");

uniqueElements.put(ar[i], ar[i]);

}

}


Fluent interface pattern

 public class UserConfigurationManager {     private String userName;     private String password;     private UserConfigurationManager() { ...