Home > Software design >  Is there a simpler way code the radio buttons in this nested if-else to get the equation to work? Do
Is there a simpler way code the radio buttons in this nested if-else to get the equation to work? Do

Time:04-25

In the form i created the user is able to select a workshop (index) in a listbox and choose a city (radiobutton) to get the total cost the workshop. They can choose any combination but it can only be one item from the list and one radio button.

So I got the first part to work (if (index == 0)) with the equation. Each radio button gave the correct cost output for the first list item, however after that everything keeps outputting the same results.

My error at the beginning isn't working but maybe i have too many statements making it redundant or canceling out the correct statement. I can say i didn't have any errors anywhere but i know there is something missing or setup wrong somewhere once the (index == 1) starts

private void Costbutton_Click(object sender, EventArgs e) {

        int fee;
        int days;
        int lodging;

        int cost = 0;

        int index;
        index = workshoplistBox.SelectedIndex;


        if (index != -1)
        {
            workshoplistBox.SelectedIndex.ToString("");
            MessageBox.Show("Error! Select a Workshop.");
        
            if (index == 0 || AustinradioButton.Checked || ChicagoradioButton.Checked || DallasradioButton.Checked || OrlandoradioButton.Checked)
            {
                fee = 595;

                if (AustinradioButton.Checked)
                {
                    lodging = 95;
                    days = 3;
                    cost = fee   (days * lodging);
                }
                else if (ChicagoradioButton.Checked)
                {
                    lodging = 125;
                    days = 3;
                    cost = fee   (days * lodging);
                }
                else if (DallasradioButton.Checked)
                {
                    lodging = 110;
                    days = 3;
                    cost = fee   (days * lodging);
                }
                else if (OrlandoradioButton.Checked)
                {
                    lodging = 100;
                    days = 3;
                    cost = fee   (days * lodging);
                }
            }
            else if (index == 1 || AustinradioButton.Checked || ChicagoradioButton.Checked || DallasradioButton.Checked || OrlandoradioButton.Checked)
            {
                fee = 695;

                if (AustinradioButton.Checked)
                {
                    lodging = 95;
                    days = 3;
                    cost = fee   (days * lodging);
                }
                else if (ChicagoradioButton.Checked)
                {
                    lodging = 125;
                    days = 3;
                    cost = fee   (days * lodging);
                }
                else if (DallasradioButton.Checked)
                {
                    lodging = 110;
                    days = 3;
                    cost = fee   (days * lodging);
                }
                else if (OrlandoradioButton.Checked)
                {
                    lodging = 100;
                    days = 3;
                    cost = fee   (days * lodging);
                }
            }
            else if (index == 2 || AustinradioButton.Checked || ChicagoradioButton.Checked || DallasradioButton.Checked || OrlandoradioButton.Checked)
            {
                fee = 995;

                if (AustinradioButton.Checked)
                {
                    lodging = 95;
                    days = 3;
                    cost = fee   (days * lodging);
                }
                else if (ChicagoradioButton.Checked)
                {
                    lodging = 125;
                    days = 3;
                    cost = fee   (days * lodging);
                }
                else if (DallasradioButton.Checked)
                {
                    lodging = 110;
                    days = 3;
                    cost = fee   (days * lodging);
                }
                else if (OrlandoradioButton.Checked)
                {
                    lodging = 100;
                    days = 3;
                    cost = fee   (days * lodging);
                }
            }
            else if (index == 3 || AustinradioButton.Checked || ChicagoradioButton.Checked || DallasradioButton.Checked || OrlandoradioButton.Checked)
            {
                fee = 1295;

                if (AustinradioButton.Checked)
                {
                    lodging = 95;
                    days = 5;
                    cost = fee   (days * lodging);
                }
                else if (ChicagoradioButton.Checked)
                {
                    lodging = 125;
                    days = 5;
                    cost = fee   (days * lodging);
                }
                else if (DallasradioButton.Checked)
                {
                    lodging = 110;
                    days = 5;
                    cost = fee   (days * lodging);
                }
                else if (OrlandoradioButton.Checked)
                {
                    lodging = 100;
                    days = 5;
                    cost = fee   (days * lodging);
                }
            }
            else if (index == 4 || AustinradioButton.Checked || ChicagoradioButton.Checked || DallasradioButton.Checked || OrlandoradioButton.Checked)
            {
                fee = 395;

                if (AustinradioButton.Checked)
                {
                    lodging = 95;
                    days = 1;
                    cost = fee   (days * lodging);
                }
                else if (ChicagoradioButton.Checked)
                {
                    lodging = 125;
                    days = 1;
                    cost = fee   (days * lodging);
                }
                else if (DallasradioButton.Checked)
                {
                    lodging = 110;
                    days = 1;
                    cost = fee   (days * lodging);
                }
                else if (OrlandoradioButton.Checked)
                {
                    lodging = 100;
                    days = 1;
                    cost = fee   (days * lodging);
                }
            }

            totalCostlabel.Text = cost.ToString("C");

        }
    }
}   }

CodePudding user response:

The following logic has each RadioButton.Tag set to an instance of

public class Item
{
    public string CityName { get; set; }
    public int Lodging { get; set; }
    public int Days { get; set; }
    public int Fee { get; set; }
    public int Cost => Fee   (Days * Lodging);
    public override string ToString() => CityName;
}

In your form (here there are enough to work with), three RadioButton in a GroupBox.

ChicagoRadioButton.Tag = new Item() { Lodging = 125, CityName = "Chicago", Days = 3};
AustinRadioButton.Tag = new Item()  { Lodging = 95, CityName = "Austin", Days = 3};
DallasRadioButton.Tag = new Item()  { Lodging = 110, CityName = "Dallas", Days = 3};

Calculate code.

private readonly Dictionary<int, int> _dictionary = new Dictionary<int, int>()
{
    [0] = 595,
    [1] = 695,
    [2] = 995,
    [3] = 1295,
    [4] = 395
};
private void CostButton_Click(object sender, EventArgs e)
{
    var selected = groupBox1.RadioButtonChecked();

    var item = (Item)selected.Tag;
    /*
     * numericUpDown1 range 0-4 to mimic workshoplistBox.SelectedIndex
     */
    item.Fee = _dictionary[(int)numericUpDown1.Value];
    
    totalCostlabel.Text = $"Total Cost: {item.Cost:C2}";

}

Language extensions.

public static class ControlExtensions
{
    public static IEnumerable<T> GetDescendants<T>(this Control control) where T : class
    {
        foreach (Control child in control.Controls)
        {
            if (child is T thisControl)
            {
                yield return (T)thisControl;
            }

            if (child.HasChildren)
            {
                foreach (T descendant in GetDescendants<T>(child))
                {
                    yield return descendant;
                }
            }
        }
    }
    public static RadioButton RadioButtonChecked(this Control control, bool @checked = true) 
        => control.GetDescendants<RadioButton>().ToList().FirstOrDefault((radioButton) 
            => radioButton.Checked == @checked);
}

Notes

  • That only enough was done to provide an alternate path to take, not a complete solution. In regards to extension methods presented, see the following.
  • Logic for index does not point to a ListBox, its simply a mock for getting the idea across.

CodePudding user response:

I like Karen's approach but I think it might be somewhat over complicated

Your list seems to drive the Fee and the Days. Let's store those infos in the list:

public record Workshop(int Fee, int DayCount){
  public override string ToString()  => $"{DayCount} day, ${Fee}";
}

Then populate the list:

workshopList.Items.Add(new Workshop(595, 3));
workshopList.Items.Add(new Workshop(695, 3));
workshopList.Items.Add(new Workshop(995, 3));
workshopList.Items.Add(new Workshop(1295, 5));
workshopList.Items.Add(new Workshop(395, 1));

The radio buttons seem to drive the lodging:

ChicagoradioButton.Tag = 125;
AustinradioButton.Tag = 95;
DallasradioButton.Tag = 110;
OrlandoradioButton.Tag = 100;

Now you can calculate the cost:

void CostButton_Click(object sender, EventArgs e){
  var rb = Controls.OfType<RadioButton>().First(r => r.Checked);
  var w = workshopListBox.SelectedItem as Workshop;

  var cost = w.Fee   ((int)rb.Tag * w.DayCount);

}

Preselect an item in the list box and a radio button to save time on writing validation code that checks if a selection is made.

The code above uses a record which is a compact way of declaring a class with properties; a constructor and equality overrides are also written for you.

If your c# doesn't have record you can do the same with a class that has properties for Fee and DayCount. The ToString override is used by listbox to drive what text is shown

  • Related