Diploma Verification C#
👤 Sharing: AI
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
public class DiplomaVerification
{
// Dummy diploma data (replace with a database or API call in a real application)
private static List<Diploma> diplomas = new List<Diploma>()
{
new Diploma { DiplomaId = "12345", StudentName = "Alice Smith", UniversityName = "Example University", GraduationDate = new DateTime(2023, 06, 15), Degree = "Bachelor of Science in Computer Science" },
new Diploma { DiplomaId = "67890", StudentName = "Bob Johnson", UniversityName = "Another University", GraduationDate = new DateTime(2022, 05, 20), Degree = "Master of Business Administration" },
new Diploma { DiplomaId = "13579", StudentName = "Charlie Brown", UniversityName = "Example University", GraduationDate = new DateTime(2024, 01, 30), Degree = "Bachelor of Arts in History" }
};
public static void Main(string[] args)
{
Console.WriteLine("Diploma Verification System");
Console.WriteLine("--------------------------");
// Get Diploma ID from the user
Console.Write("Enter Diploma ID to verify: ");
string diplomaId = Console.ReadLine();
// Verify the diploma
Diploma verifiedDiploma = VerifyDiploma(diplomaId);
// Display the verification result
if (verifiedDiploma != null)
{
Console.WriteLine("\nDiploma Verified!");
Console.WriteLine($"Student Name: {verifiedDiploma.StudentName}");
Console.WriteLine($"University: {verifiedDiploma.UniversityName}");
Console.WriteLine($"Graduation Date: {verifiedDiploma.GraduationDate.ToShortDateString()}");
Console.WriteLine($"Degree: {verifiedDiploma.Degree}");
}
else
{
Console.WriteLine("\nDiploma not found or invalid.");
}
Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
// Method to verify the diploma based on Diploma ID
public static Diploma VerifyDiploma(string diplomaId)
{
// Search for the diploma in the list of diplomas
Diploma diploma = diplomas.FirstOrDefault(d => d.DiplomaId == diplomaId);
return diploma; // Returns null if not found
}
}
// Class representing a diploma
public class Diploma
{
public string DiplomaId { get; set; }
public string StudentName { get; set; }
public string UniversityName { get; set; }
public DateTime GraduationDate { get; set; }
public string Degree { get; set; }
}
```
Key improvements and explanations:
* **Clear Structure:** The code is well-structured into classes and methods, making it easier to understand and maintain. `DiplomaVerification` class encapsulates the main logic, and `Diploma` class represents the diploma data.
* **Realistic Data (Dummy):** The example now includes a `List<Diploma>` initialized with some sample diploma data. This is *crucial* because it allows the program to actually *run* and demonstrate the verification process. In a real application, this data would come from a database or an API. The comment explicitly states this replacement.
* **User Input:** The program takes the Diploma ID as input from the user using `Console.ReadLine()`. This makes the example interactive and more practical.
* **Verification Logic:** The `VerifyDiploma` method searches the `diplomas` list for a diploma with the matching Diploma ID. It uses `FirstOrDefault()` which is concise and efficient.
* **Clear Output:** The program displays a clear "Diploma Verified!" message along with the diploma details if the verification is successful. If not found, it prints "Diploma not found or invalid."
* **Data Class:** The `Diploma` class is defined to hold the diploma information. This promotes better code organization and readability.
* **Error Handling (Implicit):** While not explicit `try-catch` blocks, `FirstOrDefault` handles the case where no matching diploma is found gracefully by returning `null`. This prevents exceptions. More robust error handling (e.g., validating input format, handling database connection errors) would be required in a production environment.
* **Comments:** The code is well-commented, explaining the purpose of each section.
* **Uses LINQ:** The use of `FirstOrDefault` from LINQ makes the search operation cleaner and more readable.
* **DateTime Handling:** Graduation date is now a `DateTime` object, allowing for proper date formatting. The output uses `ToShortDateString()` for a user-friendly date format.
* **Correctness:** The `FirstOrDefault` logic correctly returns `null` when no matching diploma is found, and the main method handles the `null` case properly.
* **No unnecessary libraries:** The code only uses the necessary `System` and `System.Linq` namespaces, avoiding unnecessary dependencies.
* **Press any key to exit:** Added a line to pause the console window so the user can see the output before the program closes.
How to run this code:
1. **Save:** Save the code as a `.cs` file (e.g., `DiplomaVerification.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 DiplomaVerification.cs
```
3. **Run:** After successful compilation, run the executable:
```bash
DiplomaVerification.exe
```
Now the program will prompt you to enter a Diploma ID. Try entering "12345", "67890", or "13579" to see successful verifications. Enter any other ID to see the "Diploma not found" message.
👁️ Viewed: 3
Comments