if用radiobuttons进行重构。

发布于 2025-01-20 23:15:37 字数 916 浏览 0 评论 0原文

有两个无线电按钮可以给出不同的价格。 示例:选择Elektric2和3天的租金。电动自行车= 200欧元 +每天30欧元(200 EU +(30*3))。

我该如何重构,因为我对所有选项都有60多行代码。

这是整个代码的一些:

if (radioButtonElectric2.Checked && radioButtonADay.Checked)
{
    electric2 = electric2 + 1 * 30;
    labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
else if (radioButtonElectric2.Checked && radioButtonThreeDays.Checked)
{
    electric2 = electric2 + 3 * 30;
    labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
else if (radioButtonElectric2.Checked && radioButtonSevenDays.Checked)
{
    electric2 = electric2 + 7 * 30;
    labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
else if (radioButtonElectrical2.Checked && radioButtonFourteenDays.Checked)
{
    electric2 = electric2 + 14 * 30;
    labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}

There are two radio buttons which give different prices.
Example: elektric2 is selected and 3 days rent. Electric bike = 200 euro + 30 euro per day (200 eu + (30*3)).

How can I refactor this because I have 60+ lines of code for all the options.

Here is a bit of the entire code:

if (radioButtonElectric2.Checked && radioButtonADay.Checked)
{
    electric2 = electric2 + 1 * 30;
    labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
else if (radioButtonElectric2.Checked && radioButtonThreeDays.Checked)
{
    electric2 = electric2 + 3 * 30;
    labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
else if (radioButtonElectric2.Checked && radioButtonSevenDays.Checked)
{
    electric2 = electric2 + 7 * 30;
    labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
else if (radioButtonElectrical2.Checked && radioButtonFourteenDays.Checked)
{
    electric2 = electric2 + 14 * 30;
    labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

刘备忘录 2025-01-27 23:15:37

您的方程式基本上归结为三分。

  1. 基本价格
  2. 租赁天

将这三件事分为单个功能使您的代码变得更加简单。

public decimal GetBasePrice()
{
    return 
        radioButtonElectric1.Checked ? 60 :
        radioButtonElectric2.Checked ? 80 :
        radioButtonGas1.Checked ? 55 :
        0;
}
public int GetRentingDays()
{
    return
        radioButtonADay.Checked ? 1 :
        radioButtonThreeDays.Checked ? 3 :
        radioButtonSevenDays.Checked ? 7 :
        radioButtonFourteenDays.Checked ? 14 :
        0;
}
public decimal GetDailyRate()
{
    return 
        radioButtonElectric1.Checked ? 20 :
        radioButtonElectric2.Checked ? 30 :
        radioButtonGas1.Checked ? 25 :
        0;
}

最后,最终代码要简单得多:

decimal subTotal = GetBasePrice() + GetRentingDays() * GetDailyRate();

Your equation basically boils down to three amounts.

  1. Base Price
  2. Renting Days
  3. Daily Rate

Splitting these three things into individual functions makes your code much simpler.

public decimal GetBasePrice()
{
    return 
        radioButtonElectric1.Checked ? 60 :
        radioButtonElectric2.Checked ? 80 :
        radioButtonGas1.Checked ? 55 :
        0;
}
public int GetRentingDays()
{
    return
        radioButtonADay.Checked ? 1 :
        radioButtonThreeDays.Checked ? 3 :
        radioButtonSevenDays.Checked ? 7 :
        radioButtonFourteenDays.Checked ? 14 :
        0;
}
public decimal GetDailyRate()
{
    return 
        radioButtonElectric1.Checked ? 20 :
        radioButtonElectric2.Checked ? 30 :
        radioButtonGas1.Checked ? 25 :
        0;
}

In the end the final code is much simpler:

decimal subTotal = GetBasePrice() + GetRentingDays() * GetDailyRate();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文