This is real time company problem statements or machine test in java on a Leave Allocation Calculator program. The program should take the following inputs:
Inputs:
- Number of Leaves: An integer representing the total number of leaves to allocate.
- Start Date: A string in the format “dd/MM/yyyy” representing the start date for leave allocation.
- isSaturdayHoliday: A boolean value indicating whether Saturdays are considered holidays.
- isSundayHoliday: A boolean value indicating whether Sundays are considered holidays.
- Company Holidays: A list of strings in the format “dd/MM/yyyy” representing the specific dates that are considered company holidays.
The program should calculate the leave allocation based on the provided inputs. The allocation rules are as follows:
- The leave allocation starts from the given start date.
- If a specific date is mentioned as a company holiday, leave cannot be allocated on that date.
- If isSaturdayHoliday is set to true, leave cannot be allocated on Saturdays.
- If isSundayHoliday is set to true, leave cannot be allocated on Sundays.
The program should output the allocated leave dates in chronological order.
Your task is to write a Java program that implements the Leave Allocation Calculator. The program should include the following:
- A main method that reads the inputs and calls the necessary methods to calculate and display the leave allocation.
- A method,
calculateLeaveAllocation
, that takes the inputs and returns a list of allocated leave dates based on the rules mentioned above.
Write a well-structured and efficient program to solve the problem. Use appropriate data structures and follow best coding practices. Handle any necessary input validation or error handling as per your understanding of the problem requirements.
Note: The program should be implemented using Java and the java.time
package for date and time operations.
Sample Input : Machine Test Leave Allocation Calculator
# Java coding challenges with solutions
Based on the provided information, here is a breakdown of the leave allocation:
- Number of Leaves: 9
- Start Date: 22/12/2022
- Saturday Holiday: No
- Sunday Holiday: Yes
- Company Holidays: 23/12/2022, 26/12/2022
To calculate the leave allocation, we need to consider the weekends and company holidays.
Starting from the given start date, we have the following days:
- 22/12/2022 (Thursday)
- 23/12/2022 (Friday) – Company Holiday
- 24/12/2022 (Saturday) –
- 25/12/2022 (Sunday) – Sunday Holiday Weekend
- 26/12/2022 (Monday) – Company Holiday
- 27/12/2022 (Tuesday)
- 28/12/2022 (Wednesday)
- 29/12/2022 (Thursday)
- 30/12/2022 (Friday)
- 31/12/2022 (Saturday)
- 01/01/2023 (Sunday) – Sunday Holiday Weekend
- 02/01/2023 (Monday)
- 03/01/2023 (Tuesday)
So, out of the 13 leaves, you would need to take leaves on the following days:
- 22/12/2022 (Thursday)
- 24/12/2022 (Saturday) –
- 27/12/2022 (Tuesday)
- 28/12/2022 (Wednesday)
- 29/12/2022 (Thursday)
- 30/12/2022 (Friday)
- 31/12/2022 (Saturday)
- 02/01/2023 (Monday)
- 03/01/2023 (Tuesday)
Please note that this calculation assumes that weekends (Saturdays) are working days. If Saturdays are also considered as non-working days, the allocation of leaves may be different.
Solution 1 : Machine Test Leave Allocation Calculator
# Java coding challenges with solutions 1
Here’s a Java code snippet that calculates the leave allocation based on the provided information:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class Main{
public static void main(String[] args) {
int noOfLeaves = 9;
String startDateString = "22/12/2022";
boolean isSaturdayHoliday = false;
boolean isSundayHoliday = true;
List<String> companyHolidays = new ArrayList<>();
companyHolidays.add("23/12/2022");
companyHolidays.add("26/12/2022");
// Parse start date
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate startDate = LocalDate.parse(startDateString, formatter);
// Calculate leave allocation
List<LocalDate> leaveDates = calculateLeaveAllocation(noOfLeaves, startDate, isSaturdayHoliday, isSundayHoliday, companyHolidays);
// Print leave dates
System.out.println("Leave Allocation:");
for (LocalDate leaveDate : leaveDates) {
System.out.println(leaveDate.format(formatter));
}
}
private static List<LocalDate> calculateLeaveAllocation(int noOfLeaves, LocalDate startDate, boolean isSaturdayHoliday, boolean isSundayHoliday, List<String> companyHolidays) {
List<LocalDate> leaveDates = new ArrayList<>();
LocalDate currentDate = startDate;
// Iterate until all leaves are allocated
while (leaveDates.size() < noOfLeaves) {
// Check if current date is a company holiday
if (companyHolidays.contains(currentDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")))) {
currentDate = currentDate.plusDays(1);
continue;
}
// Check if current date is a Saturday or Sunday holiday
if ((isSaturdayHoliday && currentDate.getDayOfWeek() == DayOfWeek.SATURDAY) || (isSundayHoliday && currentDate.getDayOfWeek() == DayOfWeek.SUNDAY)) {
currentDate = currentDate.plusDays(1);
continue;
}
// Allocate leave on the current date
leaveDates.add(currentDate);
currentDate = currentDate.plusDays(1);
}
return leaveDates;
}
}
This code uses the java.time
package introduced in Java 8 for working with dates and time. It calculates the leave allocation based on the given parameters and prints the allocated leave dates. Please note that this code assumes valid input and does not handle exceptions or input validation. You may need to add additional error handling and input validation logic as per your requirements.
Solution 2 : Leave Allocation Calculator (Spring Boot)
# Java coding challenges with solutions 1
Take input from postman.
Service Layer Interface :
public interface LeavesService {
public List<String> findLeavesDate(Leaves leaves);
}
Its declares a method findLeavesDate()
that takes a parameter of type Leaves
and returns a List
of String
objects.
Based on the code snippet, LeavesService
appears to be a service interface related to handling leaves or vacation time. The findLeavesDate()
method likely takes a Leaves
object as input and returns a list of strings representing the dates of leaves.
Please note that without the definition of the Leaves
class, it is not possible to provide further details about the exact functionality of this interface. The Leaves
class likely contains properties or methods related to leaves, such as leave type, duration, or employee information.
Service Layer Implementation :
public class LeavesServiceIMPL implements LeavesService {
@Override
public List<String> findLeavesDate(Leaves leaves) {
List<String> list = new ArrayList<>();
String startDate = leaves.getStartDate();
String[] dmy = startDate.split("/");
int day = Integer.parseInt(dmy[0]);
int month = Integer.parseInt(dmy[1]);
int year = Integer.parseInt(dmy[2]);
Month m = Month.of(month);
boolean isLeapYear = isLeapYear(year);
for (int i = 0; i <leaves.getNoOfLeaves(); i++) {
boolean isWeekEnd;
boolean iscompanyHoliday = false;
LocalDate localDate = LocalDate.of(year, month, day);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formattedDate = localDate.format(dateTimeFormatter);
isWeekEnd = isWeekEnd(localDate, leaves.getIsSaturdayHoliday(), leaves.getIsSundayHoliday());
if(!isWeekEnd) {
iscompanyHoliday = isCompanyHoliday(formattedDate, leaves.getCompanyHoliday());
}
if (!isWeekEnd && !iscompanyHoliday) {
list.add(formattedDate);
if (list.size() == leaves.getNoOfLeaves()) {
break;
}
}
if (day == m.length(isLeapYear)) {
System.out.println(m.getValue());
if (m.getValue() == 12) {
month = 1;
day = 1;
year = year + 1;
} else {
month = month + 1;
day = 1;
}
} else {
day = day + 1;
}
}
return list;
}
public static boolean isWeekEnd(LocalDate localDate, boolean saturday, boolean sunday) {
String dayOfWeek = localDate.getDayOfWeek().toString();
boolean isWeekend = false;
if (saturday) {
if ("SATURDAY".equalsIgnoreCase(dayOfWeek)) {
isWeekend = true;
}
}
if (sunday) {
if ("SUNDAY".equalsIgnoreCase(dayOfWeek)) {
isWeekend = true;
}
}
return isWeekend;
}
public boolean isCompanyHoliday(String formattedDate, List<String> holidays) {
if (holidays.contains(formattedDate)) {
return true;
} else {
return false;
}
}
public boolean isLeapYear(int year) {
boolean isLeapYear = false;
if (year % 400 == 0) {
isLeapYear = true;
} else if (year % 100 == 0) {
isLeapYear = false;
} else if (year % 4 == 0) {
isLeapYear = true;
} else {
isLeapYear = false;
}
return isLeapYear;
}
}
The provided code appears to be a Java implementation of a LeavesService
interface. It contains a method findLeavesDate
that takes a Leaves
object as a parameter and returns a list of strings representing the dates for leaves.
Here’s a breakdown of the code:
- The method
findLeavesDate
initializes an empty list and extracts the start date from theLeaves
object. - The start date is then split into day, month, and year components.
- The month value is converted into a
Month
enum instance. - loop should run until the desired number of leaves (
noOfLeaves
) is reached. - Inside the loop, it checks if the current date is a weekend using the
isWeekEnd
method, considering theisSaturdayHoliday
andisSundayHoliday
flags from theLeaves
object. - If the current date is not a weekend, it checks if it is a company holiday using the
isCompanyHoliday
method and thecompanyHoliday
list from theLeaves
object. - If the date is neither a weekend nor a company holiday, it is added to the list.
- The loop continues until the desired number of leaves (
noOfLeaves
) is reached. - If the current day reaches the last day of the month, the code handles the transition to the next month or year accordingly.
- Finally, the method returns the list of leaves.
The code also includes helper methods:
isWeekEnd
takes aLocalDate
object and the flags for Saturday and Sunday holidays, and determines if the given date falls on a weekend.isCompanyHoliday
checks if the given formatted date exists in the list of company holidays.isLeapYear
determines if the given year is a leap year based on the conditions specified.
Note: It’s important to consider any dependencies or missing parts of the code that are not provided, such as the Leaves
class and the LeavesService
interface, to fully understand the context and functionality of this code.
You can run above code here.
Thank you for sharing this valuable information. It’s incredibly helpful! Watch Sport Movies Online
Hey there, You’ve done an incredible job. I will definitrly digg it and personally suggest to my friends.
I’m sure they’ll be benefited from this web site.
Also visit my website https://Supportvavada.Populiser.com/
I enjoyed reading this. It’s clear and well-written.