Fertigstellung
This commit is contained in:
parent
2150bad16d
commit
f11be713d0
Binary file not shown.
@ -22,11 +22,11 @@
|
||||
"Title": "Program.cs",
|
||||
"DocumentMoniker": "C:\\Users\\bib\\Documents\\PMC_Projekt\\PMCProjekt\\PMCProjekt\\Program.cs",
|
||||
"RelativeDocumentMoniker": "PMCProjekt\\Program.cs",
|
||||
"ToolTip": "C:\\Users\\bib\\Documents\\PMC_Projekt\\PMCProjekt\\PMCProjekt\\Program.cs*",
|
||||
"RelativeToolTip": "PMCProjekt\\Program.cs*",
|
||||
"ViewState": "AQIAAHMAAAAAAAAAAAAkwHoAAAAAAAAA",
|
||||
"ToolTip": "C:\\Users\\bib\\Documents\\PMC_Projekt\\PMCProjekt\\PMCProjekt\\Program.cs",
|
||||
"RelativeToolTip": "PMCProjekt\\Program.cs",
|
||||
"ViewState": "AQIAAAMAAAAAAAAAAAAAAE0AAAAJAAAA",
|
||||
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
|
||||
"WhenOpened": "2024-06-14T09:14:59.548Z",
|
||||
"WhenOpened": "2024-08-30T09:51:48.989Z",
|
||||
"EditorCaption": ""
|
||||
}
|
||||
]
|
||||
|
@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Collections.Generic;
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
namespace PMCProjekt
|
||||
{
|
||||
@ -8,12 +10,28 @@ namespace PMCProjekt
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// Kundennummer eingeben
|
||||
Console.Write("Bitte geben Sie die Kundennummer ein (z.B. K2-002): ");
|
||||
string kundennummer = Console.ReadLine();
|
||||
string dateiPfad = "/Schule/PMC/import_yutani.csv";
|
||||
string protokollPfad = "/Schule/PMC/ungueltige_Daten.csv";
|
||||
|
||||
// Datei einlesen
|
||||
string dateiPfad = "C:/Schule/PMC/import_yutani.csv";
|
||||
// MySQL-Verbindungszeichenfolge
|
||||
string connectionString = "server=localhost;uid=root;pwd=root;database=import_export";
|
||||
|
||||
// Abfrage der Kundennummer vom Benutzer
|
||||
Console.Write("Bitte geben Sie die Kundennummer (clientno) ein: ");
|
||||
string clientno = Console.ReadLine();
|
||||
|
||||
int clientId = HoleClientId(connectionString, clientno);
|
||||
|
||||
if (clientId == 0)
|
||||
{
|
||||
Console.WriteLine("Ungültige Kundennummer. Programm wird beendet.");
|
||||
return;
|
||||
}
|
||||
|
||||
List<List<string>> alleDatensaetze = new List<List<string>>();
|
||||
List<string> aktuellerDatensatz = new List<string>();
|
||||
|
||||
// Überprüfen, ob die Datei existiert
|
||||
if (File.Exists(dateiPfad))
|
||||
{
|
||||
using (StreamReader sr = new StreamReader(dateiPfad))
|
||||
@ -21,37 +39,35 @@ namespace PMCProjekt
|
||||
string zeile;
|
||||
while ((zeile = sr.ReadLine()) != null)
|
||||
{
|
||||
// Werte aus der Zeile extrahieren
|
||||
string[] werte = zeile.Split(',');
|
||||
if (werte.Length >= 4)
|
||||
if (zeile.StartsWith("Frau") || zeile.StartsWith("Herr") || zeile.StartsWith("Divers"))
|
||||
{
|
||||
string anrede = werte[0];
|
||||
string vorname = werte[1];
|
||||
string nachname = werte[2];
|
||||
string geburtsdatum = werte[3];
|
||||
string adresse = werte.Length > 4 ? werte[4] : null;
|
||||
string plz = werte.Length > 5 ? werte[5] : null;
|
||||
string stadt = werte.Length > 6 ? werte[6] : null;
|
||||
string email = werte.Length > 7 ? werte[7] : null;
|
||||
string telefonnummern = werte.Length > 8 ? werte[8] : null;
|
||||
// Wenn ein neuer Datensatz beginnt, speichere den vorherigen
|
||||
if (aktuellerDatensatz.Count > 0)
|
||||
{
|
||||
alleDatensaetze.Add(new List<string>(aktuellerDatensatz));
|
||||
aktuellerDatensatz.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
// Daten validieren und ungültige Daten protokollieren
|
||||
if (!DatenValidieren(anrede, vorname, nachname, geburtsdatum, adresse, plz, stadt, email, telefonnummern))
|
||||
// Füge die aktuelle Zeile dem Datensatz hinzu
|
||||
aktuellerDatensatz.Add(zeile);
|
||||
}
|
||||
|
||||
// Füge den letzten Datensatz hinzu, falls vorhanden
|
||||
if (aktuellerDatensatz.Count > 0)
|
||||
{
|
||||
ProtokolliereUngueltigeDaten(werte);
|
||||
Console.WriteLine("Ungültige Daten: " + string.Join(",", werte));
|
||||
alleDatensaetze.Add(aktuellerDatensatz);
|
||||
}
|
||||
else
|
||||
}
|
||||
|
||||
// Verarbeitung aller gesammelten Datensätze
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
Console.WriteLine("Gültige Daten: " + string.Join(",", werte));
|
||||
}
|
||||
}
|
||||
else
|
||||
connection.Open();
|
||||
|
||||
foreach (var datensatz in alleDatensaetze)
|
||||
{
|
||||
// Protokolliere Zeilen mit zu wenigen Datenfeldern
|
||||
ProtokolliereUngueltigeDaten(werte);
|
||||
Console.WriteLine("Ungültige Daten (zu wenige Felder): " + string.Join(",", werte));
|
||||
}
|
||||
VerarbeiteDatensatz(datensatz, protokollPfad, connection, clientId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -61,79 +77,227 @@ namespace PMCProjekt
|
||||
}
|
||||
}
|
||||
|
||||
static bool DatenValidieren(string anrede, string vorname, string nachname, string geburtsdatum, string adresse, string plz, string stadt, string email, string telefonnummern)
|
||||
static int HoleClientId(string connectionString, string clientno)
|
||||
{
|
||||
bool istGueltig = true;
|
||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
string query = "SELECT id FROM client WHERE clientno = @ClientNo";
|
||||
using (MySqlCommand command = new MySqlCommand(query, connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@ClientNo", clientno);
|
||||
var result = command.ExecuteScalar();
|
||||
return result != null ? Convert.ToInt32(result) : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Geburtsdatum validieren
|
||||
if (!PrüfGeburtstag(geburtsdatum))
|
||||
static void VerarbeiteDatensatz(List<string> datensatz, string protokollPfad, MySqlConnection connection, int clientId)
|
||||
{
|
||||
string gesamteZeile = string.Join(";", datensatz);
|
||||
string[] werte = gesamteZeile.Split(';');
|
||||
|
||||
string anrede = "";
|
||||
string vorname = "";
|
||||
string nachname = "";
|
||||
string geburtsdatum = "";
|
||||
string strasse = "";
|
||||
string plz = "";
|
||||
string stadt = "";
|
||||
string email = "";
|
||||
List<string> telefonnummern = new List<string>();
|
||||
|
||||
for (int i = 0; i < werte.Length; i++)
|
||||
{
|
||||
if (werte[i] == "Frau" || werte[i] == "Herr" || werte[i] == "Divers")
|
||||
{
|
||||
anrede = werte[i];
|
||||
vorname = werte[i + 1];
|
||||
nachname = werte[i + 2];
|
||||
geburtsdatum = werte[i + 3];
|
||||
i += 3;
|
||||
}
|
||||
else if (werte[i] == "Adresse")
|
||||
{
|
||||
strasse = werte[i + 1];
|
||||
plz = werte[i + 2];
|
||||
stadt = werte[i + 3];
|
||||
i += 3;
|
||||
}
|
||||
else if (werte[i] == "E-Mail")
|
||||
{
|
||||
email = werte[i + 1];
|
||||
i += 1;
|
||||
}
|
||||
else if (werte[i] == "Telefon")
|
||||
{
|
||||
telefonnummern.Add($"{werte[i + 1]} {werte[i + 2]}");
|
||||
i += 2;
|
||||
}
|
||||
}
|
||||
|
||||
bool gueltig = true;
|
||||
|
||||
if (!PruefeGeburtstag(geburtsdatum))
|
||||
{
|
||||
Console.WriteLine($"Ungültiges Geburtsdatum: {geburtsdatum}");
|
||||
istGueltig = false;
|
||||
gueltig = false;
|
||||
}
|
||||
|
||||
// E-Mail-Adresse validieren
|
||||
if (!string.IsNullOrEmpty(email) && !PrüfEmail(email))
|
||||
if (!string.IsNullOrEmpty(email) && !PruefeEmail(email))
|
||||
{
|
||||
Console.WriteLine($"Ungültige E-Mail-Adresse: {email}");
|
||||
istGueltig = false;
|
||||
gueltig = false;
|
||||
}
|
||||
|
||||
// Postleitzahl validieren
|
||||
if (!string.IsNullOrEmpty(plz) && !PrüfPLZ(plz))
|
||||
if (!string.IsNullOrEmpty(plz) && !PruefePLZ(plz))
|
||||
{
|
||||
Console.WriteLine($"Ungültige Postleitzahl: {plz}");
|
||||
istGueltig = false;
|
||||
gueltig = false;
|
||||
}
|
||||
|
||||
// Telefonnummern validieren
|
||||
if (!string.IsNullOrEmpty(telefonnummern))
|
||||
foreach (var telefon in telefonnummern)
|
||||
{
|
||||
string[] telefonNummern = telefonnummern.Split(';');
|
||||
foreach (var telefon in telefonNummern)
|
||||
{
|
||||
if (!PrüfTelefon(telefon))
|
||||
if (!PruefeTelefon(telefon))
|
||||
{
|
||||
Console.WriteLine($"Ungültige Telefonnummer: {telefon}");
|
||||
istGueltig = false;
|
||||
}
|
||||
gueltig = false;
|
||||
}
|
||||
}
|
||||
|
||||
return istGueltig;
|
||||
}
|
||||
|
||||
static bool PrüfGeburtstag(string geburtsdatum)
|
||||
if (gueltig)
|
||||
{
|
||||
string pattern = @"^\d{4}\-\d{2}\-\d{2}$";
|
||||
int userId = GeneriereNaechsteId("user", connection);
|
||||
EinfuegenInUserTabelle(userId, anrede, vorname, nachname, geburtsdatum, connection, clientId);
|
||||
|
||||
EinfuegenInAddressTabelle(GeneriereNaechsteId("address", connection), userId, strasse, plz, stadt, connection);
|
||||
EinfuegenInEmailTabelle(GeneriereNaechsteId("email", connection), userId, email, connection);
|
||||
foreach (var telefon in telefonnummern)
|
||||
{
|
||||
string[] telefonTeile = telefon.Split(' ');
|
||||
EinfuegenInPhoneTabelle(GeneriereNaechsteId("phone", connection), userId, telefonTeile[0], telefonTeile[1], connection);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ProtokolliereUngueltigeDaten(datensatz, protokollPfad);
|
||||
}
|
||||
}
|
||||
|
||||
static int GeneriereNaechsteId(string tabelle, MySqlConnection connection)
|
||||
{
|
||||
string query = $"SELECT IFNULL(MAX(id), 0) + 1 FROM {tabelle}";
|
||||
using (MySqlCommand command = new MySqlCommand(query, connection))
|
||||
{
|
||||
return Convert.ToInt32(command.ExecuteScalar());
|
||||
}
|
||||
}
|
||||
|
||||
static void EinfuegenInUserTabelle(int userId, string anrede, string vorname, string nachname, string geburtsdatum, MySqlConnection connection, int clientId)
|
||||
{
|
||||
string query = "INSERT INTO user (id, clientId, genderId, firstname, lastname, birthdate, created) " +
|
||||
"VALUES (@Id, @ClientId, @GenderId, @Vorname, @Nachname, @Geburtsdatum, @Created)";
|
||||
using (MySqlCommand command = new MySqlCommand(query, connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@Id", userId);
|
||||
command.Parameters.AddWithValue("@ClientId", clientId);
|
||||
command.Parameters.AddWithValue("@GenderId", GetGenderId(anrede, connection));
|
||||
command.Parameters.AddWithValue("@Vorname", vorname);
|
||||
command.Parameters.AddWithValue("@Nachname", nachname);
|
||||
command.Parameters.AddWithValue("@Geburtsdatum", DateTime.Parse(geburtsdatum));
|
||||
command.Parameters.AddWithValue("@Created", DateTime.Now);
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
static int GetGenderId(string anrede, MySqlConnection connection)
|
||||
{
|
||||
string query = "SELECT id FROM gender WHERE description = @Anrede";
|
||||
using (MySqlCommand command = new MySqlCommand(query, connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@Anrede", anrede);
|
||||
return Convert.ToInt32(command.ExecuteScalar());
|
||||
}
|
||||
}
|
||||
|
||||
static void EinfuegenInAddressTabelle(int addressId, int userId, string strasse, string plz, string stadt, MySqlConnection connection)
|
||||
{
|
||||
string query = "INSERT INTO address (id, userId, street, postalcode, city) " +
|
||||
"VALUES (@Id, @UserId, @Strasse, @PLZ, @Stadt)";
|
||||
using (MySqlCommand command = new MySqlCommand(query, connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@Id", addressId);
|
||||
command.Parameters.AddWithValue("@UserId", userId);
|
||||
command.Parameters.AddWithValue("@Strasse", strasse);
|
||||
command.Parameters.AddWithValue("@PLZ", plz);
|
||||
command.Parameters.AddWithValue("@Stadt", stadt);
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
static void EinfuegenInEmailTabelle(int emailId, int userId, string email, MySqlConnection connection)
|
||||
{
|
||||
string query = "INSERT INTO email (id, userId, email) VALUES (@Id, @UserId, @Email)";
|
||||
using (MySqlCommand command = new MySqlCommand(query, connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@Id", emailId);
|
||||
command.Parameters.AddWithValue("@UserId", userId);
|
||||
command.Parameters.AddWithValue("@Email", email);
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
static void EinfuegenInPhoneTabelle(int phoneId, int userId, string phoneprefix, string phonenumber, MySqlConnection connection)
|
||||
{
|
||||
string query = "INSERT INTO phone (id, userId, phoneprefix, phonenumber) VALUES (@Id, @UserId, @PhonePrefix, @PhoneNumber)";
|
||||
using (MySqlCommand command = new MySqlCommand(query, connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@Id", phoneId);
|
||||
command.Parameters.AddWithValue("@UserId", userId);
|
||||
command.Parameters.AddWithValue("@PhonePrefix", phoneprefix);
|
||||
command.Parameters.AddWithValue("@PhoneNumber", phonenumber);
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
static bool PruefeGeburtstag(string geburtsdatum)
|
||||
{
|
||||
string pattern = @"^\d{2}\.\d{2}\.\d{4}$";
|
||||
return Regex.IsMatch(geburtsdatum, pattern);
|
||||
}
|
||||
|
||||
static bool PrüfEmail(string email)
|
||||
static bool PruefeEmail(string email)
|
||||
{
|
||||
string pattern = @"^[a-z]+\.[a-z]+@[a-z]+\.[a-z]{2,}$";
|
||||
string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
|
||||
return Regex.IsMatch(email, pattern);
|
||||
}
|
||||
|
||||
static bool PrüfPLZ(string plz)
|
||||
static bool PruefePLZ(string plz)
|
||||
{
|
||||
string pattern = @"^[0-9]{5}$";
|
||||
string pattern = @"^\d{5}$";
|
||||
return Regex.IsMatch(plz, pattern);
|
||||
}
|
||||
|
||||
static bool PrüfTelefon(string telefon)
|
||||
static bool PruefeTelefon(string telefon)
|
||||
{
|
||||
string pattern = @"^[0-9]{3,5}\s?[0-9]{4,10}$";
|
||||
string pattern = @"^\d{3,5}\s?\d{4,10}$";
|
||||
return Regex.IsMatch(telefon, pattern);
|
||||
}
|
||||
|
||||
static void ProtokolliereUngueltigeDaten(string[] daten)
|
||||
static void ProtokolliereUngueltigeDaten(List<string> daten, string protokollPfad)
|
||||
{
|
||||
string protokollPfad = "C:/Schule/PMC/ungueltige_Daten.csv";
|
||||
using (StreamWriter sw = new StreamWriter(protokollPfad, true))
|
||||
{
|
||||
sw.WriteLine(string.Join(",", daten));
|
||||
foreach (var zeile in daten)
|
||||
{
|
||||
sw.WriteLine(zeile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("PMCProjekt")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+3ecbf97c766628394acad938d340da68a9d05e5c")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+2150bad16d353de9bebea8f2feae3039c472b9e6")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("PMCProjekt")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("PMCProjekt")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
@ -1 +1 @@
|
||||
d4ca53237659e1a7007941b655e24571191b87dfdcf8a3a5ea0ebb2f5de16263
|
||||
b181418c207b62c0f6b18dd50a2d5203ab8bd79309967fd8ba3cf377b6e89772
|
||||
|
Binary file not shown.
@ -66,7 +66,7 @@
|
||||
"privateAssets": "all"
|
||||
}
|
||||
},
|
||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.204/PortableRuntimeIdentifierGraph.json"
|
||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.304/PortableRuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\bib\.nuget\packages\</NuGetPackageFolders>
|
||||
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.9.2</NuGetToolVersion>
|
||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.10.2</NuGetToolVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<SourceRoot Include="C:\Users\bib\.nuget\packages\" />
|
||||
|
@ -1850,7 +1850,7 @@
|
||||
"privateAssets": "all"
|
||||
}
|
||||
},
|
||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.204/PortableRuntimeIdentifierGraph.json"
|
||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.304/PortableRuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"version": 2,
|
||||
"dgSpecHash": "Jmj3WRDtSrMYQNSWXK2x6TEKWOlCmkjM6nbeQ2iYrfCgkzMw4GiPixfUmBUGiPiTU/KE5IO8pzSrja1hWLPpDA==",
|
||||
"dgSpecHash": "/54hNmRIYgQ=",
|
||||
"success": true,
|
||||
"projectFilePath": "C:\\Users\\bib\\Documents\\PMC_Projekt\\PMCProjekt\\PMCProjekt\\PMCProjekt.csproj",
|
||||
"expectedPackageFiles": [
|
||||
|
Loading…
Reference in New Issue
Block a user