// Merge Command public static void MergeCommand(ContactBookLogic contactbooklogic, SQLConnection sql) { Console.WriteLine("Please enter the index of the contact which merges his location into a second contact."); sql.ReadContactsTable(); bool z = int.TryParse(Console.ReadLine(), out int temp1); if (z) { Console.WriteLine($"Please enter the index of the contact that you want to merge locations bla bla of the contact with index : {temp1} with.\n"); bool y = int.TryParse(Console.ReadLine(), out int temp2); if (y) { contactbooklogic.MergeContacts(temp1, temp2, sql); } else { Console.WriteLine("WARNING: Invalid Input!"); } } else { Console.WriteLine("WARNING: Invalid Index!"); } }
//Add or Get Location Command public static Location AddOrGetLocationCommand(ContactBookLogic contactbooklogic, SQLConnection sql, long countLocations) { bool addressIsCorrectInput = false; bool cityNameIsCorrectInput = false; var address = ""; var cityName = ""; while (!addressIsCorrectInput) { Console.WriteLine("\nPlease enter the address of the new contact."); address = Console.ReadLine(); addressIsCorrectInput = InputChecker.NoEmptyInputCheck(address); } while (!cityNameIsCorrectInput) { Console.WriteLine("\nPlease enter the cityname of the new contact."); cityName = Console.ReadLine(); cityNameIsCorrectInput = InputChecker.NoEmptyInputCheck(cityName); } Location location = contactbooklogic.AddOrGetLocation(contactbooklogic, sql, countLocations, address, cityName); return(location); }
//only locations public void ShowLocations(ContactBookLogic contactbooklogic, SQLConnection sql) { Console.WriteLine("\nWhat List of locations do you want to display?\n1. All locations\n2. All locations of a specific city\n3. All cities\nType 1, 2 or 3\n"); var check = Console.ReadLine(); Console.WriteLine(""); if (check == "1") { Console.WriteLine("\nListing all locations:\n"); List <Location> locationsList = sql.ReadLocationsTable(); foreach (var loc in locationsList) { Console.WriteLine($"{loc.LocationID} {loc.Address} {loc.CityName}, has contact: {loc.HasContact} "); } Console.WriteLine(""); } else if (check == "2") { Console.WriteLine("\nWhich city do you want to display the locations from?\nPossible cities are:\n"); List <string> tempLocationCityList = sql.ShowCitiesOfLocations(); foreach (string s in tempLocationCityList) { Console.WriteLine(s); } Console.WriteLine(""); var chosenCity = Console.ReadLine(); var CommandText = $"SELECT * FROM Locations l WHERE l.CityName = '{chosenCity}';"; long count = sql.ExecuteScalar(CommandText); if (count > 0) { Console.WriteLine($"\nListing all locations from city: {chosenCity}\n"); sql.ShowChosenCityOfLocation(chosenCity); Console.WriteLine(""); } else { Console.WriteLine($"\nWARNING: {chosenCity} does not exist in the database\n"); } } else if (check == "3") { Console.WriteLine("\nCities with locations:\n"); List <string> tempLocationCityList = sql.ShowCitiesOfLocations(); foreach (string s in tempLocationCityList) { Console.WriteLine(s); } } else { Console.WriteLine("Invalid Input!"); } }
// Edit Location Command public static void EditLocationCommand(ContactBookLogic contactbooklogic, SQLConnection sql, long countLocations) { Console.WriteLine($"Please enter the index of the location you wish to edit."); List <Location> locationsList = sql.ReadLocationsTable(); foreach (var loc in locationsList) { Console.WriteLine($"{loc.LocationID} {loc.Address} {loc.CityName}, has contact: {loc.HasContact} "); } bool z = int.TryParse(Console.ReadLine(), out int inputindex); if (z && inputindex <= countLocations && inputindex > 0) { Console.WriteLine("Please enter the number of what you want to edit.\n1. Address\n2. City\n"); var c = Console.ReadLine(); if (c == "1" || c == "2") { bool CorrectInput = false; var newvalue = ""; if (c == "1") { Console.WriteLine($"Please enter the new value for the address."); } else if (c == "2") { Console.WriteLine($"Please enter the new value for the cityname."); } while (!CorrectInput) { newvalue = Console.ReadLine(); CorrectInput = InputChecker.NoEmptyInputCheck(newvalue); //if (CorrectInput) // break; //TODO: check ob das nötig ist oder ob es ohne break auch geht } contactbooklogic.EditLocation(inputindex, c, sql, newvalue); } else { Console.WriteLine("WARNING: Invalid Input."); } } else { Console.WriteLine("WARNING: Invalid Index."); } }
// Add Contact Command public static void AddContactCommand(ContactBookLogic contactbooklogic, SQLConnection sql, long countLocations) { bool nameIsCorrectInput = false; bool inputIsNumber = false; bool mailAddressIsCorrectInput = false; bool genderIsCorrectInput = false; var name = ""; var mailAddress = ""; long phoneNumber = 0; var gender = ""; while (!nameIsCorrectInput) { Console.WriteLine("\nPlease enter the Name of the new Contact."); name = Console.ReadLine(); nameIsCorrectInput = InputChecker.NoEmptyInputCheck(name); } Location location = AddOrGetLocationCommand(contactbooklogic, sql, countLocations); while (!inputIsNumber) { Console.WriteLine("\nPlease enter the phone number for the new Contact"); inputIsNumber = long.TryParse(Console.ReadLine(), out phoneNumber); } while (!mailAddressIsCorrectInput) { Console.WriteLine("\nPlease enter a mailaddress for the new Contact"); mailAddress = Console.ReadLine(); mailAddressIsCorrectInput = InputChecker.MailFormatCheck(mailAddress); } while (!genderIsCorrectInput) { Console.WriteLine("\nPlease enter the gender for the new Contact ('Male' or 'Female')"); gender = Console.ReadLine(); genderIsCorrectInput = InputChecker.GenderCheck(gender); } bool conWasDupe = contactbooklogic.AddContact(contactbooklogic, sql, countLocations, name, location, phoneNumber, mailAddress, gender); if (conWasDupe) { Console.WriteLine("INFO: Contact was a duplicate and has not been added to the database."); } }
//TODOH: hier liste bekommen und ausgabe hier - damit es von SQL connection getrennt ist - überall (vlt mit List<object>?) public void ListWanted(ContactBookLogic contactbooklogic, SQLConnection sql, long countContacts, long countLocations) { Console.WriteLine("\nDo you want a list of 1. only contacts, 2. only locations or 3. both?\nType 1, 2 or 3\n"); var v = Console.ReadLine(); if (v == "1") { if (countContacts > 0) { ShowContacts(contactbooklogic, sql); } else { Console.WriteLine("\nWARNING: There are no contacts which could be displayed.\n"); } } else if (v == "2") { if (countLocations > 0) { ShowLocations(contactbooklogic, sql); } else { Console.WriteLine("\nWARNING: There are no locations which could be displayed.\n"); } } else if (v == "3") { if (countContacts > 0 || countLocations > 0) { ShowBoth(contactbooklogic, sql); } else { Console.WriteLine("\nWARNING: There are neither contacts nor locations which could be displayed."); } } else { Console.WriteLine("Invalid Input."); } }
// Edit Contact Command public static void EditContactCommand(ContactBookLogic contactbooklogic, SQLConnection sql, long countContacts) { Console.WriteLine("\nPlease enter the index of the contact you wish to edit."); sql.ReadContactsTable(); bool correctIndex = int.TryParse(Console.ReadLine(), out int inputindex); if (correctIndex && inputindex <= countContacts && inputindex > 0) { var newValue = ""; bool newValueIsCorrectInput = false; var CommandText = ""; bool inputIsNumber = false; long phoneNumber = 0; Console.WriteLine("\nPlease enter the number of what you want to edit.\n1. Name\n2. PhoneNumber\n3. MailAddress"); var c = Console.ReadLine(); if (c == "1") { while (!newValueIsCorrectInput) { Console.WriteLine($"Please enter the new value for the name."); var name = Console.ReadLine(); newValueIsCorrectInput = InputChecker.NoEmptyInputCheck(newValue); } CommandText = $"UPDATE contacts SET Name = '{newValue}' WHERE ContactID = {inputindex};"; sql.ExecuteNonQuery(CommandText); } else if (c == "2") { while (!inputIsNumber) { Console.WriteLine("\nPlease enter the phone number for the new Contact"); inputIsNumber = long.TryParse(Console.ReadLine(), out phoneNumber); } CommandText = $"UPDATE contacts SET phoneNumber = '{phoneNumber}' WHERE ContactID = {inputindex};"; sql.ExecuteNonQuery(CommandText); } else if (c == "3") { while (!newValueIsCorrectInput) { Console.WriteLine($"Please enter the new value for the Mailaddress."); var mailAddress = Console.ReadLine(); newValueIsCorrectInput = InputChecker.MailFormatCheck(mailAddress); } CommandText = $"UPDATE contacts SET MailAddress = '{newValue}' WHERE ContactID = {inputindex};"; sql.ExecuteNonQuery(CommandText); } else { Console.WriteLine("WARNING: Invalid Input."); } } else { Console.WriteLine("WARNING: Invalid Index."); } }
//only contacts public void ShowContacts(ContactBookLogic contactbooklogic, SQLConnection sql) { List <string> tempList = new List <string>(); Console.WriteLine("\nWhat List of contacts do you want to display?\n1. All contacts\n2. All contacts of a specific city\n3. All cities\n4. All male contacts\n5. All female contacts\nType 1, 2 or 3\n"); var check = Console.ReadLine(); if (check == "1") { Console.WriteLine("\nListing all contacts:\n"); sql.ReadContactsTable(); Console.WriteLine(""); } else if (check == "2") { Console.WriteLine(""); Console.WriteLine("\nWhich city do you want to display the contacts from?\nPossible cities are:\n"); tempList = sql.ShowCitiesOfContacts(); foreach (string s in tempList) { Console.WriteLine(s); } Console.WriteLine(""); var chosenCity = Console.ReadLine(); var CommandText = $"SELECT * FROM locations l INNER JOIN contacts c ON c.LocationID = l.LocationID WHERE l.CityName = '{chosenCity}';"; long count = sql.ExecuteScalar(CommandText); if (count > 0) { Console.WriteLine($"\nListing all contacts from city: {chosenCity}\n"); sql.ShowChosenCityOfContacts(chosenCity); Console.WriteLine(""); } else { Console.WriteLine($"\nWARNING: {chosenCity} does not exist in the database\n"); } } else if (check == "3") { Console.WriteLine("\nCities with contacts:\n"); tempList = sql.ShowCitiesOfContacts(); foreach (string s in tempList) { Console.WriteLine(s); } } else if (check == "4") { Console.WriteLine("\nMale contacts:\n"); var gender = "Male"; sql.ShowGenderSpecificList(gender); } else if (check == "5") { Console.WriteLine("\nFemale contacts:\n"); var gender = "Female"; sql.ShowGenderSpecificList(gender); } else { Console.WriteLine("\nInvalid input!\n"); } }
//contacts and locations public void ShowBoth(ContactBookLogic contactbooklogic, SQLConnection sql) { List <Contact> tempList = new List <Contact>(); Console.WriteLine("\nWhat List do you want to display?\n1. All Contacts and Locations\n2. All contacts and locations of a specific city\n3. All cities\nType 1, 2 or 3\n"); var check = Console.ReadLine(); if (check == "1") { Console.WriteLine("\nListing all contacts and locations...\n"); Console.WriteLine("Contacts:"); sql.ReadContactsTable(); Console.WriteLine(""); Console.WriteLine("\nLocations:"); List <Location> locationsList = sql.ReadLocationsTable(); foreach (var loc in locationsList) { if (loc.HasContact == true) { Console.WriteLine($"{loc.LocationID} {loc.Address} {loc.CityName} assigned to a contact "); } else if (loc.HasContact == false) { Console.WriteLine($"{loc.LocationID} {loc.Address} {loc.CityName}"); } } Console.WriteLine(""); } else if (check == "2") { Console.WriteLine("\nWhich city do you want to display the contacts and locations from?\nPossible cities are:\n"); List <string> tempLocationCityList = sql.ShowCitiesOfLocations(); foreach (string s in tempLocationCityList) { Console.WriteLine(s); } Console.WriteLine(""); var chosenCity = Console.ReadLine(); var CommandText = $"SELECT * FROM Locations l WHERE l.CityName = '{chosenCity}';"; long count = sql.ExecuteScalar(CommandText); if (count > 0) { Console.WriteLine($"Showing Contacts and Locations of the city: {chosenCity}"); Console.WriteLine("\nContacts:"); sql.ShowChosenCityOfContacts(chosenCity); Console.WriteLine("\nLocations:"); sql.ShowChosenCityOfLocation(chosenCity); } else { Console.WriteLine($"\nWARNING: {chosenCity} does not exist in the database\n"); } } else if (check == "3") { Console.WriteLine("\nCities with contacts:\n"); List <string> tempContactCityList = sql.ShowCitiesOfContacts(); foreach (string s in tempContactCityList) { Console.WriteLine(s); } Console.WriteLine("\nCities with locations:\n"); List <string> tempLocationCityList = sql.ShowCitiesOfLocations(); foreach (string s in tempLocationCityList) { Console.WriteLine(s); } Console.WriteLine(""); } else { Console.WriteLine("Invalid Input."); } }
static void Main() { //TODOL: Refactoring komplett //TODO: Trennung von Input und Logik für WPF //TODOL: csv path anpassen + generic? //TODO: List 2.2 2.3 3.2 3.3 - Import - Edit - general check again ShowConsoleOutput showList = new ShowConsoleOutput(); ContactBookLogic contactbooklogic = new ContactBookLogic(); CsvReader reader = new CsvReader(); SQLConnection sql = new SQLConnection(); while (true) { Console.WriteLine("\nType 'Add', 'Edit', 'Remove', 'List', 'Quit', 'Help', 'Clear' or 'Import'"); long countContacts = sql.GetTableRowCount("contacts"); long countLocations = sql.GetTableRowCount("locations"); Console.WriteLine($"There are currently {countContacts} contacts and {countLocations} locations in the database.\n"); string input = Console.ReadLine(); // ADD METHOD if (input == "Add") { Console.WriteLine("\nWhat do you want to add?\n1. Contact\n2. Location\n"); input = Console.ReadLine(); if (input == "1") { ContactbookConsoleInputControl.AddContactCommand(contactbooklogic, sql, countLocations); } else if (input == "2") { ContactbookConsoleInputControl.AddOrGetLocationCommand(contactbooklogic, sql, countLocations); } else { Console.WriteLine("WARNING: Invalid Input.\n"); } } // EDIT AND MERGE METHODS else if (input == "Edit") { if (countContacts > 0 || countLocations > 0) { Console.WriteLine("\nWhat do you want to do?\n1. Edit a contact or location\n2. Merge a contact\n"); var i = Console.ReadLine(); //EDIT //TODO: hier edit refactoren if (i == "1") { Console.WriteLine("\nWhat do you want to edit?\n1. Contact\n2. Location\n"); var a = Console.ReadLine(); if (a == "1") { if (countContacts > 0) { ContactbookConsoleInputControl.EditContactCommand(contactbooklogic, sql, countContacts); } else { Console.WriteLine("\nWARNING: There is no contact that can be edited.\n"); } } else if (a == "2") { if (countLocations > 0) { ContactbookConsoleInputControl.EditLocationCommand(contactbooklogic, sql, countLocations); } else { Console.WriteLine("\nWARNING: There is no location that can be edited.\n"); } } else { Console.WriteLine("WARNING: Invalid Input"); } } //MERGE else if (i == "2" && countContacts > 1) { ContactbookConsoleInputControl.MergeCommand(contactbooklogic, sql); } else { Console.WriteLine("\nWARNING: Invalid Input or not enough contacts."); } } else { Console.WriteLine("\nWARNING: You need atleast a contact or a location to edit or merge something."); } } // REMOVE METHOD else if (input == "Remove") { if (countContacts > 0 || countLocations > 0) { Console.WriteLine("\nWhat do you want to remove?\n1. Contact\n2. Location\n3. Everything\n"); var b = Console.ReadLine(); if (b == "1") { if (countContacts > 0) { Console.WriteLine("Please enter the index of the contact you want to remove."); sql.ReadContactsTable(); Console.WriteLine(""); bool numberCheck = int.TryParse(Console.ReadLine(), out var value); if (numberCheck) { contactbooklogic.RemoveContact(contactbooklogic, countContacts, sql, value); } else { Console.WriteLine("\nWARNING: There is no contact that can be removed.\n"); } } } else if (b == "2") { if (countLocations > 0) { Console.WriteLine("\nPlease enter the index of the location you want to remove.\n"); List <Location> locationsList = sql.ReadLocationsTable(); foreach (var loc in locationsList) { Console.WriteLine($"{loc.LocationID} {loc.Address} {loc.CityName}, has contact: {loc.HasContact} "); } Console.WriteLine(""); bool numberCheck = int.TryParse(Console.ReadLine(), out var value); if (numberCheck) { contactbooklogic.RemoveLocation(contactbooklogic, countLocations, sql, value); } else { Console.WriteLine("WARNING: Invalid Input"); } } else { Console.WriteLine("\nWARNING: There is no location that can be removed.\n"); } } else if (b == "3") { if (countContacts > 0 || countLocations > 0) { Console.WriteLine("\nIf you really want to empty the entire database enter 'y' now.\n"); var confirmation = Console.ReadLine(); if (confirmation == "y") { contactbooklogic.RemoveEverything(sql); } } else { Console.WriteLine("\nWARNING: There is nothing that can be deleted from the table.\n"); } } else { Console.WriteLine("WARNING: Invalid Input.\n"); } } else { Console.WriteLine("\nWARNING: There is nothing that can be removed.\n"); } } // LIST METHOD else if (input == "List") { if (countContacts > 0 || countLocations > 0) { showList.ListWanted(contactbooklogic, sql, countContacts, countLocations); } else { Console.WriteLine("\nWARNING: There is nothing that can be listed.\n"); } } // QUIT METHOD else if (input == "Quit") { break; } else if (input == "Clear") { Console.Clear(); } // HELP METHOD else if (input == "Help") { ContactbookConsoleInputControl.HelpCommand(); } //IMPORT METHOD else if (input == "Import") { Console.WriteLine("Do you want to import 1. testfile.csv or 2. errortestfile.csv?\nType 1 or 2\n"); string csvFileName = ""; string fileNameInput = Console.ReadLine(); Console.WriteLine(""); if (fileNameInput == "1") { csvFileName = "testfile"; reader.ImportEntriesFromCsvIntoList(contactbooklogic, csvFileName, sql); } else if (fileNameInput == "2") { csvFileName = "errortestfile"; reader.ImportEntriesFromCsvIntoList(contactbooklogic, csvFileName, sql); } else { Console.WriteLine("WARNING: Wrong input."); } } else { Console.WriteLine($"\nWARNING: {input} is not a valid input. \n"); } } }