Exemple #1
0
        private void DisplayQuote_Load(object sender, EventArgs e)
        {
            customerQuote = Program.Quotes.GetLatest();

            /* string[] prices = File.ReadAllLines(@"RushOrderPrices.txt");
             * testRushOrder.Text = "";
             * foreach (string rushprice in prices)
             * {
             *
             *   // Use a tab to indent each line of the file.
             *   testRushOrder.Text += "\t" + rushprice + " ";
             * }*/
            try
            {
                int Area  = customerQuote.GetWidth() * customerQuote.GetDepth();
                int rDays = customerQuote.GetRushDays();
                OrderDateLabel.Text         = customerQuote.GetDate();
                ResultsCustomerName.Text    = customerQuote.GetName();
                ResultsRushDays.Text        = customerQuote.GetRushDays().ToString();
                ResultsWidth.Text           = customerQuote.GetWidth().ToString();
                ResultsDepth.Text           = customerQuote.GetDepth().ToString();
                ResultsTotalPrice.Text      = customerQuote.GetPrice().ToString();
                ResultsNumDrawers.Text      = customerQuote.GetNumDrawers().ToString();
                ResultsSurfaceMaterial.Text = customerQuote.GetMaterial().ToString();
                ResultRushOrder.Text        = customerQuote.GetRushOrder(rDays, Area).ToString();
            }
            catch
            {
                ResultsCustomerName.Text    = "";
                ResultsRushDays.Text        = "";
                ResultsWidth.Text           = "";
                ResultsDepth.Text           = "";
                ResultsNumDrawers.Text      = "";
                ResultsSurfaceMaterial.Text = "";
                ResultsTotalPrice.Text      = "Problem calculating total price.";
            }
        }
        private void CreateQuoteButton_Click(object sender, EventArgs e)
        {
            customerNameValue    = CustomerName.Text;
            rushDaysValue        = Convert.ToInt32(RushDays.Text);
            widthValue           = (int)Width.Value;
            depthValue           = (int)Depth.Value;
            numDrawersValue      = (int)NumDrawers.Value;
            surfaceMaterialValue = SurfaceMaterial.Text;

            currentDate = DateTime.Now;
            // saved currentdate in string so the format is nice for the JSON file
            string orderDate = currentDate.ToString("dd MMMM yyyy");
            //rushCostValue = ;

            Desk      customerDesk  = new Desk(widthValue, depthValue, numDrawersValue, surfaceMaterialValue);
            DeskQuote customerQuote = new DeskQuote(customerNameValue, customerDesk, rushDaysValue, orderDate);

            Program.Quotes.Add(customerQuote);

            //private static Desk customerDesk = new Desk(widthValue, depthValue, numDrawersValue, surfaceMaterialValue);
            //private static DeskQuote customerQuote = new DeskQuote(customerNameValue, customerDesk, rushDaysValue);

            //  Saving JSON to text file
            #region Save Quotes to JSON file / serialize/deserialize
            var json = "";
            try
            {
                json = JsonConvert.SerializeObject(Program.Quotes);

                string           fileName   = @"quotes.json";
                List <DeskQuote> deskOrders = new List <DeskQuote>();

                if (File.Exists(fileName))
                {
                    using (StreamReader reader = new StreamReader(fileName))
                    {
                        string quotes = reader.ReadToEnd();
                        if (quotes.Length > 0)
                        {
                            deskOrders = JsonConvert.DeserializeObject <List <DeskQuote> >(quotes);
                        }
                        deskOrders.Add(customerQuote);
                    }

                    // convert to Json
                    var serializedOrders = JsonConvert.SerializeObject(deskOrders);
                    // save to json
                    File.WriteAllText(fileName, serializedOrders);
                }
                else
                {
                    deskOrders = new List <DeskQuote> {
                        customerQuote
                    };
                    var serializedOrders = JsonConvert.SerializeObject(deskOrders);
                    // save to json
                    File.WriteAllText(fileName, serializedOrders);
                }
                #endregion

                //File.WriteAllText(@"quotes.json", JsonConvert.SerializeObject(Program.Quotes));
                // using (StreamWriter x = File.WriteAllText(fileName))
                //{
                //   x.WriteLine(json);
                // }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "Error writing to file.");
            }



            DisplayQuote viewDisplayQuote = new DisplayQuote();

            viewDisplayQuote.Tag = this.Tag;
            viewDisplayQuote.Show();
            Hide();
        }
 public void Add(DeskQuote quote)
 {
     quotes.Add(quote);
 }