在程序中遇到麻烦

时间:2014-04-15 05:12:46

标签: c# linq math

当我编写论坛时,我在区分csv文件中的两位数信息时遇到了麻烦。

问题是:如果,有2位成年人想要去一个地方,价格=更低 然而,它是一个单身的成年人,价格将等于更多。

问题:出现的问题是,我的所有细分受众群都认为所有成年人都应该收取更少的费用,而某些交易费用应显示更多。

确切信息:包含HolidayTran.CSV的文件包含数组[3],其中包含聚会中1或2名成人的信息。

当我携带功能参考时,双人成人是我带到顶端。

已编辑 - 这是我的大学教授想要的方法 ...是慢而愚蠢的,但是他的练习决赛,所以我想弄清楚我错过了什么。 我理解我想做自己的工作,但我希望有人可以告诉我编程中的数学错误。

编辑#2 更改了变量以帮助更清晰。我找到了我方程中位于If循环部分的孤立问题。它已经将所有内容乘以两位而不是1.我如何创建一个函数,如果消息读取1或2,那么正确的数学运算会适用?

AdultPricing 此功能假设选择正确的数学,但我倾向于失去如何正确完成该功能的概念。如果你看看If循环,你可以看到我出错的地方......任何想法的人?

我正在尝试使用if语句bool函数,但目前无法正常工作...我不知道还有什么需要atm ..

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;
using System.IO;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string[] myfile = File.ReadAllLines(@"C:\temp\customerinfo.csv");
        var myquery = from mylines in myfile
                      let myfield = mylines.Split(',')
                      let names = myfield[1]
                      let lastname = myfield[2]
                      let id = myfield[0]
                      orderby lastname, names
                      select new { id, names, lastname };
        foreach (var listing in myquery) { cmbcustomerinfo.Items.Add(listing.id + " " + listing.names + " " + listing.lastname); }
    }

    private void cmbcustomerinfo_SelectedIndexChanged(object sender, EventArgs e)
    {

        //recalling all private void information at the top of the combobox file, inorder to send information to the listbox.
        string tempvariable = "";
        string iddvariable = "";
        string format1 = "{0,55}{1,5}";
        string format2 = "{0,-5:d}{1,15:d}{2,20:c}{3,20:c}";
        string format3 = "{0,-15}{1,35:c}{2,20:c}";
        string format4 = "{0,-15}{1,72:c}";
        getCustomerIDFirstName(out tempvariable, out iddvariable);
        getcustomerinfo(iddvariable);


        //set the required information to connect to the Holiday Transaction. Where we can connect if the ID found in Holiday Matchs the ID in CustomerInfo.CSV,
        //then we can show the data of the dates and pricing of the information
        string[] transaction = File.ReadAllLines(@"C:\temp\HolidayTrans.csv");
        var TransactionQuery = from myLinesshown in transaction
                               let myfield2 = myLinesshown.Split(',')
                               let customerid = myfield2[0]
                               let datestart = myfield2[1]
                               let numofadults = byte.Parse(myfield2[2])
                               let numofkids = byte.Parse(myfield2[3])
                               where customerid == iddvariable
                               orderby datestart, customerid
                               select new
                               {
                                   customerid,
                                   datestart,
                                   numofadults,
                                   numofkids
                               };





        foreach (var staff1 in TransactionQuery)
        {

            lstInvoice.Items.Clear();
            lstInvoice.Items.Add("Purchase Date     EndDate       Adult Price   Kid Price");

            //set up all variables used or to be used.
            string idgiven = "";
            double KidsSubPricing = 0;
            double AdultPricing = 0;
            double singleddigit = 0;
            double TwinAdultPricing = 0;
            double totaladult = 0;
            double subtotal = 0;
            double subtotal1 = 0;
            double kidpricing = 3300;
            byte NumberOfDaysSpent = 0;
            string EndofDays = "";
            DateTime daybegin;
            DateTime startthedate;
            DateTime datebeginning = DateTime.Now;

            //set the basic functionality to find the proper ID and date to be shown that corresponses to the person. 
            foreach (var transactionfound in TransactionQuery)
            {
                idgiven = transactionfound.customerid;
                datebeginning = DateTime.Parse(transactionfound.datestart); 
                break;
            }
            //set the datetime interval to show the proper grouping later on
           int xyy = datebeginning.Year; 

            //This is suppose to show where and how I can seperate the transaction of single and double pricing. 
            foreach (var transactionfound in TransactionQuery)
            {
                   if (transactionfound.numofadults.ToString().Contains("1"))
                    {
                        singleddigit = transactionfound.numofadults;

                    }
                   if (transactionfound.numofadults.ToString().Contains("2"))
                    {
                        TwinAdultPricing = transactionfound.numofadults;

                    }
                if (transactionfound.customerid == idgiven && DateTime.Parse(transactionfound.datestart).Year == xyy)
                {

                    getpackagepriceinfo(transactionfound.datestart,ref singleddigit, ref TwinAdultPricing,  ref NumberOfDaysSpent);
                    KidsSubPricing = transactionfound.numofkids * kidpricing;
                    //AdultPricing = transactionfound.numofadults * TwinAdultPricing;
                    subtotal += KidsSubPricing;
                    subtotal1 += AdultPricing;
                    daybegin = DateTime.Parse(transactionfound.datestart);
                    startthedate = daybegin.AddDays(NumberOfDaysSpent);
                    EndofDays = startthedate.ToString("d");
                    lstInvoice.Items.Add(string.Format(format2, daybegin, EndofDays, AdultPricing, KidsSubPricing));

                }
                else
                {
                    lstInvoice.Items.Add(string.Format(format3, "Subtotal Amount",subtotal1, subtotal));
                    lstInvoice.Items.Add(" ");
                    getpackagepriceinfo(transactionfound.datestart,ref singleddigit,  ref TwinAdultPricing, ref NumberOfDaysSpent);
                    KidsSubPricing = transactionfound.numofkids * kidpricing;
                    //AdultPricing = transactionfound.numofadults * singleddigit;
                    subtotal += KidsSubPricing;
                    subtotal1 += AdultPricing;
                    daybegin = DateTime.Parse(transactionfound.datestart);
                    startthedate = daybegin.AddDays(NumberOfDaysSpent);
                    EndofDays = startthedate.ToString("d");
                    lstInvoice.Items.Add(string.Format(format2, daybegin, EndofDays, AdultPricing, KidsSubPricing));
                    idgiven = transactionfound.customerid;
                    xyy = DateTime.Parse(transactionfound.datestart).Year;
                }
                if (idgiven == "")
                {
                    lstInvoice.Items.Clear();
                    lstInvoice.Items.Add(string.Format(format1, "Sorry no Transaction Found For" + " ", tempvariable));
                }
                //else
                //{
                //    lstInvoice.Items.Add(string.Format(format3, "Subtotal Amount", subtotal1, subtotal));
                //    lstInvoice.Items.Add("");
                //}
            }lstInvoice.Items.Add(string.Format(format3, "Subtotal Amount", subtotal1, subtotal));
        }

    }


    private void getCustomerIDFirstName(out string tempp, out string idd)
    {
        string[] temp = cmbcustomerinfo.SelectedItem.ToString().Split(' ');
        tempp = temp[1];
        idd = temp[0];
    }

    private void getpackagepriceinfo(string date, ref double CostPerSingleAdult, ref double CostPerTwoAdults, ref byte numofdays1)
    {
        //set the information here, so we can recall the csv file into the main program.
        string[] production = File.ReadAllLines(@"C:\temp\PackagePrice.csv");
        var productQuery = from myLinesshown in production
                           let myfield1 = myLinesshown.Split(',')
                           let numofdays = byte.Parse(myfield1[0])
                           let startdateshown = myfield1[1]
                           let twinadult = myfield1[2]
                           let singlepricing = myfield1[3]
                           where startdateshown == date
                           select new
                           {
                              numofdays,
                              startdateshown,
                              twinadult,
                              singlepricing
                           };

        //setting the factor of the private function doubles and bytes to be able to get recalled, back to the top.
        foreach (var xyz in productQuery)
        {
            numofdays1 = xyz.numofdays;
            CostPerTwoAdults = double.Parse(xyz.twinadult);
            CostPerSingleAdult = double.Parse(xyz.singlepricing);
            date = xyz.startdateshown;
            break;
        }

    }
    //redo the customer information, so we can recall the string of customer id inorder to recall proper functionality. 
    private void getcustomerinfo(string customerid){
            string[] myfile = File.ReadAllLines(@"C:\temp\customerinfo.csv");
             var myquery = from mylines in myfile
                      let myfield = mylines.Split(',')
                      let names = myfield[1]
                      let lastname = myfield[2]
                      let id = myfield[0]
                      where id == customerid
                      select new { id, names, lastname};}

}

}

2 个答案:

答案 0 :(得分:0)

修复变量名称。

将您的代码重构为更小更整洁的方法。

问题变为 MUCH 更清晰

您有一个名为getpackagepriceinfo的方法,它接受参数

(string date, ref double adult, ref double single12, ref byte numofdays1)

成人为costPerSingleAdult,而单身12为costPerTwoAdults

调用此方法时,您已将变量singleddigit传递给现在重命名的参数costPerTwoAdults。那有什么意思?您是否意识到尚未在您的代码中使用此变量

首先放置代码清晰度。当代码易于理解和正常工作时,您可以开始重写速度/内存/减少LoC /实验语言功能/其他任何原因的部分,当你这样做写一个注释显示代码的初衷,以便当一个bug写入您可以快速找到的新代码。

编辑后

这绝对是一种改进。您现在可以看到,您已经获得了每笔交易的成人人数,但您已将此号码分配到用于定价的变量中。我认为你需要回顾一下我所读到的规范,如果有#2;如果有2个成人和2个成人价格,则收取双胞胎价格。否则,如果没有两个成人价格2 *单身成人价格,否则收取成人价格"你的代码应该是相同的。

Decimal adultPrice
if ( twoAdults && twinPrice > 0)
    adultPrice = twinPrice;
else if ( twoAdults )
    adultPrice = 2 * singlePrice;
else
    adultPrice = singlePrice;

请注意,如果有超过2名成年人,这不会起作用。不确定是否符合规范

答案 1 :(得分:0)

所以新的编码如下。感谢 James Barrass ,我能够正确地修复编码工作。问题是以下


    numberAdults = byte.Parse(Transaction.NumberofAdults);

                    if(numberAdults == 2)
                        adultpricing = AdultCost*2;
                    else
                        adultpricing = CostSingle;

由于上面的代码,整个编码现在能够正常工作。


   private void Form1_Load(object sender, EventArgs e)
    {
        string[] myfile = File.ReadAllLines(@"C:\temp\customerinfo.csv");
        var myQuery = from mylines in myfile
                      let myfield = mylines.Split(',')
                      let CustomerID = myfield[0]
                      let CustomerFirstName = myfield[1]
                      let CustomerLastName = myfield[2]
                      orderby CustomerID, CustomerLastName, CustomerFirstName
                      select new { 
                      CustomerID, CustomerFirstName, CustomerLastName
                      };
        foreach (var customerinfo in myQuery) { cmbCustomer.Items.Add(customerinfo.CustomerID + " " + customerinfo.CustomerFirstName + " " + customerinfo.CustomerLastName); }

    }


    private void getCustomerFirstandID(out string customerfirst, out string idd)
    {
        string[] tempp = cmbCustomer.SelectedItem.ToString().Split(' ');
        customerfirst = tempp[1];
        idd = tempp[0];       
    }
    private void getCustomerInfo(string StatedID)
    {
        string[] myfile = File.ReadAllLines(@"C:\temp\customerinfo.csv");
        var myQuery = from mylines in myfile
                      let myfield = mylines.Split(',')
                      let CustomerID = myfield[0]
                      let CustomerFirstName = myfield[1]
                      let CustomerLastName = myfield[2]
                      where CustomerID == StatedID
                      select new
                      {
                          CustomerID,
                          CustomerFirstName,
                          CustomerLastName
                      };


    }
    private void getPackagePriceInfo(DateTime date, ref double CostofAdults, ref double CostofSingle, ref byte NumberofDaysShown)
    {
        string[] myGivenFile = File.ReadAllLines(@"C:\temp\PackagePrice.csv");
        var myPackageTransaction = from myLinesGiven in myGivenFile
                                   let myFieldShown = myLinesGiven.Split(',')
                                   let NumberofDays = myFieldShown[0]
                                   let StartDate = DateTime.Parse(myFieldShown[1])
                                   let TwinAdult = myFieldShown[2]
                                   let SingleAdult = myFieldShown[3]
                                   where StartDate == date
                                   select new { 
                                   NumberofDays, StartDate, TwinAdult, SingleAdult

                                   };
        foreach (var Package in myPackageTransaction) {
            CostofSingle = double.Parse(Package.SingleAdult);
            CostofAdults = double.Parse(Package.TwinAdult);
            NumberofDaysShown = byte.Parse(Package.NumberofDays);
            date = Package.StartDate;
            break;
        }


    }

    private void cmbCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        string customerfirstvariable = "";
        string iddvariable = "";
        getCustomerFirstandID(out customerfirstvariable, out iddvariable);
        getCustomerInfo(iddvariable);
        string format1 = "{0,55}{1,5}";
        string format2 = "{0,-5:d}{1,15:d}{2,20:c}{3,20:c}";
        string format3 = "{0,25}{1,19:c}{2,20:c}";
        string format4 = "{0,-15}{1,72:c}";

        string[] myGivenFile1 = File.ReadAllLines(@"C:\temp\holidaytrans.csv");
        var myHolidayTransaction = from myLinesGiven1 in myGivenFile1
                                   let myFieldShown = myLinesGiven1.Split(',')
                                   let CustomerGivenID = myFieldShown[0]
                                   let PackageStartDate = DateTime.Parse(myFieldShown[1])
                                   let NumberofAdults = myFieldShown[2]
                                   let NumberofKids = myFieldShown[3]
                                   where CustomerGivenID == iddvariable
                                   orderby PackageStartDate
                                   select new
                                   {
                                      CustomerGivenID, PackageStartDate, NumberofAdults, NumberofKids

                                   };
            lstInvoice.Items.Clear();
     //   foreach (var transactionfound in myHolidayTransaction) {


    //set up all variables used or to be used.
             string EndofDays = "";
            DateTime daybegin;
            DateTime startthedate;
            DateTime datebeginning = DateTime.Now;
            string idgiven = "";
            double AdultCost = 0;
            byte DaysUsed = 0;
            double adultpricing = 0;
            double KidsPricing = 0;
            double KidsCost = 3300;
            double subtotal = 0;
            double totalamt = 0;
            double subtotal1 = 0;
            double total1 = 0;
            double total = 0;
            double CostSingle = 0;
            byte numberAdults = 0;
            double adultgiven = 0;
        int xyz = 0;
            foreach (var Transaction in myHolidayTransaction)
            {
                idgiven = Transaction.CustomerGivenID;
                lstInvoice.Items.Add("Purchase Date     EndDate          Adult Price          Kid Price");
                datebeginning = Transaction.PackageStartDate;
                xyz = datebeginning.Year;
                break;

            }


            foreach (var Transaction in myHolidayTransaction)
            {
                if (Transaction.PackageStartDate.Year == xyz) {
                    getPackagePriceInfo(Transaction.PackageStartDate,ref AdultCost,  ref  CostSingle, ref DaysUsed);
                    KidsPricing = KidsCost * byte.Parse(Transaction.NumberofKids);
                    numberAdults = byte.Parse(Transaction.NumberofAdults);

                    if(numberAdults == 2)
                        adultpricing = AdultCost*2;
                    else
                        adultpricing = CostSingle;


                    subtotal += KidsPricing;
                    subtotal1 += adultpricing;
                    total += KidsPricing;
                    total1 += adultpricing;
                    daybegin = Transaction.PackageStartDate;
                    startthedate = daybegin.AddDays(DaysUsed);
                    EndofDays = startthedate.ToString("d");
                    lstInvoice.Items.Add(string.Format(format2, startthedate, EndofDays, adultpricing, KidsPricing));}
                    else
                    {
                    lstInvoice.Items.Add(string.Format(format3, "Subtotal Amount", subtotal1, subtotal));
                    lstInvoice.Items.Add(" ");
                    getPackagePriceInfo(Transaction.PackageStartDate, ref AdultCost, ref  CostSingle, ref DaysUsed);
                    KidsPricing = KidsCost * byte.Parse(Transaction.NumberofKids);
                    numberAdults = byte.Parse(Transaction.NumberofAdults);

                    if (numberAdults == 2)
                        adultpricing = AdultCost * 2;
                    else
                        adultpricing = CostSingle;
                    subtotal = KidsPricing;
                    subtotal1 = adultpricing;
                    total += KidsPricing;
                    total1 += adultpricing;
                    daybegin =Transaction.PackageStartDate;
                    startthedate = daybegin.AddDays(DaysUsed);
                    EndofDays = startthedate.ToString("d");
                    lstInvoice.Items.Add(string.Format(format2, startthedate, EndofDays, adultpricing, KidsPricing));
                    idgiven = Transaction.CustomerGivenID;
                    xyz = Transaction.PackageStartDate.Year;

             }}

   if (idgiven == "")
                {
                lstInvoice.Items.Clear();
                lstInvoice.Items.Add(string.Format(format1, "Sorry no Transaction Found For" + " ", customerfirstvariable));
                } 
   else
                lstInvoice.Items.Add(" "); 
                lstInvoice.Items.Add(string.Format(format3, "Subtotal Amount", subtotal1, subtotal));
                lstInvoice.Items.Add(" ");
                lstInvoice.Items.Add(string.Format(format3, "Total Amount", total1, total));

        } 
                }



            }