-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSalaryCalculator.java
More file actions
81 lines (72 loc) · 3.75 KB
/
SalaryCalculator.java
File metadata and controls
81 lines (72 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class SalaryCalculator extends SalaryMaths implements Calculations {
public static void generateSalarySummary(String empID, SalaryMaths salaryMaths, double monthlySalary) throws IOException {
// Ensure the directory exists
File directory = new File(FileFunctions.SALARY_DATA_DIR);
if (!directory.exists()) {
directory.mkdirs();
}
// Create the HTML file for the employee
File f1 = new File(directory, "employee" + empID + ".html");
if (f1.createNewFile()) {
try (FileWriter htmlContent = new FileWriter(f1)) {
htmlContent.write("<!DOCTYPE html>");
htmlContent.write("<html>");
htmlContent.write("<head>");
htmlContent.write("<title>Yearly Salary Summary</title>");
htmlContent.write("<style>");
htmlContent.write("table { width: 70%; border-collapse: collapse; margin: 20px auto; }");
htmlContent.write("th, td { border: 1px solid #ddd; padding: 8px; text-align: center; }");
htmlContent.write("th { background-color: #f4f4f4; }");
htmlContent.write("</style>");
htmlContent.write("</head>");
htmlContent.write("<body>");
htmlContent.write("<h1 style='text-align: center;'>Yearly Salary Summary</h1>");
htmlContent.write("<table>");
htmlContent.write("<tr><th>Month</th><th>Days in Month</th><th>Daily Salary (LKR)</th><th>Monthly Salary (LKR)</th></tr>");
int totalDays = 0;
double totalSalary = 0;
for (int month = 1; month <= 12; month++) {
int daysInMonth = salaryMaths.daysInMonth(month);
double dailySalary = monthlySalary / daysInMonth;
totalDays += daysInMonth;
totalSalary += monthlySalary;
htmlContent.write("<tr>");
htmlContent.write("<td>");
htmlContent.write(getMonthName(month));
htmlContent.write("</td>");
htmlContent.write("<td>");
htmlContent.write(String.valueOf(daysInMonth));
htmlContent.write("</td>");
htmlContent.write("<td>");
htmlContent.write(String.format("LKR %.2f", dailySalary));
htmlContent.write("</td>");
htmlContent.write("<td>");
htmlContent.write(String.format("LKR %.2f", monthlySalary));
htmlContent.write("</td>");
htmlContent.write("</tr>");
}
htmlContent.write("<tr><th>Total</th><th>");
htmlContent.write(String.valueOf(totalDays));
htmlContent.write("</th><th>-</th><th>");
htmlContent.write(String.format("LKR %.2f", totalSalary));
htmlContent.write("</th></tr>");
htmlContent.write("</table>");
htmlContent.write("</body>");
htmlContent.write("</html>");
}
System.out.println("Yearly salary summary generated successfully in 'employee" + empID + ".html'.");
} else {
System.out.println("File 'employee" + empID + ".html' already exists.");
}
}
private static String getMonthName(int month) {
String[] months = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
return months[month - 1];
}
}