gpt4 book ai didi

c# - 在程序中实现逻辑时,If else 的更好选择是什么? | C#

转载 作者:行者123 更新时间:2023-11-30 19:37:21 26 4
gpt4 key购买 nike

我正在构建一个自动化应用程序,它允许用户从复选框等中选择选项,并在最后显示费用的价格。

问题是我完成这个申请的方式很别扭。例如,请参阅我的 if else 语句,它们并未涵盖决策的各个方面。

例如,这是我的代码的一部分

private decimal RushesMethod(out decimal radiatorRush_var, out decimal transmissionFlush_var, out decimal both_var)
{
radiatorRush_var = 0m;
transmissionFlush_var = 0m;
both_var = 0m;

if (radiatorRushCheckBox.Checked)
{
radiatorRush_var = 30.00m;
}

else if (transmissionFlushCheckBox.Checked)
{
transmissionFlush_var = 80.00m;
}

else if (radiatorRushCheckBox.Checked && transmissionFlushCheckBox.Checked)
{
radiatorRush_var = 30.00m;
transmissionFlush_var = 80.00m;
both_var = radiatorRush_var + transmissionFlush_var;
}

return both_var + transmissionFlush_var + radiatorRush_var;


}

如果用户选择了 radiatorRushCheckBox.Checked 选项和一些其他选项,但不是这个方法,假设 oilChangeCheckBox.Checked 那么我将如何涵盖所有那个决定。为所有人制作 if else 语句太冗长了,因为谁知道用户选择了什么。

这是如果我选择了所有选项但未显示正确价格时会发生的情况。

enter image description here

这是完整的应用程序代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Automative_APP
{
public partial class automativeForm : Form
{
public automativeForm()
{
InitializeComponent();
}

private void ClearOilLube()
{

oilChangeCheckBox.CheckState = CheckState.Unchecked;
lubeJobCheckBox.CheckState = CheckState.Unchecked;
}

private void ClearFlushes()
{
radiatorRushCheckBox.CheckState = CheckState.Unchecked;
transmissionFlushCheckBox.CheckState = CheckState.Unchecked;
}

private void ClearMisc()
{
inspectionCheckBox.CheckState = CheckState.Unchecked;
replaceMufflerCheckBox.CheckState = CheckState.Unchecked;
tireRotationCheckBox.CheckState = CheckState.Unchecked;
}

private void ClearOthers()
{
partsTextBox.Text = "";
laborTextBox.Text = "";
}

private void ClearFees()
{
serviceLaborAnsLabelBox.Text = "";
partsSummaryAnsLabelBox.Text = "";
taxPartsAnsLabelBox.Text = "";
totalFeesAnsLabelBox.Text = "";
}






private decimal TotalCharges()
{
decimal rushesVar = RushesMethod();
decimal oiLAndLubeVar = OilLubeCharges();
decimal miscVar = MiscMethod();
decimal partsLaborVar = PartsLaborMethod();
decimal storeTaxCharges = TaxCharges();
decimal totalSum;
decimal totalSum1;

totalSum1 = (rushesVar + oiLAndLubeVar + miscVar);


totalSum = (rushesVar + oiLAndLubeVar + miscVar + partsLaborVar);


partsSummaryAnsLabelBox.Text = partsLaborVar.ToString();

partsSummaryAnsLabelBox.Text = partsTextBox.Text;

serviceLaborAnsLabelBox.Text = "Total Services fee is " + " " + totalFeesAnsLabelBox.Text + " " + "and Labor amount is" + " " + laborTextBox.Text;

taxPartsAnsLabelBox.Text = storeTaxCharges.ToString();

return totalSum;

}





private decimal TaxCharges()
{
const decimal PARTS_TAX_VAR = 0.6m;
decimal storeTax;
decimal taxCal;


storeTax = decimal.Parse(partsTextBox.Text);
taxCal = PARTS_TAX_VAR * storeTax;

return taxCal;


}



private decimal PartsLaborMethod()
{
decimal PL=0m;
decimal labor;
decimal totalPL = 0m;

PL = decimal.Parse(partsTextBox.Text);
labor = decimal.Parse(laborTextBox.Text);

totalPL= PL* labor;

return totalPL;
}



private decimal MiscMethod()
{
decimal valueTotal2 = 0m;

if (inspectionCheckBox.Checked && replaceMufflerCheckBox.Checked && tireRotationCheckBox.Checked)
{
valueTotal2 += (15.00m + 100.00m + 20.00m);
}

else if (inspectionCheckBox.Checked)
{
valueTotal2 += 15.00m;
}

else if (replaceMufflerCheckBox.Checked)
{
valueTotal2 += 100.00m;
}

else if (tireRotationCheckBox.Checked)
{
valueTotal2 += 20.00m;
}

return valueTotal2;


}






private decimal RushesMethod()
{
decimal valueTotal = 0m;

if (radiatorRushCheckBox.Checked)
{
valueTotal += 30.00m;
}

else if (transmissionFlushCheckBox.Checked)
{
valueTotal += 80.00m;
}

else if (radiatorRushCheckBox.Checked && transmissionFlushCheckBox.Checked)
{

valueTotal += (80.00m + 30.00m);
}

return valueTotal;


}


private decimal OilLubeCharges()
{

decimal valueTotalOL=0m;

if (oilChangeCheckBox.Checked && lubeJobCheckBox.Checked)
{
valueTotalOL += (26.00m + 18.00m);

}

else if (oilChangeCheckBox.Checked)
{
valueTotalOL += 26.00m;

}

else if (lubeJobCheckBox.Checked)
{
valueTotalOL += 18.00m;
}

return valueTotalOL;

}


private void partsSummaryLabelBox_Click(object sender, EventArgs e)
{

}

private void taxPartsLabelBox_Click(object sender, EventArgs e)
{

}

private void exitButton_Click(object sender, EventArgs e)
{
this.Close(); //close the form
}

private void calculateButton_Click(object sender, EventArgs e)
{

decimal totalStore= TotalCharges();

totalFeesAnsLabelBox.Text = totalStore.ToString();


}

private void clearButton_Click(object sender, EventArgs e)
{
ClearOilLube();
ClearFlushes();
ClearMisc();
ClearOthers();
ClearFees();
}
}
}

EDIT:

问题是这样的,

The application should have the following value-returning methods:
• OilLubeCharges —Returns the total charges for an oil change and/or a lube job, if any.
• FlushCharges —Returns the total charges for a radiator flush and/or a transmission flush, if any.
• MiscCharges —Returns the total charges for an inspection, muffler replacement, and/or a tire rotation, if any.
• OtherCharges —Returns the total charges for other services (parts and labor), if any.
• TaxCharges —Returns the amount of sales tax, if any. Sales tax is 6% and is charged only on parts. If the customer purchases services only, no sales tax is charged.
• TotalCharges —Returns the total charges.
The application should have the following void methods, called when the user clicks the Clear button:
• ClearOilLube —Clears the check boxes for oil change and lube job.
• ClearFlushes —Clears the check boxes for radiator flush and transmission flush.
• ClearMisc —Clears the check boxes for inspection, muffler replacement, and tire rotation.
• ClearOther —Clears the text boxes for parts and labor.
• ClearFees —Clears the labels that display the labels in the section marked Summary

Finally, I did it by using the @OmegaMan's solution. I wanted to post the changes so that it can help anyone

我对 TotalCharges() 方法进行了更改。比较早期的和这个。

private decimal TotalCharges()
{

decimal total = 0.0m;

if ( inspectionCheckBox.Checked)

total += 15.00m;

if (replaceMufflerCheckBox.Checked)

total += 100.00m;

if (tireRotationCheckBox.Checked)

total += 20.00m;

if (oilChangeCheckBox.Checked)

total += 26.00m;

if (lubeJobCheckBox.Checked)
total += 18.00m;

if (radiatorRushCheckBox.Checked)

total += 30.00m;

if (transmissionFlushCheckBox.Checked)

total += 80.00m;

return total;

我还更改了计算按钮点击事件处理程序:

private void calculateButton_Click(object sender, EventArgs e)
{

decimal totalStore= TotalCharges();
decimal taxCharge = TaxCharges();

totalFeesAnsLabelBox.Text = (totalStore + taxCharge).ToString();
taxPartsAnsLabelBox.Text = taxCharge.ToString();
partsSummaryAnsLabelBox.Text = partsTextBox.Text;
serviceLaborAnsLabelBox.Text = "The Total Service charges are" + totalStore + "and Labor is " + laborTextBox.Text;

其余部分相同,逻辑成功。感谢所有做出贡献的人。

最佳答案

在单独的方法中汇总和集中汇总操作,并在进行汇总时检查每个复选框的状态:

public decimal TotalCosts()
{
decimal total = 0.0m;

if ({Oil Changed Checked})
total += {Oil Cost};

if ({Transmission checked})
total += {Transmission total};

if ({Repair Clutch})
total += {Clutch Cost}; // Maybe call a separate method ClutchTotal()?

... { Do this for all check boxes }

return total;
}

不要尝试将不同的操作单独添加在一起(就像您对 both_var = radiatorRush_var + transmissionFlush_var; 所做的那样,那是您的困惑。


有限状态机

我提到了一个有限状态机逻辑,它在组织任何代码时都很有用。

想想一台自动售货机,它必须接受所有不同类型的硬币和美元钞票,并且在提供产品之前它有特定的状态。 通过映射所有状态,然后集中代码来处理每个状态,这将大大减少代码错误和可维护性。

如果向机器中添加一角硬币,状态将从 Welcome 变为汇总当前硬币和美元但不提供产品。在 total > cost 之前,该状态为真,然后 Vend 状态被触发,它会分发产品。在完成 provide any moneys if overpayment 的最后一步之前,它不会返回到 Welcome 状态。

通过设置状态,您可以组织代码来处理您在应用中看到的所有情况。

关于c# - 在程序中实现逻辑时,If else 的更好选择是什么? | C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38295730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com