July 28, 2022

How to find the count of employees in each department using java 8 streams and collection | interview questions MAANG

/**
 * 
 */
package com.learning.stockmarket;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author vishnu prasad blog
 *
 */
public class MainClass {

/**
* @param args
*/
public static void main(String[] args) {
List<Employee> empList = addEmployee();
Map<Integer, Long> result = empList.stream().collect(Collectors.groupingBy(Employee:: getDeptId, Collectors.counting()));
System.out.println(result);
}

private static List<Employee> addEmployee() {
List<Employee> list = new ArrayList<>();
int id = 1;

for (int i = 0; i < 100; i++) {
byte[] array = new byte[7]; 
    new Random().nextBytes(array);
    String name = new String(array, Charset.forName("UTF-8"));
    
    int dept = new Random().nextInt(6);

Employee employee = new Employee(id, name, dept);
id = id++;
list.add(employee);

}

return list;
}

}

Employee


/**
 * 
 */
package com.learning.stockmarket;

/**
 * @author vishnu prasad blog

 * Kozhikode
 */
public class Employee {
private int id;
private String name;
private int deptId;
public Employee(int id, String name, int deptId) {
this.id = id;
this.name = name;
this.deptId = deptId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}


}


July 21, 2022

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

Problem

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

This issue happens due to version mismatch between swaggerfox and spring boot framework.


Solution




/**
 * @author aswathi sajeevan
 *
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build();
}
}


Pom.xml



<!--  swagger  -->

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>


Spring boot version



<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2-SNAPSHOT</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>



Swagger url


http://localhost:9090/swagger-ui/index.html




July 14, 2022

How to create a textile billing and inventory system using google spreadsheet and google app sheet

Hi 


We have created a textile inventory and billing system for Nijeeshma tailoring athanikkal kozhikode using google app sheet and google spreadsheet. Any one can develop this application with a day or two. No coding required


Please visit our git url


Video

July 07, 2022

How to use observable pattern to notify stock market breakouts entry stoploss


Created a stock market application by implementing observable pattern.
In observable pattern the subsribers will be notified when ever there is any event occured in publishers object. In our example stock is the publisher ( Observables) and we kept a list of subscribers list in NotificationManager class.



Stock.java


package com.learning.designpattern.observer;

import java.io.File;
/**
 * Stock class
 * Act as Observable or publisher
 * 
 * Where ever a change happens in Stock object (publisher), it will notifies the subscribers
 * 
 * two methods stoploss() & entry() corresponds to file opening and saving events
 * 
 *
 * 
 * 
 * @author smithesh k k
 *
 */
public class Stock {

private Breakout breakout;
public NotificationManager events;

public Stock() {
this.events = new NotificationManager(); 
}

public void stoploss(String nseTicker) {
if (!nseTicker.isEmpty()) {
this.breakout = new Breakout(nseTicker);
events.notify(Alerts.STOPLOSS.toString(), breakout);
}
}

public void entry(String nseTicker) {
if (!nseTicker.isEmpty()) {
this.breakout = new Breakout(nseTicker);
events.notify(Alerts.ENTRY.toString(), breakout);
}
}

}

NotificationManager.java


/**
 * 
 */
package com.learning.designpattern.observer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  NotificationManager
 *  
 *  keeps the subscriber list ( watchlist), through NotificationManager you can subscribe or unsusbscribe for an event
 *  
 *  notify() helps to notify the subscribers/observer's
 * 
 * 
 * @author bijuvas
 *
 */
public class NotificationManager {

private Map<String, List<EventListener>> watchlist;
public NotificationManager() {
this.watchlist = new HashMap<>();
addEvents();
}
public void notify(String eventType, Breakout file) {
List<EventListener> users = watchlist.get(eventType);
users.forEach(user -> {
user.update(eventType, file);
});
}
public void subscribe(String eventType, EventListener listener) {
List<EventListener> users = watchlist.get(eventType);
users.add(listener);
}
public void unsubscribe(String eventType, EventListener listener) {
List<EventListener> users = watchlist.get(eventType);
users.remove(listener);
}

public void addEvents() {
final Alerts[] alerts = Alerts.values();
for (Alerts event : alerts) {
            this.watchlist.put(event.toString(), new ArrayList<>());
        }
}
}

EventListener.java


/**
 * 
 */
package com.learning.designpattern.observer;

/**
 * EventListener
 * 
 * update()
 * 
 * @author sreekumar
 *
 */
public interface EventListener {
public void update(String eventType, Breakout breakout);
}


EmailNotificationListener.java


/**
 * 
 */
package com.learning.designpattern.observer;

/**
 * EmailNotificationListener
 * 
 * @author perambra kozhikode
 *
 */
public class EmailNotificationListener implements EventListener {
private String email;
public EmailNotificationListener(String email) {
this.email = email;
}

@Override
public void update(String eventType, Breakout breakout) {
System.out.println(" email send to "+email +" for "+eventType);
}

}


MainClass.java


/**
 * 
 */
package com.learning.designpattern.observer;

/**
 * @author valiyakode
 *
 */
public class MainClass {

/**
* @param args
*/
public static void main(String[] args) {
Stock editor = new Stock();
NotificationManager event = editor.events;
event.subscribe(Alerts.STOPLOSS.toString(), new EmailNotificationListener("test@gmail.com"));
editor.stoploss("SGX");
}

}


Breakout.java


/**
 * 
 */
package com.learning.designpattern.observer;

/**
 * @author Zamorins Guruvayoorappan college
 *
 */
public class Breakout{  //implements Strategies{

public Breakout(String nseTicker) {

}


Alerts.java


/**
 * 
 */
package com.learning.designpattern.observer;

/**
 * @author lijesh
 *
 */
public enum Alerts {
TARGET1, ENTRY, TARGET2, STOPLOSS;

}


Full source code


Git url
 

Facebook comments