Ejemplo n.º 1
0
        private void LoadData()
        {
            try {
                _donators  = new List <Donator>();
                _charities = new List <Charity>();

                using (var sr = new StreamReader(FileName)) {
                    while (!sr.EndOfStream)
                    {
                        var line  = sr.ReadLine();
                        var parts = line.Split(':');
                        if (parts.Length == 4)
                        {
                            int i = 0;
                            while (i < _charities.Count && _charities[i].Name != parts[2])
                            {
                                i++;
                            }
                            Charity f;
                            if (i < _charities.Count)
                            {
                                f = _charities[i];  // Use existing sphere
                            }
                            else
                            {
                                // Create a new sphere and add it to the list
                                f = new Charity(parts[2], parts[3]);
                                _charities.Add(f);
                            }

                            var donator = new Donator(parts[0], int.Parse(parts[1]));
                            donator.Charity = f;
                            _donators.Add(donator);
                        }
                    }
                }
            }
            catch (FileNotFoundException) {
                // Файла с данными нет, благотворительные акции по умолчанию, чтобы можно было
                // выбирать его при добавлении пожертвований
                _charities.Add(new Charity("Озеленение города", "Фонд 'Леса России'"));
                _charities.Add(new Charity("Помощь вымирающим животным", "Красная книга России"));
                _charities.Add(new Charity("Лечение тяжелых заболеваний", "Фонд 'Подари жизнь'"));
                _charities.Add(new Charity("Помощь детским домам", "Фонд 'Подари жизнь'"));
            }
            catch {
                MessageBox.Show("Ошибка чтения из файла");
            }
            RefreshListBox();
        }
Ejemplo n.º 2
0
        private void buttonAdd_Click(object sender, RoutedEventArgs e)
        {
            int sum;

            if (string.IsNullOrWhiteSpace(textBoxFio.Text))
            {
                MessageBox.Show("Необходимо ввести фамилию");
                textBoxFio.Focus();
                return;
            }
            if (!int.TryParse(textBoxSum.Text, out sum))
            {
                MessageBox.Show("Некорректное значение суммы");
                textBoxSum.Focus();
                return;
            }
            if (sum < 0)
            {
                MessageBox.Show("Сумма должна быть больше 0");
                textBoxSum.Focus();
                return;
            }

            if (comboBoxCharities.SelectedItem == null)
            {
                MessageBox.Show("Необходимо выбрать благотворительную акцию");
                comboBoxCharities.Focus();
                return;
            }

            _newDonator = new Donator(textBoxFio.Text,
                                      sum);
            _newDonator.Charity = comboBoxCharities.SelectedItem as Charity;
            // Close current window
            DialogResult = true;
        }