Example #1
0
 /// <summary>
 /// Восстанавливает коллекцию, записанную в файл
 /// </summary>
 public void OpenFile(string filename)
 {
     if (!System.IO.File.Exists(filename))
     {
         throw new Exception("Файл не существует");
     }
     if (musicFiles.Count != 0)
     {
         DeleteMusic();
     }
     using (StreamReader sw = new StreamReader(filename))
     {
         while (!sw.EndOfStream)
         {
             string   str          = sw.ReadLine();
             String[] dataFromFile = str.Split(new String[] { "|" },
                                               StringSplitOptions.RemoveEmptyEntries);
             ushort    id        = (ushort)Convert.ToInt32(dataFromFile[0]);
             string    artist    = dataFromFile[1];
             string    song      = dataFromFile[2];
             ushort    year      = (ushort)Convert.ToInt32(dataFromFile[3]);
             string    genre     = dataFromFile[4];
             MusicFile musicFile = new MusicFile(id, artist, song, year, genre);
             AddMusicFile(musicFile);
         }
     }
 }
Example #2
0
 private void btnAddRecord_Click(object sender, EventArgs e)
 {
     try
     {
         string artist = textBoxArtist.Text;
         string song   = textBoxSong.Text;
         ushort year   = (ushort)Convert.ToInt32(textBoxYear.Text);
         string genre  = textBoxGenre.Text;
         textBoxArtist.Text = "";
         textBoxSong.Text   = "";
         textBoxYear.Text   = "";
         textBoxGenre.Text  = "";
         MusicFile musicFile = new MusicFile(generateID(), artist, song, year, genre);
         data.AddMusicFile(musicFile);
         int       n     = data.MusicFiles.Count;
         MusicFile music = (MusicFile)data.MusicFiles[n - 1];
         dataGridViewTable.Rows.Add(music.SongID, artist, song, year, genre);
         BanChangeColumn(n - 1);
         if ((!timer1.Enabled) && (filename != ""))
         {
             timer1.Enabled = true;
             timer1.Start();
         }
     } catch (Exception exception)
     {
         MessageBox.Show($"Ошибка: {exception.Message}");
     }
 }
Example #3
0
        int IComparer.Compare(object x, object y)
        {
            MusicFile music1 = (MusicFile)x;
            MusicFile music2 = (MusicFile)y;

            return((this.m_direction == SortDirection.Ascending) ?
                   music1.YearRelease.CompareTo(music2.YearRelease) :
                   music2.YearRelease.CompareTo(music1.YearRelease));
        }
Example #4
0
        /// <summary>
        /// Изменить год релиза песни у заданного элемента
        /// </summary>
        public void ChangeYearRelease(ushort year, int index)
        {
            MusicFile music = (MusicFile)MusicFiles[index];

            if ((year < 1900) || (year > DateTime.Now.Year))
            {
                throw new Exception("Год выпуска не раньше 1900 и не позднее " + DateTime.Now.Year);
            }
            music.YearRelease = year;
        }
Example #5
0
        /// <summary>
        /// Поиск по заданному параметру и возвращение индексов найденных элементов
        /// Вернет -1, если элементы не найдены
        /// </summary>
        public List <int> SearchMusicFile(string query)
        {
            List <int> count = new List <int>();
            ushort     num_query;

            if (ushort.TryParse(query, out num_query))
            {
                for (int i = 0; i < musicFiles.Count; i++)
                {
                    MusicFile music = (MusicFile)musicFiles[i];
                    if (music.SongID == num_query)
                    {
                        count.Add(i);
                        break;
                    }
                    else
                    {
                        if (music.YearRelease == num_query)
                        {
                            count.Add(i);
                        }
                    }
                }
                if (count.Count == 0)
                {
                    count.Add(-1);
                }
                return(count);
            }
            query = query.ToLower(); // перевод в нижний регистр
            query = query.Replace(" ", "");
            for (int i = 0; i < musicFiles.Count; i++)
            {
                MusicFile music = (MusicFile)musicFiles[i];
                if (music.ArtistName.ToLower().Replace(" ", "").Contains(query))
                {
                    count.Add(i);
                }
                else
                if (music.SongTitle.ToLower().Replace(" ", "").Contains(query))
                {
                    count.Add(i);
                }
                else
                if (music.SongGenre.ToLower().Replace(" ", "").Contains(query))
                {
                    count.Add(i);
                }
            }
            if (count.Count == 0)
            {
                count.Add(-1);
            }
            return(count);
        }
Example #6
0
 private void WriteToDataGrid()
 {
     for (int i = 0; i < data.MusicFiles.Count; i++)
     {
         MusicFile music = (MusicFile)data.MusicFiles[i];
         dataGridViewTable.Rows.Add(music.SongID, music.ArtistName,
                                    music.SongTitle, music.YearRelease, music.SongGenre);
         BanChangeColumn(i);
     }
     // последнюю строку запрещаем редактировать
     dataGridViewTable.Rows[data.MusicFiles.Count].ReadOnly = true;
 }
Example #7
0
        // Генерация не повторяющихся id
        public ushort generateID()
        {
            Random r  = new Random();
            int    id = r.Next(0, 1000);

            for (int i = 0; i < data.MusicFiles.Count; i++)
            {
                MusicFile music = (MusicFile)data.MusicFiles[i];
                if (music.SongID == id)
                {
                    i  = 0;
                    id = r.Next(0, 1000);
                }
            }
            return((ushort)id);
        }
Example #8
0
        /// <summary>
        /// Изменить жанр песни у заданного элемента
        /// </summary>
        public void ChangeGenreSong(string genre, int index)
        {
            MusicFile music = (MusicFile)MusicFiles[index];

            music.SongGenre = genre;
        }
Example #9
0
        /// <summary>
        /// Изменить название песни у заданного элемента
        /// </summary>
        public void ChangeSongTittle(string song, int index)
        {
            MusicFile music = (MusicFile)MusicFiles[index];

            music.SongTitle = song;
        }
Example #10
0
        /// <summary>
        /// Изменить имя исполнителя у заданного элемента
        /// </summary>
        public void ChangeArtistName(string artist, int index)
        {
            MusicFile music = (MusicFile)MusicFiles[index];

            music.ArtistName = artist;
        }
Example #11
0
 /// <summary>
 /// Добавление песни в коллекцию
 /// </summary>
 public void AddMusicFile(MusicFile musicFile)
 {
     musicFiles.Add(musicFile);
 }