Manage event management differently depending on system state

I am trying to build an Ingenico POS terminal simulator (iWL220). On the home screen, I have a combo box. When the user enters ID and password, 6 menus are loaded into the combo box. If the user clicks on btn1, then in the combo box, clear the menu and add another set of menus. If the user clicks btn1

for a new loaded menu, then again the combo box is cleared and loads another set of menus, and so on.

My problem is every button click (btn1, btn2, btn3, btn4, btn5) I need to code a lot of if else commands. Example

The first menu (in the combo box) has 6 sectors.

  • 1.SectorA
  • 2.SectorB
  • 3.SectorC
  • 4.SectorD
  • 5.SectorE
  • 6.SectorF

If the user selects 1.SectorA then the user hits btn1. Then btn1 clear the combo box and load another set of menus. There are 3 companies in this time menu (in the combo box).

  • 1.CompanyA
  • 2.CompanyB
  • 3.CompanyC

This time the user selects 1.CompanyA, then the user clicks btn1 again. Then btn1 clear the combo box and load another set of menus. This time menu (in the combo box) has 2 payment options.

  • 1.FullPayment
  • 2.ParitalPayment

Now when the user hits btn1 or btn2, the combo box becomes false and the home screen has a shortcut and text box. The text box allows the user to enter the subscriber number and press Enter (green button).

I already load the Ingenico terminal picture as jpeg, and on top I set my buttons. I only gave a small version of my simulation. In my application, there is a probability that the user can select.

In my application, btn1 has a 92 click probability, btn2 has a 53 click probability, and so on. After the user enters the subscriber number and clicks the green button, my application uses wfc services to format the data and send it to the sql server. But before the user clicks each button combination where in my application I store the btn number as 422. This 422 means that the user has selected the SectorD + CompanyB + ParitalPayment option. So my wfc will know what 422 means.

My question is, is this the shortest way to construct button events for this probabilistic case?


I have 4 buttons. Btn1, Btn2, Btn3 and Btn4. Also I have multiple arrays as shown below and 1 combo box.

1.ArrayMain() = {"1.Water","2.Air","3.Soil","4.Fire"}
   1.1. ArrayWater() = {"1.Salty","2.Fresh", "3.Contaminated"}
      1.1.1.ArraySalty() = {1."AA", 2."BB", 3."CC"}
      1.1.2.ArrayFresh() = {1."DD", 2."EE", 3."FF"}
      1.1.3.ArrayContaminated() = {1."XX", 2."YY", 3."ZZ"}                              

1.2   ArrayAir() = {"1.Fresh", "2.Contaminated"}
1.3   ArraySoil() = {"1.Normal", "2.Contaminated"}
1.4   ArrayFire() = {"1.Low","2.Mid","3.High"}

      

When my application starts, the first array values 1.(ArrayMain)

populate the comboBox. This comboBox will have 4 values ​​like "1.Water", "2.Air", "3.Soil", "4.Fire". If the user selects "1.Water", then the user presses Btn1. Than btn1 events clear the comboBox and load the values 1.1ArrayWater()

into the comboBox.

Second time if the user selects "1.Salty" than the user hits btn1 again, and this time the btn1 events clear the comboBox and load the values 1.1.1ArraySalty()

into the comboBox.

The third time, if the user selects "2.BB", than the user presses Btn2 and sends the information "BB" for calculation.

Change ComboBox Values ​​with button selection

First you have 5 (more or less) menu items and every time you press any (numeric) buttons (1 to 9 lilke in pos terminal) a new menu appears on the screen.

+3


source to share


1 answer


Each button at any particular time performs a specific action depending on the state of the system. Obviously, if you try to define a specific action based on many different variables, you will create a lot of branching code. Such code is very difficult to write correctly and even harder to debug and maintain.

So what if we encapsulate the current action for each possible state (sequence of states) in some particular class (interface):

/// <summary>
/// Represents internal terminal presenter that is used inside IGlobalTerminalPresenter.
/// </summary>
public interface ITerminalPresenter
{
    void UpdateUI();
    ITerminalPresenter this[Int32 index]
    {
        get;
    }
    ITerminalPresenter Do1();
    ITerminalPresenter Do2();
    ITerminalPresenter Parent
    {
        get;
        set;
    }
    void Reset();
}

      

Inside the form, we will use a field of a similar interface that encapsulates all of the presenter's changes.

/// <summary>
/// Represents terminal presenter that UI can operate upon.
/// </summary>
public interface IGlobalTerminalPresenter
{
    void UpdateUI();

    void Do1();

    void Do2();

    Int32 SelectedIndex
    {
        get;
        set;
    }

    void Reset();
}

      

Our event handlers will become:

    private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        var senderComboBox = (ComboBox)sender;

        this.globalTerminalPresenter.SelectedIndex = senderComboBox.SelectedIndex;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.globalTerminalPresenter.Do1();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.globalTerminalPresenter.Do2();
    }

      

To make our specific TerminalPresenters interact with the form, we'll make our form implement the following interface:

/// <summary>
/// This represents your UI in technology-independent manner
/// </summary>
public interface ITerminalView
{
    String Title { get; set; }
    String Input { get; set; }
    String Output { get; set; }
    String Button1_Text { get; set; }
    String Button2_Text { get; set; }
    IEnumerable<String> SelectionItems { get; set; }
    void Clear();
}

public partial class MainForm : Form,
    ITerminalView
{
    ...
    #region ITerminalView implementation

    public string Title
    {
        get { return this.Text; }
        set { this.Text = value; }
    }

    public String Button1_Text
    {
        get { return this.button1.Text; }
        set { this.button1.Text = value; }
    }

    public String Button2_Text
    {
        get { return this.button2.Text; }
        set { this.button2.Text = value; }
    }

    public string Input
    {
        get { return this.textBox_Input.Text; }
        set { this.textBox_Input.Text = value; }
    }

    public string Output
    {
        get { return this.textBox_Output.Text; }
        set { this.textBox_Output.Text = value; }
    }

    public IEnumerable<string> SelectionItems
    {
        get { return this.comboBox.Items.Cast<String>(); }
        set
        { 
            this.comboBox.Items.Clear();

            if (value == null)
                return;

            foreach (var item in value)
            {
                this.comboBox.Items.Add(item);
            }
        }
    }

    public void Clear()
    {
        this.comboBox.SelectedIndex = -1;
        this.Title = String.Empty;
        this.Input = String.Empty;
        this.Output = String.Empty;
        this.SelectionItems = null;
    }

    #endregion

      



We will now create two TerminalPresenters - one to simply allow the next option to be selected through a combobox that calculates the sum of the two numbers. They both use the same base class.

/// <summary>
/// Base class for all presenters
/// </summary>
public abstract class TerminalPresenterBase : ITerminalPresenter
{
    protected ITerminalView view;

    public TerminalPresenterBase(ITerminalView view)
    {
        if (view == null) 
            throw new ArgumentNullException("view");

        this.view = view;
        this.Parent = this;
    }

    public abstract void UpdateUI();

    public abstract ITerminalPresenter this[int index]
    {
        get;
    }

    public abstract ITerminalPresenter Do1();
    public abstract ITerminalPresenter Do2();

    public virtual ITerminalPresenter Parent
    {
        get;
        set;
    }

    public virtual void Reset()
    {
        this.UpdateUI();
    }
}

/// <summary>
/// Presenter whose sole goal is to allow user to select some other option and press next
/// </summary>
public class SelectOptionPresenter : TerminalPresenterBase
{
    private IList<KeyValuePair<String, ITerminalPresenter>> options;
    private ITerminalPresenter selected;
    private String title;

    public SelectOptionPresenter(ITerminalView view,
        String title, 
        IList<KeyValuePair<String, ITerminalPresenter>> options)
        : base(view)
    {
        if (options == null)
            throw new ArgumentNullException("options");

        this.title = title;

        this.options = options;

        foreach (var item in options)
        {
            item.Value.Parent = this;
        }
    }

    public override void UpdateUI()
    {
        this.view.Clear();

        this.view.Button1_Text = "Confirm selection";
        this.view.Button2_Text = "Go back";
        this.view.Title = title;
        this.view.SelectionItems = options
            .Select(opt => opt.Key);
    }

    public override ITerminalPresenter this[int index]
    {
        get
        {
            this.selected = this.options[index].Value;

            return this;
        }
    }

    public override ITerminalPresenter Do1()
    {
        return this.ConfirmSelection();
    }

    public override ITerminalPresenter Do2()
    {
        return this.GoBack();
    }

    public ITerminalPresenter ConfirmSelection()
    {
        this.selected.UpdateUI();
        return this.selected;
    }

    public ITerminalPresenter GoBack()
    {
        this.Parent.UpdateUI();
        return this.Parent;
    }
}

public enum APlusBState
{
    EnterA,
    EnterB,
    Result
}

public class StepActions
{
    public Action UpdateUI { get; set; }

    public Func<ITerminalPresenter> Do1 { get; set; }

    public Func<ITerminalPresenter> Do2 { get; set; }
}

public class APlusBPresenter : TerminalPresenterBase
{
    private Int32 a, b;
    private APlusBState state;
    private String error = null;

    private Dictionary<APlusBState, StepActions> stateActions;

    private void InitializeStateActions()
    {
        this.stateActions = new Dictionary<APlusBState, StepActions>();

        this.stateActions.Add(APlusBState.EnterA,
            new StepActions()
            {
                UpdateUI = () =>
                {
                    this.view.Title = this.error ?? "Enter A";
                    this.view.Input = this.a.ToString();
                    this.view.Button1_Text = "Confirm A";
                    this.view.Button2_Text = "Exit";
                },
                Do1 = () => // Confirm A
                {
                    if (!Int32.TryParse(this.view.Input, out this.a))
                    {
                        this.error = "A is in incorrect format. Enter A again";
                        return this;
                    }

                    this.error = null;                     
                    this.state = APlusBState.EnterB;

                    return this;
                },
                Do2 = () => // Exit
                {
                    this.Reset();

                    return this.Parent;
                }
            });

        this.stateActions.Add(APlusBState.EnterB,
            new StepActions()
            {
                UpdateUI = () =>
                {
                    this.view.Title = this.error ?? "Enter B";
                    this.view.Input = this.b.ToString();
                    this.view.Button1_Text = "Confirm B";
                    this.view.Button2_Text = "Back to A";
                },
                Do1 = () => // Confirm B
                {
                    if (!Int32.TryParse(this.view.Input, out this.b))
                    {
                        this.error = "B is in incorrect format. Enter B again";
                        return this;
                    }

                    this.error = null;                     
                    this.state = APlusBState.Result;

                    return this;
                },
                Do2 = () => // Back to a
                {
                    this.state = APlusBState.EnterA;

                    return this;
                }
            });

        this.stateActions.Add(APlusBState.Result,
            new StepActions()
            {
                UpdateUI = () =>
                {
                    this.view.Title = String.Format("The result of {0} + {1}", this.a, this.b);
                    this.view.Output = (this.a + this.b).ToString();
                    this.view.Button1_Text = "Exit";
                    this.view.Button2_Text = "Back";
                },
                Do1 = () => // Exit
                {
                    this.Reset();

                    return this.Parent;
                },
                Do2 = () => // Back to B
                {
                    this.state = APlusBState.EnterB;

                    return this;
                }
            });
    }

    public APlusBPresenter(ITerminalView view) : base(view)
    {
        this.InitializeStateActions();
        this.Reset();
    }

    public override void UpdateUI()
    {
        this.view.Clear();

        this.stateActions[this.state].UpdateUI();
    }

    public override ITerminalPresenter this[int index]
    {
        get { throw new NotImplementedException(); }
    }

    public override ITerminalPresenter Do1()
    {
        var nextPresenter = this.stateActions[this.state].Do1();

        nextPresenter.UpdateUI();

        return nextPresenter;
    }

    public override ITerminalPresenter Do2()
    {
        var nextPresenter = this.stateActions[this.state].Do2();

        nextPresenter.UpdateUI();

        return nextPresenter;
    }

    public override void Reset()
    {
        this.state = APlusBState.EnterA;
        this.a = 0;
        this.b = 0;
        this.error = null;
    }
}

/// <summary>
/// Represents terminal presenter to use inside GUI. It handles current ISpecificTerminalPresenter inside itself.
/// </summary>
public class GlobalTerminalPresenter : IGlobalTerminalPresenter
{
    #region Fields

    private ITerminalPresenter current;
    private Int32 selectedIndex;

    #endregion


    #region Constructors

    public GlobalTerminalPresenter(ITerminalPresenter mainPresenter)
    {
        if (mainPresenter == null)
            throw new ArgumentNullException("mainPresenter");

        this.current = mainPresenter;

        this.UpdateUI();
    }

    #endregion

    public void UpdateUI()
    {
        this.current.UpdateUI();
    }

    public void Do1()
    {
        this.current = this.current.Do1();
    }

    public void Do2()
    {
        this.current = this.current.Do2();
    }

    public Int32 SelectedIndex
    {
        get
        {
            return this.selectedIndex;
        }
        set
        {
            this.selectedIndex = value;

            if (value == -1)
                return;

            this.current = this.current[value];
        }
    }

    public void Reset()
    {
        this.current.Reset();
    }
}

      

We then initialize them in the constructor of our form:

public partial class MainForm : Form,
    ITerminalView
{
    private IGlobalTerminalPresenter globalTerminalPresenter;

    public MainForm()
    {
        InitializeComponent();

        var nextLevelPresenters = new KeyValuePair<String, ITerminalPresenter>[]
        {
            new KeyValuePair<String, ITerminalPresenter>(
                "A plus B", 
                new APlusBPresenter(this)),
            new KeyValuePair<String, ITerminalPresenter>(
                "Just empty selector", 
                new SelectOptionPresenter(this, 
                    "Selector with no selection choices", 
                    Enumerable
                        .Empty<KeyValuePair<String, ITerminalPresenter>>()
                        .ToArray()))
        };

        var topPresenter = new SelectOptionPresenter(this, "Select the option and press the confirm button",  nextLevelPresenters);

        this.globalTerminalPresenter = new GlobalTerminalPresenter(topPresenter);
    }

      

The PS1: . These code snippets assume you have a form named MainForm that has two buttons - button1, button2, one combobox, two text boxes - textBox_Input, textBox_Output.

PS2: The template used is close enough to Model-View-Presenter , without DataBindings .

PS3 . You can create a more or less generic end document feeder if you change the code APlusBPresenter

. Or try to shape classes and interfaces ChainXxxx...

.

PS4: And sorry about those code walls. This is probably too much for the [SO] format, so I have implemented an ad-hoc proof of concept on GitHub - https://github.com/Podskal/StackOverflow_29870164.git . This is ugly in many respects, but be that as it may, it may at least give few ideas on how to implement your own system.

PS5: There are many bottlenecks in this code, so you should consider very carefully how you will create your own system.

+3


source







All Articles