From d6aa5fd8320ea1e630110bc18d399b994c9ab1cc Mon Sep 17 00:00:00 2001 From: dejOni Date: Sat, 28 Feb 2026 12:49:15 +0800 Subject: [PATCH] Add files via upload --- Program.cs | 11 ++++ Service/StringService.cs | 81 +++++++++++++++++++++++ View/StringView.cs | 138 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 230 insertions(+) create mode 100644 Program.cs create mode 100644 Service/StringService.cs create mode 100644 View/StringView.cs diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..543b6a3 --- /dev/null +++ b/Program.cs @@ -0,0 +1,11 @@ +using System; + +public class Program +{ + public static void Main(String[] args) + { + StringView view = new StringView(); + + view.Menu(); + } +} \ No newline at end of file diff --git a/Service/StringService.cs b/Service/StringService.cs new file mode 100644 index 0000000..6b308ca --- /dev/null +++ b/Service/StringService.cs @@ -0,0 +1,81 @@ +using System; + +public class StringService +{ + public void CheckNullOrEmpty(String text) + { + if (string.IsNullOrEmpty(text)) + { + Console.WriteLine("Enter a text first"); + Console.WriteLine(); + Console.WriteLine(); + } + } + + public void ConvertUppercase(String text) + { + if (string.IsNullOrEmpty(text)) + { + CheckNullOrEmpty(text); + } + else + { + Console.WriteLine(text.ToUpper()); + Console.WriteLine(); + Console.WriteLine(); + } + + } + public void ConvertLowercase(String text) + { + if (string.IsNullOrEmpty(text)) + { + CheckNullOrEmpty(text); + } + else + { + Console.WriteLine(text.ToLower()); + Console.WriteLine(); + Console.WriteLine(); + } + } + public int CountCharacters(String text) + { + return text.Length; + } + public bool Contains(String text, String searchWord) + { + if(string.IsNullOrEmpty(text) || string.IsNullOrEmpty(searchWord)) + { + return false; + } + return text.Contains(searchWord); + } + public string ReplaceWord(string currentText, string oldWord, string newWord) + { + if (string.IsNullOrEmpty(currentText)) + { + return ""; + } + return currentText.Replace(oldWord, newWord); + } + + public string Substring(string input, int startIndex, int length) + { + if (string.IsNullOrEmpty(input)) return ""; + + return input.Substring(startIndex, length); + } + + public string Trim(string input) + { + if (string.IsNullOrEmpty(input)) return ""; + + return input.Trim(); + } + + public String Reset(string originalText) + { + return originalText; + } +} diff --git a/View/StringView.cs b/View/StringView.cs new file mode 100644 index 0000000..c0cedab --- /dev/null +++ b/View/StringView.cs @@ -0,0 +1,138 @@ +using System; + +public class StringView +{ + public void Menu() + { + StringService service = new StringService(); + String text = ""; + String originalText = ""; + + bool running = true; + while (running) + { + + Console.WriteLine("---- Display Menu ----"); + Console.WriteLine(); + 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"); + + String displayOptions = Console.ReadLine(); + + switch (displayOptions) + { + case "1": + Console.WriteLine("Enter a text: "); + text = Console.ReadLine(); + Console.WriteLine(); + Console.WriteLine(); + break; + + case "2": + if (string.IsNullOrEmpty(text)) + { + service.CheckNullOrEmpty(text); + } + else + { + Console.WriteLine(text); + Console.WriteLine(); + Console.WriteLine(); + } + break; + + case "3": + service.ConvertUppercase(text); + break; + + case "4": + service.ConvertLowercase(text); + break; + + case "5": + if(string.IsNullOrEmpty(text)) + { + service.CheckNullOrEmpty(text); + } + else + { + int count = service.CountCharacters(text); + + Console.WriteLine($"Characters Count: {count}"); + } + break; + case "6": + Console.Write("Enter the word to search for: "); + string word = Console.ReadLine(); + + bool found = service.Contains(text, word); + + if (found) + { + Console.WriteLine($"Result: Yes, the word '{word}' was found!"); + } + else + { + Console.WriteLine($"Result: No, the word '{word}' is not in the text."); + } + Console.WriteLine(); + break; + + case "7": + if (string.IsNullOrEmpty(text)) + { + Console.WriteLine("Error: Please enter text first (Option 1)."); + } + else + { + Console.Write("Enter the word you want to change: "); + string target = Console.ReadLine(); + + Console.Write("Enter the new word: "); + string replacement = Console.ReadLine(); + + text = service.ReplaceWord(text, target, replacement); + + Console.WriteLine($"Updated Text: {text}"); + } + Console.WriteLine(); + break; + + case "8": + Console.Write("Enter starting index (0 is the first letter): "); + int.TryParse(Console.ReadLine(), out int start); + + Console.Write("Enter how many characters to extract: "); + int.TryParse(Console.ReadLine(), out int len); + + string result = service.Substring(text, start, len); + Console.WriteLine($"Extracted part: {result}"); + break; + + case "9": + text = service.Trim(text); // Update the text variable + Console.WriteLine("Spaces trimmed from the ends!"); + break; + + case "10": + text = service.Reset(originalText); + Console.WriteLine("Text reset to original!"); + break; + + case "11": + Console.Write("Exiting..."); + running = false; + break; + } + } + } +}