diff --git a/StringService.cs b/StringService.cs new file mode 100644 index 0000000..331d936 --- /dev/null +++ b/StringService.cs @@ -0,0 +1,68 @@ +using System; + +namespace StringProcessingApp.Services +{ + public class StringService + { + private string currentText = ""; + private string originalText = ""; + + public void SetText(string text) + { + currentText = text; + originalText = text; + } + + public string GetText() + { + return currentText; + } + + public void ToUpperCase() + { + currentText = currentText.ToUpper(); + } + + public void ToLowerCase() + { + currentText = currentText.ToLower(); + } + + public int CountCharacters() + { + return currentText.Length; + } + + public bool ContainsWord(string word) + { + return currentText.Contains(word); + } + + public void ReplaceWord(string oldWord, string newWord) + { + currentText = currentText.Replace(oldWord, newWord); + } + + public void ExtractSubstring(int startIndex, int length) + { + if (startIndex >= 0 && startIndex + length <= currentText.Length) + { + currentText = currentText.Substring(startIndex, length); + } + else + { + Console.WriteLine("Invalid substring range."); + } + } + + public void TrimSpaces() + { + currentText = currentText.Trim(); + } + + public void ResetText() + { + currentText = originalText; + } + } +} diff --git a/Stringview.cs[MERCADO] b/Stringview.cs[MERCADO] new file mode 100644 index 0000000..8fa1692 --- /dev/null +++ b/Stringview.cs[MERCADO] @@ -0,0 +1,113 @@ +using System; +using StringProcessingApp.Services; + +namespace StringProcessingApp.Views +{ + public class StringView + { + private StringService service = new StringService(); + + public void Run() + { + bool running = true; + + while (running) + { + ShowMenu(); + Console.Write("Choose an option: "); + string choice = Console.ReadLine(); + + Console.WriteLine(); + + switch (choice) + { + case "1": + Console.Write("Enter text: "); + string text = Console.ReadLine(); + service.SetText(text); + break; + + case "2": + Console.WriteLine("Current Text: " + service.GetText()); + break; + + case "3": + service.ToUpperCase(); + Console.WriteLine("Converted to UPPERCASE."); + break; + + case "4": + service.ToLowerCase(); + Console.WriteLine("Converted to lowercase."); + break; + + case "5": + Console.WriteLine("Character Count: " + service.CountCharacters()); + break; + + case "6": + Console.Write("Enter word to check: "); + string word = Console.ReadLine(); + Console.WriteLine(service.ContainsWord(word) + ? "Word found!" + : "Word not found."); + break; + + case "7": + Console.Write("Enter word to replace: "); + string oldWord = Console.ReadLine(); + Console.Write("Enter new word: "); + string newWord = Console.ReadLine(); + service.ReplaceWord(oldWord, newWord); + Console.WriteLine("Word replaced."); + break; + + case "8": + Console.Write("Start index: "); + int start = int.Parse(Console.ReadLine()); + Console.Write("Length: "); + int length = int.Parse(Console.ReadLine()); + service.ExtractSubstring(start, length); + break; + + case "9": + service.TrimSpaces(); + Console.WriteLine("Spaces trimmed."); + break; + + case "10": + service.ResetText(); + Console.WriteLine("Text reset to original."); + break; + + case "11": + running = false; + Console.WriteLine("Exiting program..."); + break; + + default: + Console.WriteLine("Invalid choice."); + break; + } + + Console.WriteLine(); + } + } + + private void ShowMenu() + { + Console.WriteLine("==== String Processing System ===="); + Console.WriteLine("1. Enter Text"); + Console.WriteLine("2. View Current Text"); + Console.WriteLine("3. Convert to UPPERCASE"); + Console.WriteLine("4. Convert to lowercase"); + 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"); + } + } +}