From 1fed1c5169709b6b495d3eced14726f7debd5a22 Mon Sep 17 00:00:00 2001 From: Jens Albrecht <jens.albrecht@th-nuernberg.de> Date: Tue, 7 Jan 2025 18:19:10 +0100 Subject: [PATCH] . --- Person/Person.cs | 81 ++++++++++++++++++++++++++++++++++ Person/Person.csproj | 10 +++++ Person/Program.cs | 16 +++++++ README.md | 10 ++--- Speisekarte/Speisekarte.cs | 43 ++++++++++++++++++ Speisekarte/Speisekarte.csproj | 10 +++++ Speisekarte/Speisekarte.txt | 26 +++++------ Uebung11.sln | 31 +++++++++++++ 8 files changed, 209 insertions(+), 18 deletions(-) create mode 100644 Person/Person.cs create mode 100644 Person/Person.csproj create mode 100644 Person/Program.cs create mode 100644 Speisekarte/Speisekarte.cs create mode 100644 Speisekarte/Speisekarte.csproj create mode 100644 Uebung11.sln diff --git a/Person/Person.cs b/Person/Person.cs new file mode 100644 index 0000000..6c082ca --- /dev/null +++ b/Person/Person.cs @@ -0,0 +1,81 @@ +namespace Uebung11; + +// Hauptprogramm in Program.cs + +class Person +{ + int persnr; + string vorname; + string nachname; + string email; + + // Parameter Konstruktur + public Person(int persnr, string vorname, string nachname, string email) + { + if (vorname == null || vorname.Length < 2 || + nachname == null || nachname.Length < 2) + throw new ArgumentException("Ungültiger Name"); + else if (!EmailGueltig(email)) + throw new ArgumentException("Ungültige Email"); + + this.persnr = persnr; + this.vorname = vorname; + this.nachname = nachname; + this.email = email; + } + + #region Methoden + // Getter für Name + public string GetName() + { + return nachname + ", " + vorname; + } + + // Getter und Setter für Email + public string GetEmail() => email; // ist das gleiche wie nur return email; + + public void SetEmail(string newEmail) + { + if (!EmailGueltig(newEmail)) // Aufgabe 11.3 + throw new ArgumentException("Ungültige Email"); + this.email = newEmail; + } + + // ToString-Methode mit override + public override string ToString() + { + return $"{persnr}: {nachname}, {vorname}; {email}"; + } + + // Gültigkeitstest für Email. Achtung: static! + // Die Methode überprüft nur für einen String, ob er eine gültige Email-Adresse darstellt. + // Als Objekt-Methode geht das nicht, denn wir wollen ja gar kein Objekt haben, + // das eine ungültige Adresse hat! + public static bool EmailGueltig(string email) + { + if (email == null || email.Length == 0) + return false; + + // erstes Zeichen ist Buchstabe + if (!Char.IsLetter(email[0])) + return false; + + // Merker für @ und . danach + bool atGefunden = false; + bool pktNachAtGefunden = false; + + // Schleife über email + foreach (char c in email) + { + if (c == ' ') + return false; + else if (c == '@') + atGefunden = true; + else if (atGefunden && c == '.') + pktNachAtGefunden = true; + } + + return pktNachAtGefunden; + } + #endregion +} diff --git a/Person/Person.csproj b/Person/Person.csproj new file mode 100644 index 0000000..206b89a --- /dev/null +++ b/Person/Person.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/Person/Program.cs b/Person/Program.cs new file mode 100644 index 0000000..4d7f6f3 --- /dev/null +++ b/Person/Program.cs @@ -0,0 +1,16 @@ +namespace Uebung11; + +// Hauptprogramm mit Tests für Person und Abteilung +class Program +{ + static void Main(string[] args) + { + // In Vorlesung erweitert um Enum Geschlecht + Person person = new Person(12, "Anne", "Meier", "ameier@thn.de"); + + Console.WriteLine(person.GetName()); + Console.WriteLine(person); + person.SetEmail("ameier@th-nuernberg.de"); // Ändern der Email + Console.WriteLine(person); + } +} \ No newline at end of file diff --git a/README.md b/README.md index 6a8b94a..8ce07b7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# Übung 11 - -Hier finden Sie das [Übungsblatt](Übung11.pdf) und (nach Abschluss der letzten Übung) auch die Musterlösungen. - -Sollten die Lösungen einmal nicht rechtzeitig eingestellt werden, melden Sie sich bitte. +# Übung 11 + +Hier finden Sie das [Übungsblatt](Übung11.pdf) und (nach Abschluss der letzten Übung) auch die Musterlösungen. + +Sollten die Lösungen einmal nicht rechtzeitig eingestellt werden, melden Sie sich bitte. diff --git a/Speisekarte/Speisekarte.cs b/Speisekarte/Speisekarte.cs new file mode 100644 index 0000000..4018771 --- /dev/null +++ b/Speisekarte/Speisekarte.cs @@ -0,0 +1,43 @@ +namespace Uebung11; + +class Program +{ + static void Rechnung(string[] bestellung) + { + StreamReader sr = new StreamReader(@"..\..\..\Speisekarte.txt"); + + double summe = 0; + + while (!sr.EndOfStream) + { + string zeile = sr.ReadLine() ?? ""; + string[] felder = zeile.Split(';'); + string nummer = felder[0]; + + int anzahl = 0; + for (int i = 0; i < bestellung.Length; i++) + { + if (nummer == bestellung[i]) + anzahl++; + } + + if (anzahl > 0) + { + string gericht = felder[1]; + double preis = Convert.ToDouble(felder[2]); + + Console.WriteLine($"{anzahl}x {gericht}: {anzahl * preis:f2}"); + summe += anzahl * preis; + } + } + Console.WriteLine($"Summe: {summe:f2}"); + + sr.Close(); + } + + static void Main(string[] args) + { + string[] bestellung = { "S2", "V1", "H6", "V1", "H4", "H6", "D2" }; + Rechnung(bestellung); + } +} diff --git a/Speisekarte/Speisekarte.csproj b/Speisekarte/Speisekarte.csproj new file mode 100644 index 0000000..206b89a --- /dev/null +++ b/Speisekarte/Speisekarte.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/Speisekarte/Speisekarte.txt b/Speisekarte/Speisekarte.txt index 9611c6f..592b2f3 100644 --- a/Speisekarte/Speisekarte.txt +++ b/Speisekarte/Speisekarte.txt @@ -1,13 +1,13 @@ -S1;Peking Suppe;3,00 -S2;Miso Suppe;2,90 -V1;Frühlingsrolle;3,10 -V2;Krabbenchips (Krupuk);2,80 -V3;Gemischter Salat;3,30 -H1;Gebratene Nudeln mit Gemüse;8,50 -H2;Nasi-Goreng;10,80 -H3;Hühnerfleisch Chop-Suey;9,50 -H4;Schweinefleisch süß-sauer;10,00 -H5;Rindfleisch Gong-Bao;11,50 -H6;Ente mit Gemüse;12,50 -D1;Gebackene Banane mit Honig;3,00 -D2;Frisches Obst;2,50 +S1;Peking Suppe;3,00 +S2;Miso Suppe;2,90 +V1;Frühlingsrolle;3,10 +V2;Krabbenchips (Krupuk);2,80 +V3;Gemischter Salat;3,30 +H1;Gebratene Nudeln mit Gemüse;8,50 +H2;Nasi-Goreng;10,80 +H3;Hühnerfleisch Chop-Suey;9,50 +H4;Schweinefleisch süß-sauer;10,00 +H5;Rindfleisch Gong-Bao;11,50 +H6;Ente mit Gemüse;12,50 +D1;Gebackene Banane mit Honig;3,00 +D2;Frisches Obst;2,50 diff --git a/Uebung11.sln b/Uebung11.sln new file mode 100644 index 0000000..b5211dc --- /dev/null +++ b/Uebung11.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34616.47 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Person", "Person\Person.csproj", "{BBA20558-C5C6-4269-B174-5E1D2D0F5E4F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Speisekarte", "Speisekarte\Speisekarte.csproj", "{EC1D1AFD-7C0C-486D-BE64-6809A551F75B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BBA20558-C5C6-4269-B174-5E1D2D0F5E4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BBA20558-C5C6-4269-B174-5E1D2D0F5E4F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BBA20558-C5C6-4269-B174-5E1D2D0F5E4F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BBA20558-C5C6-4269-B174-5E1D2D0F5E4F}.Release|Any CPU.Build.0 = Release|Any CPU + {EC1D1AFD-7C0C-486D-BE64-6809A551F75B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EC1D1AFD-7C0C-486D-BE64-6809A551F75B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EC1D1AFD-7C0C-486D-BE64-6809A551F75B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EC1D1AFD-7C0C-486D-BE64-6809A551F75B}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {47941D0B-150B-4C64-8E29-CBFAF73E381F} + EndGlobalSection +EndGlobal -- GitLab