September 28, 2020

Java Sample Code to convert a calendar to string object





//2020-06-05T00:00:00.000Z
public static String calendarToStr(Calendar calendar) {
try {
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);

Instant instant = calendar.getTime().toInstant();

LocalDateTime localDateTime = instant.atZone(ZoneId.of(DEFAULT_ZONE_ID, ZoneId.SHORT_IDS)).toLocalDateTime();

String formattedDate = inputFormatter.format(localDateTime);

return formattedDate;


                catch (Exception e) {
e.printStackTrace();
}


return null;
}

September 21, 2020

Hibernate Interview questions

SessionFactory

SessionFactory is a factory class developed on factory design pattern, to get a session object.
its responsibility is to read hibernate configuration class, 
connect to database and 
create sesson object.


Session

Core interface use to perform database operation, 
life cycle of a session is bound by begining and end transaction.


Difference between currentSession() and openSession() 

currentSession is to get the current session object.
we dont need to close it.
it belongs to hibernate context. once sessionfactory gets closed the session object close.

opensession is to open a new session.
we need to manually close after the operation.


Difference between get() and load()

get method loads data as soon as it is called. 
load method returns a proxy object when ever it called. a lazy loading process


What are the patterns used in hibernate ?

1. Domain model pattern
2. Factory pattern - example SessionFactory class
3. Proxy pattern -
4. Data Mapper -



Second level cache ( EH Cache)

dependency : hibernate-ehcache
class : EhCacheRegionFactory

Separate ehcache.xml file where we give details like diskstore path, time to live etc

@cache annotation has to be used in entity class



Immutable class in java



“Hope is important because it can make the present moment less difficult to bear. If we believe that tomorrow will be better, we can bear a hardship today.”

September 14, 2020

Pain is inevitable, but suffering is a choice


A senior monk & a junior monk were traveling. 

They reached a river with a strong current. They were preparing to cross, & they saw an attractive woman also attempting to cross.

She asked if they could help her cross to the other side.

They both glanced at one another because they had taken vows not to touch a woman.

Then, without a word, the older monk picked up the woman, carried her across the river, placed her gently on the other side, & started walking.

The younger monk was shocked to see. After rejoining his companion, he was speechless, & an hour passed without a word between them.

Two more hours, then three, finally the younger monk asked “As monks, we are not permitted a woman, how could you then carry that woman on your shoulders?”

The older monk replied calmly, “Dear, I set her down on the other side of the river hours ago, WHY ARE YOU STILL CARRYING HER?”
______

In life, sometimes we keep carrying the past hurts, worries, sadness, guilt, resentments so tight, that we forget to live the present, unaware that the person we are hurting is our own self.

Pain is inevitable, but suffering is a choice.

We can choose to let go of what doesn’t serve us anymore, & live in the NOW, because now is all that exists.

September 07, 2020

Chef's in paris cooking with ingredients: How to solve below complex problem using java

below is the problem :-

Chef's ingredients:-


1.The chef receives exactly 1 ingredient per day from the market.The
ingredients never repeat.

2. Every ingredient belongs to 1 of the 3 categories namely FIBER,FAT & CARB.

3.Every ingredient has a unique ingredient id.

4.The ingredient id always starts with the category name (ex:FIBERBroccoli,FATCheese,CARBRice)

Chef's Dishes

1. All of the chef's dishes have a constant number of ingredients.(this will be your program's input)

2. All the ingredients used will be fully used in a Dish. The chef doesn' use some part/quntity of an ingredient.

3.All of the chef's dishes mush have at least 60% of the ingredients from a single category.(i.e. if the chef cooks using
4 ingredients,then at leaast 3 FAT ingredients OR at least 3 FIBER ingredients OR at least 3 CARB ingredients are needed)


Chef's Cooking style:-

1. If the chef has multiple options of ingredients for the dish,then he takes the oldest possible ones to cook
in the order of their arrival.

2.After the chef prepares a dish,the ingredients used can Not be reused as theyhave been already used.

3.The chef prepares a maximum of 1 dish per day.

4.if the Chef does not have enough ingredients to cook the dish with above mentioned rules,then he does not
cook on that day.

Given the input array of ingredient id that the chef receives every day (i.e. array index is the day number)
write a program to print when does the chef cook a dish and when he does not.


Input:-

Line 1: The total number of days for the scope of the problem(1<=input<=20)

Line 2: The exact number of ingredients that chef uses to cook(1<=input<=20)

Line 3: The space separated ingredient ids.(6<=length(ingredientid)<=20)

Output:- Print the ":" separated used ingredient ids in order of their arrival if the chef cooks on that day
and print "-" if the chef doesn't cook anything on that day.Print the output as  single string.

Example input 1:

5
3
FATOil FATCheese FATEgg FIBERSpinach CARBRice FIBERBeans

Example INPUT 2:

6
3
FATOil:FATCheese FATEgg FIBERSpinach CARBRice FIBERBeans

EXAMPLE OUTPUT 2:

--FATOil:FATCheese:FATEgg--FIBERSpinach:CARBRice:FIBERBeans

EXAMPLE INPUT 3:

12
4
FATOil FIBERSpinach CARBRice FATCheese FIBERBeans FATEgg
FIBERBroccoil CARBPotato CARBCorn FATOlive FIBERCarrot




package com.cfed;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class Solution {

public static void main(String args[]) {

String[] ingre = { "FATOil", "FIBERSpinach", "CARBRice", " FATCheese", "FIBERBeans" };
Map<Integer, String> ingred = new HashMap<Integer, String>();
ingred.put(1, "FATOil");
ingred.put(2, "FIBERSpinach");
ingred.put(3, "CARBRice");
ingred.put(4, "FATCheese");
ingred.put(5, "FIBERBeans");
ingred.put(6, "FATEgg");
ingred.put(7, "FIBERBrocoli");
ingred.put(8, "CARBPotato");
ingred.put(9, "CARBCorn");
ingred.put(10, "FATOlive");
ingred.put(11, "FIBERCarrot");
ingred.put(12, "CARBBeetroot");

int days = 6;
findChefCookingDays(days, ingred);

}

List<String> categories = List.of("FAT", "FIBER", "CARB");
static int fatCount = 0;
static int fiberCount = 0;
static int CarbCount = 0;

static List<String> fatList = new ArrayList<>();
static List<String> fiberList = new ArrayList<>();
static List<String> CarbList = new ArrayList<>();

private static void findChefCookingDays(int days, Map<Integer, String> ingred) {

for (Entry<Integer, String> entry : ingred.entrySet()) {

if (entry.getValue().contains("FAT")) {
fatList.add(entry.getValue());
fatCount++;
} else if (entry.getValue().contains("FIBER")) {
fiberList.add(entry.getValue());
fiberCount++;
} else if (entry.getValue().contains("CARB")) {
CarbList.add(entry.getValue());
CarbCount++;
}

int isCook = tryToCook(ingred);
System.out.print(isCook);

}

/*
* for (String ing : ingred) { tryToCook(); if (ing.contains("FAT")) {
* fatCount++; } else if (ing.contains("FIBER")) { fiberCount++; } else if
* (ing.contains("CARB")) { CarbCount++; }
* }
*/

}

private static int tryToCook(Map<Integer, String> ingred) {

if (fatList.size() > 2 || (fatList.size() >= 2 && (fiberList.size() > 0 || CarbList.size() > 0))) {
if (CarbList.size() > 0) {
CarbList.remove(0);
}else if (fiberList.size() > 0) {
fiberList.remove(0);
} else if (fatList.size() > 2) {
fatList.remove(0);
}
fatList.remove(0);
fatList.remove(0);

return 1;

} else if (fiberList.size() >2 ||( fiberList.size() >= 2 && (fatList.size() > 0 || CarbList.size() > 0))) {

if (fatList.size() > 0) {
fatList.remove(0);
} else if (CarbList.size() > 0) {
CarbList.remove(0);
} else if (fiberList.size() > 2) {
fiberList.remove(0);
}
fiberList.remove(0);
fiberList.remove(0);

return 1;

} else if (CarbList.size() > 2 || (CarbList.size() >= 2 && (fatList.size() > 0 || fiberList.size() > 0))) {

if (fatList.size() > 0) {
fatList.remove(0);
} else if (fiberList.size() > 0) {
fiberList.remove(0);
} else if (CarbList.size() > 2) {
CarbList.remove(0);
}
CarbList.remove(0);
CarbList.remove(0);
return 1;
} else {

return 0;
}

}

}

Facebook comments