What is wrong with my strategy design MVC code? [closed]

So I failed this coding technical exam. I am very confident with my answer. Please help I really wanted to improve. So what is wrong with this? I can’t find the issue with my code.

This is the Exam

Create a windows form application that has the following requirements in C#.

The program’s primary functionality is to sort a string with the input from a user.

Example input: befdac

Expected output: abcdef

The user interface will contain the following four elements:

A textbox that allows the user to input a string A control (for
instance a combo box) that selects the strategy to use for sorting the string.

A button that will sort the string using the selected strategy.

A label where the output will be shown (the sorted string)

The FOCUS of the assignment is on implementing the DESIGN PATTERNS.

a. Strategy pattern: is a software design pattern that enables an algorithm’s behavior to be selected at runtime. The strategy pattern will be used for sorting the strings in 2 different implementations. You can choose two of these sorting methods: Bubble Sort, Quicksort, or Merge Sort.

b. For the logic and view part of the application, it will be implemented in the Model-View-Controller pattern and you are not allowed to use a framework for this.

Make sure to follow proper development practices, and ensure that all validations are in place. Write unit tests for the individual components of your code to gain an advantage.

My Code

Interface

public interface ISortStrategy
{
   string Sort(string input);
}

Controller

namespace YouSourceCodingExam.Controller
{
    public class SortController
    {
        private readonly SortModel _model;
        private ISortStrategy _strategy;

        public SortController(SortModel model)
        {
            _model = model;
        }

        public void SetSortStrategy(ISortStrategy strategy)
        {
            _strategy = strategy;
        }

        public void Sort()
        {
            if (_strategy != null)
            {
                _model.Sorted = _strategy.Sort(_model.Input);
            }
        }

        public void UpdateModel(string input)
        {
            _model.Input = input;
        }

        public string GetSortedString()
        {
            return _model.Sorted;
        }
    }

}

Model

  public class SortModel
    {
        public string Input { get; set; }
        public string Sorted { get; set; }
    }

Concrete Strategies

 public class BubbleSortStrategy : ISortStrategy
 {

     //BUBBLE SORT STRATEGY
     public string Sort(string input)
     {
         //Repeatedly steps through the list, compares elements, and swaps them if they are 
         //in the wrong order.This process is repeated until the list is sorted.

         if (string.IsNullOrEmpty(input)) return input;

         char[] str = input.ToCharArray();
         for (int i = 0; i < str.Length - 1; i++)
         {
             for (int j = 0; j < str.Length - i - 1; j++)
             {
                 if (str[j] > str[j + 1])
                 {
                     // Swap str[j] and str[j+1]
                     char t = str[j];
                     str[j] = str[j + 1];
                     str[j + 1] = t;
                 }
             }
         }

         return new string(str);

         //OR USING LINQ
         var sortedChars = input.OrderBy(c => c);
         return new string(sortedChars.ToArray());


     }
 }

 public class QuickSortStrategy : ISortStrategy
 {

     //QUICK SORT STRATEGY
     public string Sort(string input)
     {
         if (input == null)
             throw new ArgumentNullException(nameof(input), "Input cannot be null.");
         if (input.Length == 0)
             return input;

         char[] str = input.ToCharArray();
         QSort(str, 0, str.Length - 1);
         return new string(str);
     }

     private void QSort(char[] str, int l, int h)
     {
         if (l < h)
         {
             int pi = Split(str, l, h);

             //Iterate repetitively before spllitting
             QSort(str, l, pi - 1);
             QSort(str, pi + 1, h);
         }
     }


     //Method to split or partition the string array
     private int Split(char[] str, int l, int h)
     {
         char p = str[h]; 
         int i = (l - 1); //smaller element

         for (int j = l; j < h; j++)
         {
             //if current is smaller than or equal to p
             if (str[j] < p)
             {
                 i++;
                 char t = str[i];
                 str[i] = str[j];
                 str[j] = t;
             }
         }

         char s = str[i + 1];
         str[i + 1] = str[h];
         str[h] = s;

         return i + 1;
     }
 }

View


  public partial class MainForm : Form
  {

      private readonly SortModel _model;
      private readonly SortController _controller;
      public MainForm()
      {
          InitializeComponent();

          _model = new SortModel();
          _controller = new SortController(_model);

          // Populate ComboBox with sorting strategies
          comboBoxSortStrategy.Items.Add("Bubble Sort");
          comboBoxSortStrategy.Items.Add("Quick Sort");
          comboBoxSortStrategy.SelectedIndex = 0;
      }

      private void MainForm_Load(object sender, EventArgs e)
      {
          
      }

      private void buttonSort_Click(object sender, EventArgs e)
      {
          string input = textBoxInput.Text;


          // Validate the input
          if (string.IsNullOrEmpty(input))
          {
              MessageBox.Show("Input string cannot be null or empty.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
              return;
          }


          _controller.UpdateModel(input);

          

          // Set the sorting strategy based on ComboBox selection
          ISortStrategy strategy;
          string selectedStrategy = comboBoxSortStrategy.SelectedItem.ToString();

          if (selectedStrategy == "Bubble Sort")
          {
              strategy = new BubbleSortStrategy();
          }
          else if (selectedStrategy == "Quick Sort")
          {
              strategy = new QuickSortStrategy();
          }
          else
          {
              throw new InvalidOperationException("Invalid sorting strategy selected");
          }

          _controller.SetSortStrategy(strategy);
          _controller.Sort();

          labelOutput.Text = _controller.GetSortedString();
      }

      private void comboBoxSortStrategy_SelectedIndexChanged(object sender, EventArgs e)
      {          
              labelOutput.Text = "";
              textBoxInput.Text = "";                     
      }
  }

New contributor

Nate 18 years ago is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật