ITU-3 Kontaktbuch
Ein vollständiges C# Adressbuch mit Live-PHP-Integration
C# Quellcode
Vollständiger C# Code mit UML-konformer Implementierung. Scrollbarer Code-Viewer mit Syntax-Highlighting und Navigation.
Live Terminal
Interaktives Terminal zur direkten Ausführung von C# Befehlen. Echte Programmausführung auf dem Server.
UML Architektur
Detaillierte Erklärung der Code-Architektur mit UML-Klassendiagramm und Implementierungsdetails.
Live Verwaltung
Moderne Web-Oberfläche zur Kontaktverwaltung mit direkter C# Backend-Integration und Echtzeit-Updates.
C# Adressbuch Quellcode
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.IO;
public class Contact
{
public string Name { get; set; } = "";
public string Email { get; set; } = "";
public string Phone { get; set; } = "";
public string Twitter { get; set; } = "";
public int Id { get; set; } // Hinzugefügt: 'set' ist wichtig für JSON Deserialisierung
public Contact(string name, string email, string phone, string twitter, int id)
{
Name = name;
Email = email;
Phone = phone;
Twitter = twitter;
Id = id;
}
public Contact() { }
}
public class Addressbook
{
private string name;
private List<Contact> contacts;
private int nextId;
private string dataFilePath;
public Addressbook(string name)
{
this.name = name;
dataFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "contacts.json");
LoadContactsFromFile();
}
private List<Contact> GetDefaultContacts()
{
int currentDefaultId = 1;
return new List<Contact>
{
new Contact("Ken Tern", "ken.tern@mail.de", "+49 221 3982781", "@kentern", currentDefaultId++),
new Contact("Bill Iger", "bill.iger@gmx.de", "+49 211 9821348", "@billiger", currentDefaultId++),
new Contact("Flo Kati", "flo.kati@web.de", "+49 251 9346441", "@flokati", currentDefaultId++),
new Contact("Ingeborg Mirwas", "inge.mirwas@post.de", "+49 228 4663289", "@borgmirwas", currentDefaultId++),
new Contact("Ann Schweigen", "ann.schweigen@gmx.de", "+49 231 6740921", "@annschweigen", currentDefaultId++),
new Contact("Mark Enschuh", "mark.enschuh@gmail.com", "+49 234 4565657", "@markenschuh", currentDefaultId++)
};
}
private void LoadContactsFromFile()
{
if (File.Exists(dataFilePath))
{
try
{
string jsonString = File.ReadAllText(dataFilePath);
contacts = JsonSerializer.Deserialize<List<Contact>>(jsonString) ?? new List<Contact>();
nextId = contacts.Count > 0 ? contacts.Max(c => c.Id) + 1 : 1;
Console.Error.WriteLine($"[DEBUG] Loaded {contacts.Count} contacts from {dataFilePath}. Next ID: {nextId}");
}
catch (Exception ex)
{
Console.Error.WriteLine($"[ERROR] Failed to load contacts from file: {ex.Message}");
contacts = GetDefaultContacts();
nextId = contacts.Count > 0 ? contacts.Max(c => c.Id) + 1 : 1;
SaveContactsToFile();
}
}
else
{
Console.Error.WriteLine($"[DEBUG] Data file not found at {dataFilePath}. Loading default contacts.");
contacts = GetDefaultContacts();
nextId = contacts.Count > 0 ? contacts.Max(c => c.Id) + 1 : 1;
SaveContactsToFile();
}
}
private void SaveContactsToFile()
{
try
{
string jsonString = JsonSerializer.Serialize(contacts, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(dataFilePath, jsonString);
Console.Error.WriteLine($"[DEBUG] Saved {contacts.Count} contacts to {dataFilePath}");
}
catch (Exception ex)
{
Console.Error.WriteLine($"[ERROR] Failed to save contacts to file: {ex.Message}");
}
}
public void ResetToDefault()
{
contacts.Clear();
contacts = GetDefaultContacts();
nextId = contacts.Count > 0 ? contacts.Max(c => c.Id) + 1 : 1;
SaveContactsToFile();
Console.WriteLine("OK: Adressbuch auf Standardkontakte zurückgesetzt.");
}
public string ShowAll()
{
if (contacts.Count == 0)
{
return "Keine Kontakte vorhanden.";
}
string result = $"\n=== ADRESSBUCH: {name.ToUpper()} ===\n";
result += "ID | Name | Email | Phone | Twitter\n";
result += "---|-------------------|--------------------------|----------------|------------------\n";
foreach (var contact in contacts)
{
result += $"{contact.Id,2} | {contact.Name,-17} | {contact.Email,-24} | {contact.Phone,-14} | {contact.Twitter}\n";
}
return result;
}
public string ShowByName(string name)
{
var results = contacts.Where(c =>
c.Name.Contains(name, StringComparison.OrdinalIgnoreCase)
).ToList();
if (results.Count == 0)
{
return $"Kein Kontakt mit Namen '{name}' gefunden.";
}
string result = $"\n=== SUCHERGEBNISSE FÜR '{name}' ===\n";
result += "ID | Name | Email | Phone | Twitter\n";
result += "---|-------------------|--------------------------|----------------|------------------\n";
foreach (var contact in results)
{
result += $"{contact.Id,2} | {contact.Name,-17} | {contact.Email,-24} | {contact.Phone,-14} | {contact.Twitter}\n";
}
return result;
}
public void AddContact(Contact contact)
{
if (contact.Id == 0)
{
contact.Id = nextId++;
}
else
{
if (contact.Id >= nextId)
{
nextId = contact.Id + 1;
}
}
if (contacts.Any(c => c.Id == contact.Id))
{
Console.WriteLine($"ERROR: Kontakt mit ID {contact.Id} existiert bereits. Kontakt '{contact.Name}' nicht hinzugefügt.");
return;
}
contacts.Add(contact);
SaveContactsToFile();
Console.WriteLine($"OK: Kontakt '{contact.Name}' (ID: {contact.Id}) erfolgreich hinzugefügt.");
}
public Contact? GetContact(int id)
{
return contacts.FirstOrDefault(c => c.Id == id);
}
public bool RemoveContact(int id)
{
var contact = contacts.FirstOrDefault(c => c.Id == id);
if (contact != null)
{
contacts.Remove(contact);
SaveContactsToFile();
Console.WriteLine($"OK: Kontakt mit ID {id} erfolgreich entfernt.");
return true;
}
Console.WriteLine($"ERROR: Kontakt mit ID {id} nicht gefunden.");
return false;
}
public string GetContactsAsJson()
{
if (contacts.Any()) {
nextId = Math.Max(nextId, contacts.Max(c => c.Id) + 1);
} else {
nextId = 1;
}
return JsonSerializer.Serialize(contacts, new JsonSerializerOptions
{
WriteIndented = true
});
}
public int GetContactCount()
{
return contacts.Count;
}
}
class Program
{
static void Main(string[] args)
{
var addressbook = new Addressbook("Privat");
if (args.Length > 0)
{
switch (args[0].ToLower())
{
case "showall":
Console.WriteLine(addressbook.ShowAll());
break;
case "showbyname":
if (args.Length > 1)
Console.WriteLine(addressbook.ShowByName(args[1]));
else
Console.WriteLine("ERROR: Name fehlt. Verwendung: showbyname <Name>");
break;
case "json":
Console.WriteLine(addressbook.GetContactsAsJson());
break;
case "add":
// Argumente zusammenfügen
if (args.Length >= 5)
{
// Beachte: C# empfängt hier die Argumente bereits getrennt durch Leerzeichen,
// wenn PHP sie mit escapeshellarg in Anführungszeichen setzt.
// args[1] ist der Name (kann Leerzeichen enthalten, wenn PHP korrekt escapt hat)
var contact = new Contact(args[1], args[2], args[3], args[4], 0);
addressbook.AddContact(contact);
}
else
{
Console.WriteLine("ERROR: Verwendung: add <Name> <Email> <Phone> <Twitter>");
}
break;
case "remove":
if (args.Length > 1 && int.TryParse(args[1], out int id))
{
addressbook.RemoveContact(id);
}
else
{
Console.WriteLine("ERROR: Verwendung: remove <ID>");
}
break;
case "count":
Console.WriteLine($"OK: Anzahl Kontakte: {addressbook.GetContactCount()}");
break;
case "reset":
addressbook.ResetToDefault();
break;
default:
Console.WriteLine("ERROR: Unbekannter Befehl. Verfügbare Befehle: showall, showbyname <name>, add <name> <email> <phone> <twitter>, remove <id>, json, count, reset");
break;
}
}
else
{
Console.WriteLine(addressbook.ShowAll());
}
}
}
Live Terminal
UML-Klassendiagramm & Architektur
+showAll(): string
+showByName(name: string): string
+addContact(entry: Contact)
+getContact(id: integer): Contact
+removeContact(id: integer)
+resetToDefault(): void
-email: string
-phone: string
-twitter: string
-id: integer
Implementierung
Addressbook-Klasse: Verwaltet eine Sammlung von Kontakten und bietet Methoden zum Anzeigen, Suchen, Hinzufügen und Entfernen. Die Kontakte werden persistent in einer JSON-Datei gespeichert.
Contact-Klasse: Repräsentiert einen einzelnen Kontakt mit allen erforderlichen Eigenschaften.
Referenzdatentypen: Die Klassen verwenden Strings und Collections als Referenzdatentypen.
Datenpersistenz: Kontakte werden in contacts.json gespeichert und geladen, um Datenänderungen zu erhalten.
Funktionalitäten
- showAll(): Zeigt alle Kontakte in Tabellenform
- showByName(): Sucht Kontakte nach Namen
- addContact(): Fügt neue Kontakte hinzu
- removeContact(): Entfernt Kontakte anhand der ID
- getContact(): Ruft einzelne Kontakte ab
- resetToDefault(): Setzt das Adressbuch auf die initialen Standardkontakte zurück.
Design Patterns & Prinzipien
- Encapsulation: Private Felder mit öffentlichen Methoden
- Single Responsibility: Jede Klasse hat eine klare Aufgabe
- Data Transfer Object: Contact als einfaches Datenobjekt
- Repository Pattern: Addressbook als Datenverwalter
- Separation of Concerns: C# für Logik, PHP für UI, JSON für Persistenz.
Live Adressbuch Verwaltung
Neuen Kontakt hinzufügen
Aktionen
Kontakte (Live aus C#)
Kontakte werden geladen...