【问题标题】:Invoice pricing changes based on user input根据用户输入更改发票定价
【发布时间】:2019-03-03 23:41:26
【问题描述】:

这是我的车库计算器发票,它允许用户输入尺寸/油漆/修剪。我的问题出在发票上,我不知道如何根据所选的油漆/装饰更改定价。

if (trimPrice == 1 || trimPrice == 2 || trimPrice == 3) {
 if (paintPrice == 1 || paintPrice == 2 || paintPrice == 3) {
  Console.WriteLine("Siding Invoice");
  Console.WriteLine("==================");
  Console.WriteLine(gSide1 + "   Siding Boxes  " + " @" + paintPrice + "=  " + "{0:C}", gTotal1);
  Console.WriteLine(tgTotal2 + "  Trim pieces " + "    @" + trimPrice + "=  " + "{0:C}", tgCost);
  Console.WriteLine("                   Net Total    =  " + ("{0:C}"), nTotal);
  Console.WriteLine("                   GST          =  " + ("{0:C}"), GST);
  Console.WriteLine("                   Delivery Fee =  $250.00");
  Console.WriteLine("                   Total        =  " + ("{0:C}"), Total);
  Console.WriteLine("Press Y to Redo");
  Console.Write("Option ");
 }
}

修剪颜色/价格

白色 $28.35

蓝色 $41.65

红色 $49.25

【问题讨论】:

    标签: c# menu invoice


    【解决方案1】:

    希望我正确理解了您的问题。你需要重新计算你的价值观。例如,

     if (trimPrice >=1 && trimPrice <= 3 && paintPrice>=1 && paintPrice<=3)
     {
    
            gTotal1 =  // Your logic here for gTotal1
            tgCost = // Your logic here for tgCost
            nTotal = // Your logic here for nTotal
            GST = // Your logic here for GST
            Total = // Your logic here for Total 
            Console.WriteLine("Siding Invoice");
            Console.WriteLine("==================");
            Console.WriteLine(gSide1 + "   Siding Boxes  " + " @" + paintPrice + "=  " + "{0:C}", gTotal1);
            Console.WriteLine(tgTotal2 + "  Trim pieces " + "    @" + trimPrice + "=  " + "{0:C}", tgCost);
            Console.WriteLine("                   Net Total    =  " + ("{0:C}"), nTotal);
            Console.WriteLine("                   GST          =  " + ("{0:C}"), GST);
            Console.WriteLine("                   Delivery Fee =  $250.00");
            Console.WriteLine("                   Total        =  " + ("{0:C}"), Total);
            Console.WriteLine("Press Y to Redo");
            Console.Write("Option ");
    
        }
    

    您可以保留油漆和修剪的价格字典。例如,

    var dictionaryPaint = new Dictionary<int,int>
                {
                    [1] = 100,
                    [2] = 200,
                    [3] = 300,
                };
    

    我在这里使用数字进行绘制,就像在 OP 中一样,但更好的解决方案是使用 Enums。声明价格字典后,您可以使用以下内容。

    gTotal1 = dictionaryPaint[1] * 12; //12 is just an example. You can use your logic here.
    

    完整代码

    var dictionaryPaint = new Dictionary<int,int>
    {
        [1] = 100,
        [2] = 200,
        [3] = 300,
    };
    
    var dictionaryTrim = new Dictionary<string,int>
    {
       [1] = 110,
       [2] = 210,
       [3] = 310,
    };
    if (trimPrice >=1 && trimPrice <= 3 && paintPrice>=1 && paintPrice<=3)
    {
    
                gTotal1 = dictionaryPaint[1] * 12;  // Your logic here for gTotal1
                tgCost = // Your logic here for tgCost
                nTotal = // Your logic here for nTotal
                GST = // Your logic here for GST
                Total = // Your logic here for Total 
                Console.WriteLine("Siding Invoice");
                Console.WriteLine("==================");
                Console.WriteLine(gSide1 + "   Siding Boxes  " + " @" + paintPrice + "=  " + "{0:C}", gTotal1);
                Console.WriteLine(tgTotal2 + "  Trim pieces " + "    @" + trimPrice + "=  " + "{0:C}", tgCost);
                Console.WriteLine("                   Net Total    =  " + ("{0:C}"), nTotal);
                Console.WriteLine("                   GST          =  " + ("{0:C}"), GST);
                Console.WriteLine("                   Delivery Fee =  $250.00");
                Console.WriteLine("                   Total        =  " + ("{0:C}"), Total);
                Console.WriteLine("Press Y to Redo");
                Console.Write("Option ");
    
    
    
    
    }
    

    顺便说一句,请注意你可以替换

    if (trimPrice == 1 || trimPrice == 2 || trimPrice == 3) {
     if (paintPrice == 1 || paintPrice == 2 || paintPrice == 3) {
    

    有

     if (trimPrice >=1 && trimPrice <= 3 && paintPrice>=1 && paintPrice<=3)
    

    【讨论】:

    • 是的,我需要重新计算输入油漆/装饰的总成本。例如,他们可以选择三种涂料(白色、红色和蓝色),每种涂料的成本都不同。一旦他们选择了一个,总成本将根据他们的选择而变化
    • 因此,您可以根据油漆价格计算它们。例如,Total 可以是 Total = Paint Price * AreaToPaint
    • 那么其他两种油漆选项呢?因为如果他们选择三种颜色中的一种,那么总成本会发生变化。
    • @SH093 我想我现在更好地理解了你的问题。请检查更新的答案。
    【解决方案2】:

    假设您的价格在您的应用程序运行时不会发生变化,您可以创建一个静态的Dictionary 来根据装饰类型或油漆类型标识符存储价格。

    static readonly Dictionary<int, int> TrimPrices = new Dictionary<int, int>
    {
        { 1, 100 },
        { 2, 200 },
        { 3, 500 }
    };
    

    然后您可以访问并打印所选装饰的价格,如下所示:

    Console.WriteLine(TrimPrices[trimPrice]);
    

    【讨论】:

    • 每种油漆/装饰的价格不同。我的问题是客户选择的最终总成本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 2012-09-12
    • 2015-06-22
    • 1970-01-01
    • 2018-06-09
    • 2021-03-21
    相关资源
    最近更新 更多