示例#1
0
        private void Btn_submit_Click(object sender, EventArgs e)
        {
            var temp_desk = new Desk()
            {
                width          = Convert.ToInt32(deskWidth.Value),
                depth          = Convert.ToInt32(deskDepth.Value),
                numberOfDrawer = (int)numDrawers.Value,
                desktop        = (Material)MaterialsCombo.SelectedValue
            };

            var deskQuote = new DeskQuote
            {
                desk         = temp_desk,
                customerName = txtcustomerName.Text,
                quoteDate    = DateTime.Now,
                shipping     = (Shipping)comShipping.SelectedValue,
                quotePrice   = 0
            };

            deskQuote.setPrice();
            var quotesFile = @"quotes.json";
            List <DeskQuote> deskquotes = new List <DeskQuote>();

            //read existing quotes if any
            if (File.Exists(quotesFile))
            {
                using (StreamReader reader = new StreamReader(quotesFile))
                {
                    //load existing quotes
                    string quotes = reader.ReadToEnd();

                    if (quotes.Length > 0)
                    {
                        // deserialization time!
                        deskquotes = JsonConvert.DeserializeObject <List <DeskQuote> >(File.ReadAllText(quotesFile)) ?? new List <DeskQuote>();
                    }
                }
            }
            deskquotes.Add(deskQuote);
            string jsonDesks = JsonConvert.SerializeObject(deskquotes);

            File.WriteAllText(quotesFile, jsonDesks);

            //var newQuoteString = deskquotes[2];
            //MessageBox.Show(newQuoteString);

            DisplayQuote displayQuote = new DisplayQuote(deskQuote);

            displayQuote.Show();
        }