diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..dccd566 --- /dev/null +++ b/Program.cs @@ -0,0 +1,10 @@ +using StringViews; + +class Program +{ + static void Main(string[] args) + { + StringView view = new StringView(); + view.ShowMenu(); + } +} \ No newline at end of file diff --git a/Services/StringService.cs b/Services/StringService.cs new file mode 100644 index 0000000..aa217bf --- /dev/null +++ b/Services/StringService.cs @@ -0,0 +1,66 @@ +using System; + +namespace StringService +{ + public class StringService + { + private string currentText; + + public StringService() + { + currentText = string.Empty; + } + + public string CurrentText => currentText; + + public void SetText(string text) + { + currentText = text; + } + + public string ConvertToUpper() + { + return currentText.ToUpper(); + } + + public string ConvertToLower() + { + return currentText.ToLower(); + } + + public int CountCharacters() + { + return currentText.Length; + } + + public bool ContainsWord(string word) + { + return currentText.Contains(word); + } + + public string ReplaceWord(string oldWord, string newWord) + { + return currentText.Replace(oldWord, newWord); + } + + public string ExtractSubstring(int startIndex, int length) + { + if (startIndex < 0 || startIndex + length > currentText.Length) + { + return "Invalid substring range!"; + } + + return currentText.Substring(startIndex, length); + } + + public string TrimSpaces() + { + return currentText.Trim(); + } + + public void ResetText() + { + currentText = string.Empty; + } + } +} \ No newline at end of file diff --git a/Views/StringViews b/Views/StringViews new file mode 100644 index 0000000..989e596 --- /dev/null +++ b/Views/StringViews @@ -0,0 +1,182 @@ +using System; +using StringService; + +namespace StringViews +{ + public class StringView + { + private StringService stringService; + + public StringView() + { + stringService = new StringService(); + } + + public void ShowMenu() + { + while (true) + { + Console.Clear(); + Console.WriteLine("1. Enter text."); + Console.WriteLine("2. View Current text."); + Console.WriteLine("3. Convert to upper case."); + Console.WriteLine("4. Convert to lower case."); + Console.WriteLine("5. Count Characters."); + Console.WriteLine("6. Check if contains word."); + Console.WriteLine("7. Replace word."); + Console.WriteLine("8. Extract substring."); + Console.WriteLine("9. Trim spaces."); + Console.WriteLine("10. Reset text."); + Console.WriteLine("11. Exit"); + Console.Write("Choose an option: "); + + string choice = Console.ReadLine(); + + switch (choice) + { + case "1": + EnterText(); + break; + case "2": + ViewCurrentText(); + break; + case "3": + ConvertToUpper(); + break; + case "4": + ConvertToLower(); + break; + case "5": + CountCharacters(); + break; + case "6": + CheckIfContainsWord(); + break; + case "7": + ReplaceWord(); + break; + case "8": + ExtractSubstring(); + break; + case "9": + TrimSpaces(); + break; + case "10": + ResetText(); + break; + case "11": + Exit(); + return; + default: + Console.WriteLine("Invalid option, please try again!"); + Console.ReadKey(); + break; + } + } + } + + private void EnterText() + { + Console.Write("Enter text: "); + string text = Console.ReadLine(); + stringService.SetText(text); + Console.WriteLine("Text has been set."); + Console.ReadKey(); + } + + private void ViewCurrentText() + { + Console.WriteLine("Current text: " + stringService.CurrentText); + Console.ReadKey(); + } + + private void ConvertToUpper() + { + Console.WriteLine("Text in upper case: " + stringService.ConvertToUpper()); + Console.ReadKey(); + } + + private void ConvertToLower() + { + Console.WriteLine("Text in lower case: " + stringService.ConvertToLower()); + Console.ReadKey(); + } + + private void CountCharacters() + { + Console.WriteLine("Character Count: " + stringService.CountCharacters()); + Console.ReadKey(); + } + + private void CheckIfContainsWord() + { + Console.Write("Enter word to check: "); + string word = Console.ReadLine(); + + if (stringService.ContainsWord(word)) + { + Console.WriteLine($"The text contains the word '{word}'."); + } + else + { + Console.WriteLine($"The text does not contain the word '{word}'."); + } + + Console.ReadKey(); + } + + private void ReplaceWord() + { + Console.Write("Enter word to replace: "); + string oldWord = Console.ReadLine(); + + Console.Write("Enter new word: "); + string newWord = Console.ReadLine(); + + Console.WriteLine("Updated Text: " + stringService.ReplaceWord(oldWord, newWord)); + Console.ReadKey(); + } + + private void ExtractSubstring() + { + Console.Write("Enter starting index: "); + + if (!int.TryParse(Console.ReadLine(), out int startIndex)) + { + Console.WriteLine("Invalid number!"); + Console.ReadKey(); + return; + } + + Console.Write("Enter length of substring: "); + + if (!int.TryParse(Console.ReadLine(), out int length)) + { + Console.WriteLine("Invalid number!"); + Console.ReadKey(); + return; + } + + Console.WriteLine("Substring: " + stringService.ExtractSubstring(startIndex, length)); + Console.ReadKey(); + } + + private void TrimSpaces() + { + Console.WriteLine("Text after trim: " + stringService.TrimSpaces()); + Console.ReadKey(); + } + + private void ResetText() + { + stringService.ResetText(); + Console.WriteLine("Text has been reset."); + Console.ReadKey(); + } + + private void Exit() + { + Console.WriteLine("Exiting... Thank you!"); + } + } +} \ No newline at end of file