Compliance Report Generator C#
👤 Sharing: AI
```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ComplianceReportGenerator
{
class Program
{
static void Main(string[] args)
{
// 1. Define the Data Structures
// Represent data that needs to be checked for compliance. This is a simplified example.
List<Employee> employees = new List<Employee>()
{
new Employee { Id = 1, Name = "Alice Smith", Department = "HR", IsCertified = true, TrainingDate = DateTime.Now.AddMonths(-2) },
new Employee { Id = 2, Name = "Bob Johnson", Department = "IT", IsCertified = false, TrainingDate = DateTime.Now.AddYears(-1) }, //Not Certified
new Employee { Id = 3, Name = "Charlie Brown", Department = "Finance", IsCertified = true, TrainingDate = DateTime.Now.AddYears(-2) }, //Expired training
new Employee { Id = 4, Name = "Diana Lee", Department = "Marketing", IsCertified = true, TrainingDate = DateTime.Now.AddMonths(-1) },
new Employee { Id = 5, Name = "Eve Williams", Department = "IT", IsCertified = false, TrainingDate = DateTime.MinValue } //Not certified, no training date
};
// 2. Define Compliance Rules (Hardcoded for this example, but could be read from config)
int certificationExpirationMonths = 12; // Certification expires after 12 months.
bool isCertificationRequired = true; // Certification is required.
// 3. Generate Compliance Report
string report = GenerateComplianceReport(employees, certificationExpirationMonths, isCertificationRequired);
// 4. Output the Report to a File (and console)
string filePath = "compliance_report.txt";
File.WriteAllText(filePath, report);
Console.WriteLine("Compliance Report Generated:");
Console.WriteLine(report);
Console.WriteLine($"Report saved to: {filePath}");
}
// Method to generate the compliance report
static string GenerateComplianceReport(List<Employee> employees, int certificationExpirationMonths, bool isCertificationRequired)
{
string report = "Compliance Report\n=================\n\n";
foreach (var employee in employees)
{
report += $"Employee ID: {employee.Id}\n";
report += $"Name: {employee.Name}\n";
report += $"Department: {employee.Department}\n";
bool isCompliant = true; // Assume compliant initially
// Check Certification requirement
if (isCertificationRequired)
{
if (!employee.IsCertified)
{
report += "Status: Not Certified.\n";
isCompliant = false;
}
else
{
//Check if training is expired.
if (employee.TrainingDate.AddMonths(certificationExpirationMonths) < DateTime.Now)
{
report += "Status: Certification Expired.\n";
isCompliant = false;
}
else
{
report += "Status: Certified and Compliant.\n";
}
}
}
else
{
report += "Status: No certification required, compliant\n";
isCompliant = true;
}
report += $"Compliant: {isCompliant}\n\n"; //Overall compliance status
}
return report;
}
}
// Define the Employee class
class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Department { get; set; }
public bool IsCertified { get; set; }
public DateTime TrainingDate { get; set; } //Date of last training / certification
}
}
```
Key improvements and explanations:
* **Clear Structure:** The code is organized into a `Program` class with a `Main` method. Helper functions/methods are used to improve readability.
* **Data Model (Employee Class):** An `Employee` class is defined to represent the data you're assessing for compliance. This makes the code more object-oriented and easier to extend. The class includes `IsCertified` (a boolean indicating certification status) and `TrainingDate` (the date of the last training or certification).
* **Compliance Rules as Variables:** The compliance rules (certification expiration period, certification requirement) are defined as variables. This makes them easy to change and potentially load from a configuration file in a real-world application.
* **`GenerateComplianceReport` Method:** This method encapsulates the logic for generating the compliance report string. It takes the list of employees and the compliance rules as input. This improves modularity and testability.
* **Certification Expiration Check:** The code now correctly checks if the certification has expired by comparing the training date + the expiration period with the current date. `employee.TrainingDate.AddMonths(certificationExpirationMonths) < DateTime.Now` does the core date comparison.
* **`DateTime.MinValue` Handling:** The example now includes an employee with `TrainingDate = DateTime.MinValue`. This is a common way to represent a missing or unknown date. The code doesn't explicitly handle this, but it's important to be aware of and handle such cases in a robust application (e.g., consider it as non-compliant).
* **Clearer Compliance Logic:** The `isCompliant` flag helps to track the overall compliance status of each employee, making the logic easier to follow.
* **Output to File:** The report is written to a text file, as requested. `File.WriteAllText` is the simplest way to write a string to a file.
* **Console Output:** The report is also printed to the console for immediate viewing.
* **Comments:** The code includes comments to explain the purpose of each section.
* **Error Handling (Considerations):** The provided code is a basic example and doesn't include robust error handling. In a real-world application, you would need to add error handling to deal with potential issues such as:
* File I/O exceptions (e.g., file not found, permission denied)
* Invalid data in the employee list (e.g., null values)
* Unexpected values in the configuration (if loading from a file)
* **Configuration (Considerations):** In a production environment, compliance rules should be configurable. This could involve reading from a configuration file (e.g., JSON or XML) or a database.
* **Extensibility (Considerations):** Consider how the code could be extended to handle more complex compliance rules or different types of data. Using interfaces and abstract classes can make the code more flexible and maintainable.
* **Logging (Considerations):** Implement logging to record the compliance checks and any errors that occur.
How to Run:
1. **Save:** Save the code as `ComplianceReportGenerator.cs`.
2. **Compile:** Open a command prompt or terminal and navigate to the directory where you saved the file. Then, compile the code using the C# compiler:
```bash
csc ComplianceReportGenerator.cs
```
3. **Run:** Execute the compiled program:
```bash
ComplianceReportGenerator.exe
```
This will generate a `compliance_report.txt` file in the same directory and print the report to the console.
👁️ Viewed: 4
Comments