Изменения цены счета в зависимости от ввода пользователя - PullRequest
0 голосов
/ 04 марта 2019

Это мой счет за гаражный калькулятор, где он позволяет пользователю вводить размер / краску / отделку.Моя проблема в счете, я не знаю, как изменить цену в зависимости от выбранной краски / отделки.

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

Ответы [ 2 ]

0 голосов
/ 04 марта 2019

Если ваши цены не изменятся во время работы приложения, вы можете создать статический словарь для хранения цен по идентификаторам типа отделки или типа краски.

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

Затем вы можетеполучить доступ и распечатать цену выбранной отделки следующим образом:

Console.WriteLine(TrimPrices[trimPrice]);
0 голосов
/ 04 марта 2019

Надеюсь, я правильно понял ваш вопрос.Вам нужно пересчитать свои ценности.Например,

 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 ");

    }

Вы можете сохранить словарь цен для Paint и Trim.Например,

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...