Skip to content
Snippets Groups Projects
Commit 1fed1c51 authored by Jens Albrecht's avatar Jens Albrecht
Browse files

.

parent 5341cd55
Branches
No related tags found
No related merge requests found
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
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
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
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);
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment