//Constructor public clDiscounts(string JSONInput) { JSONDiscount JSONdsc = JsonConvert.DeserializeObject <JSONDiscount>(JSONInput); totalGross = JSONdsc.TotalGross; //Initial order amount, before discounts foreach (JSONDiscountItem JSONdscItem in JSONdsc.Discounts) //Create individual discounts { clDiscount DiscountItem = new clDiscount(JSONdscItem.Name, (JSONdscItem.Type == "Fixed amount" ? TdscType.dscFixed : TdscType.dscPercentage), JSONdscItem.Value); this.Add(DiscountItem); } }
private void button1_Click(object sender, EventArgs e) { //Preparing Input JSON. This will be normally handled by the Application Server JSONDiscount JSONInput = new JSONDiscount(); JSONInput.TotalGross = Convert.ToDouble(tbTotalAmount.Text); foreach (DataGridViewRow curRow in dataGridView1.Rows) { //Order of individual discount creation is also the priority if (curRow.Cells[0].Value == null) { continue; } if (Convert.ToBoolean(Convert.ToInt32(curRow.Cells[0].Value))) { JSONDiscountItem JSONDiscount = new JSONDiscountItem(); JSONDiscount.Name = (string)curRow.Cells[1].Value; JSONDiscount.Type = (string)curRow.Cells[2].Value; JSONDiscount.Value = Convert.ToDouble(curRow.Cells[3].Value); JSONInput.Discounts.Add(JSONDiscount); } } // --------------------------------------------------// string inputJSON = JsonConvert.SerializeObject(JSONInput, Formatting.Indented); textBox1.Text = inputJSON; clDiscounts TotalDsc = new clDiscounts(inputJSON); //New collection of discounts double totalDiscount = TotalDsc.TotalDiscount; //Calculating total discount //Output JSON is to be returned to the Application Server so to be commited to the Database string outputJSON = JsonConvert.SerializeObject(TotalDsc.JSONObject, Formatting.Indented); textBox2.Text = outputJSON; // Verbal output tbResults.Clear(); foreach (JSONDiscountItem discount in TotalDsc.JSONObject.Discounts) { tbResults.AppendText(discount.Name + ": -" + discount.Total.ToString("0.00 €\r\n")); } tbResults.AppendText("--------------------------\r\n"); tbResults.AppendText("Total Discounts(" + TotalDsc.Count.ToString() + ") : " + totalDiscount.ToString("0.00 €")); }