private void btnDodadiAvtomobil_Click(object sender, EventArgs e) { if (cbMarki.SelectedIndex == -1) { string message = "Изберете марка!"; string caption = "Неуспешно додавање на автомобил"; MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (txtModel.Text.Trim().Length == 0) { string message = "Внесете модел!"; string caption = "Неуспешно додавање на автомобил"; MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Avtomobil avtomobil = new Avtomobil(); avtomobil.Marka = cbMarki.SelectedItem as Marka; avtomobil.Model = txtModel.Text; avtomobil.Cena = nupCena.Value; avtomobil.Potrosuvacka = nupPotrosuvacka.Value; lstAvtomobili.Items.Add(avtomobil); ClearValues(); RefreshComputation(); }
private void RefreshComputation() { decimal averageConsumption = 0M; decimal minConsumption = Decimal.MaxValue; decimal maxPrice = Decimal.MinValue; Avtomobil minCar = null; Avtomobil maxCar = null; foreach (Avtomobil item in lstAvtomobili.Items) { if (item.Cena > maxPrice) { maxPrice = item.Cena; maxCar = item; } if (item.Potrosuvacka < minConsumption) { minConsumption = item.Potrosuvacka; minCar = item; } averageConsumption += item.Potrosuvacka; } if ((minCar != null && maxCar != null) || averageConsumption != 0M) { txtNajskap.Text = maxCar.ToString(); txtNajekonomicen.Text = minCar.ToString(); txtProsecnaPotrosuvacka.Text = (averageConsumption / lstAvtomobili.Items.Count).ToString("0.0"); } else { ClearComputed(); } }