Reputation: 159
I want to check if the user input is one of the chars from "menuChars" and repeat the request as long as it doesn't fit. Here is my full code. I marked the actual problem with "===". You can also tell me if you find some other problems or performace improvements.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace AppointmentBook
{
class Program
{
static void Main(string[] args)
{
char userKeyPress = showMenue();
char[] menuChars = { 'e', 'E', 'l', 'L', 'k', 'K', 't', 'T', 's', 'S', 'b', 'B' };
//Console.WriteLine("\n" + userKeyPress);
Calendar calendarObj = new Calendar();
// ======================= problem area =====================================
if (menuChars.Contains(userKeyPress))
{
do
{
Console.WriteLine("\nBitte gültigen Buchstaben eingeben: ");
userKeyPress = Console.ReadKey().KeyChar;
}
while
(menuChars.Contains(userKeyPress));
}
// ============================================================
// if START
if (userKeyPress == 'e' || userKeyPress == 'E')
{
calendarObj.addMeeting();
}
if (userKeyPress == 'l' || userKeyPress == 'L')
{
calendarObj.deleteMeeting();
}
if (userKeyPress == 'k' || userKeyPress == 'K')
{
calendarObj.showCalendar();
}
if (userKeyPress == 't' || userKeyPress == 'T')
{
calendarObj.listMeetings();
}
if (userKeyPress == 's' || userKeyPress == 'S')
{
calendarObj.statistic();
}
if (userKeyPress == 'b' || userKeyPress == 'B')
{
calendarObj.endProgram();
}
}
static public char showMenue()
{
ConsoleKeyInfo readInput;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nWillkommen im Kalender!");
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\n\nDatum heute: {0:d}", DateTime.Now);
Console.BackgroundColor = ConsoleColor.Black;
Console.Write("\nMenü\n");
Console.Write("\n - Termin (e)intragen");
Console.Write("\n - Termin (l)öschen");
Console.Write("\n - (K)alender anzeigen");
Console.Write("\n - (T)erminliste ausgeben");
Console.Write("\n - (S)tatistik");
Console.Write("\n - (B)eenden");
Console.Write("\n\nIhre Wahl: ");
readInput = Console.ReadKey();
return readInput.KeyChar;
}
}
class Calendar
{
public Calendar() //Konstruktor
{
}
public void addMeeting()
{
var dateFormats = new[] {"dd.MM.yy", "dd-MM-yy", "dd/MM/yy"};
Console.Write("\n\nHinweis: Erlaubes Datumformat: TT.MM.YYYY");
Console.Write("\nDatum für Termin festlegen: ");
string readAddMeeting = Console.ReadLine();
DateTime scheduleDate;
bool validDate = DateTime.TryParseExact(
readAddMeeting,
dateFormats,
DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.None,
out scheduleDate);
if (validDate)
{
Console.WriteLine("Das Datum {0} ist gültig.", scheduleDate.ToShortDateString());
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("-- Fehler! -- Ungültiges Datumformat: \"{0}\"", readAddMeeting);
}
}
public void deleteMeeting()
{
Console.WriteLine("Termin löschen");
Console.WriteLine("=======================");
}
public void /*Rückgabetyp*/ showCalendar() // Funktion
{
Console.WriteLine("Kalender anzeigen");
Console.WriteLine("=======================");
}
public void listMeetings()
{
Console.WriteLine("Terminliste ausgeben");
Console.WriteLine("=======================");
}
public void statistic()
{
Console.WriteLine("Statistik anzeigen");
Console.WriteLine("=======================");
}
public void endProgram()
{
Console.WriteLine("Programm beenden");
Console.WriteLine("=======================");
}
}
}
Upvotes: 1
Views: 37
Reputation: 2504
The problem is that you are doing your evaluation before the while loop. In addition assign an empty character to userKeyPress
and call the showMenue()
method manually, so couldn't you do this instead:
char userKeyPress = '-';
showMenue();
while (!menuChars.Contains(userKeyPress))
{
Console.WriteLine("\nBitte gültigen Buchstaben eingeben: ");
userKeyPress = Console.ReadKey().KeyChar;
}
Upvotes: 2