private void submitAddNewQuoteBtn_Click(object sender, EventArgs e) { try { if (nameInput.Text == "" || surfaceInput.SelectedItem == null || timeInput.SelectedItem == null) { throw new ArgumentNullException(); } } catch (ArgumentNullException f) { MessageBox.Show("Fields cannot be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } widthInput.ReadOnly = true; depthInput.ReadOnly = true; drawersInput.ReadOnly = true; surfaceInput.Enabled = false; timeInput.Enabled = false; nameInput.ReadOnly = true; decimal width = widthInput.Value; decimal depth = depthInput.Value; int drawers = (int)drawersInput.Value; Desk.SurfaceMaterial deskMaterial = (Desk.SurfaceMaterial)surfaceInput.SelectedItem; DeskQuote.Delivery shipping = (DeskQuote.Delivery)timeInput.SelectedItem; string name = nameInput.Text; DateTime date = DateTime.Today; Desk desk = new Desk(width, depth, drawers, deskMaterial); DeskQuote quote = new DeskQuote(desk, shipping, name, date); priceOutput.Text = quote.Price.ToString(); priceOutput.Visible = true; priceLabel.Visible = true; string filepath = "quotes.json"; if (File.Exists(filepath)) { string text = File.ReadAllText(filepath); var list = JsonConvert.DeserializeObject <List <DeskQuote> >(text); list.Add(quote); var convertedJson = JsonConvert.SerializeObject(list, Formatting.Indented); File.WriteAllText(filepath, convertedJson); } else { List <DeskQuote> list = new List <DeskQuote>(); list.Add(quote); var convertedJson = JsonConvert.SerializeObject(list, Formatting.Indented); File.WriteAllText(filepath, convertedJson); } }
private void submitAddNewQuoteBtn_Click(object sender, EventArgs e) { try { if (nameInput.Text == "" || surfaceInput.SelectedItem == null || timeInput.SelectedItem == null) { throw new ArgumentNullException(); } } catch (ArgumentNullException f) { MessageBox.Show("Fields cannot be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } widthInput.ReadOnly = true; depthInput.ReadOnly = true; drawersInput.ReadOnly = true; surfaceInput.Enabled = false; timeInput.Enabled = false; nameInput.ReadOnly = true; decimal width = widthInput.Value; decimal depth = depthInput.Value; int drawers = (int)drawersInput.Value; Desk.SurfaceMaterial deskMaterial = (Desk.SurfaceMaterial)surfaceInput.SelectedItem; DeskQuote.Delivery shipping = (DeskQuote.Delivery)timeInput.SelectedItem; string name = nameInput.Text; DateTime date = new DateTime(); Desk desk = new Desk(width, depth, drawers, deskMaterial); DeskQuote quote = new DeskQuote(desk, shipping, name, date); decimal price = quote.GetQuote(desk, quote); priceOutput.Text = price.ToString(); priceOutput.Visible = true; priceLabel.Visible = true; TextWriter file = new StreamWriter(@"../quotes.txt", true); file.WriteLine(quote.Shipping + "," + quote.Customer + "," + quote.Date + "," + desk.Width + "," + desk.Depth + "," + desk.Drawers + "," + desk.Material + "," + price); file.Close(); }