C# 面向对象例题 -- Mp3功能实现

C# 面向对象例题 -- Mp3功能实现

请利用面向对象分析实现Mp3功能,Mp3具有播放歌曲,载入本地歌曲 ,上一首,下一首,添加歌曲,退出播放器功能。

 
实现结果:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace 面向对象7._13_Mp3完善
{
    class Program
    {
        static void Main(string[] args)
        {

            PlaySong ps = new PlaySong();
            ps.Inof();

            Console.ReadLine();
        }
    }
    //歌曲信息列表
    class Song
    {
        public Song() { }
        public Song(String title, string singer, int length)
        {
            this.titile = title;
            this.singer = singer;
            this.length = length;

        }
        ///
        /// 歌曲名
        ///
        public String titile{set;get;}
        public String singer { set; get; }
        public int length { set; get; }


    }

    class PlaySong
    {
        List list = new List();
        ///
        /// 载入本地歌曲
        ///
        
        private int listLenght;  //获取到列表长度(有几首歌)
        public void bianli() {

            listLenght = 0;
            foreach (Song item in list)
            {
                Console.Write("    " +(listLenght+1)+"."+item.titile+" - "+item.singer+" ");
                time(item.length);
                listLenght++;
            }
        }
      
        public void Inof()
        {

            Console.WriteLine("您的当前播放列表是:");
            Console.WriteLine();
            Console.WriteLine("++ ——————————— ++");
            Console.WriteLine();
            bianli();
            Console.WriteLine();
            Console.WriteLine("++ ——————————— ++");
            Console.WriteLine();
            Console.WriteLine(" 请您输入功能序号 :");
            Console.WriteLine();
            Console.WriteLine("1.播放歌曲  2.载入本地歌曲  3.上一首  4.下一首  5.暂停  6.添加歌曲  7.退出播放器");
            int n = int.Parse(Console.ReadLine());
            switch (n)
            {
                case 1:
                    play();
                    break;
                case 2:
                    load();
                    break;
                case 3:
                    upSong();
                    break;
                case 4:
                    downSong();
                    break;
                case 5:
                    stop();
                    break;
                case 6:
                    downLoad();
                    break;
                case 7:
                    return;
                default :
                    break;

            }
        }
        public void load()
        {
            Song s1 = new Song("灰色头像", "许嵩", 356);
            Song s2 = new Song("坏孩子", "许嵩", 303);
            Song s3 = new Song("城府", "许嵩", 287);
            Song s4 = new Song("因为了解", "汪苏泷", 196);
            Song s5 = new Song("送你的读白", "许嵩", 314);
            list.Add(s1);
            list.Add(s2);
            list.Add(s3);
            list.Add(s4);
            list.Add(s5);
            Console.WriteLine("++ ——————————— ++");
            Console.WriteLine();
            bianli();
            Console.WriteLine();
            Console.WriteLine("++ ——————————— ++");
            Inof();
        }
        public void downLoad()
        {
            Song s = new Song();

            Console.Write("请输入您要下载的歌曲名称:");
            s.titile = Console.ReadLine();
            Console.Write("请输入您要下载的歌手名称:");
            s.singer = Console.ReadLine();
            Console.Write("请输入您要下载歌曲的时长:");
            s.length =int.Parse( Console.ReadLine());

            list.Add(s);
            listLenght++;
            Console.WriteLine("您的歌曲下载成功!");
            Console.WriteLine();
            Inof();
        }
       private int index = 0; //记录播放的歌曲
        public void play()
        {
            Console.WriteLine();
            if (index < 0)
            {
                Console.WriteLine("当前列表没有歌曲!");
               
            }
            if (index < listLenght)
            {
                Console.WriteLine("正在播放:" + list[index].titile);
            }
            Console.WriteLine();
            Inof();
        }

       
        public void upSong()
        {
            if (index < 0) {
                Console.WriteLine("当前列表没有歌曲");
            }
            
            index = index - 1 < 0 ?  0: index - 1;
            if (index - 1 < 0)
            {
                Console.WriteLine("当前播放已经是第一首音乐:" + list[0].titile);
            }
            else
            {
                Console.WriteLine("正在播放:" + list[index].titile);
            }
            Console.WriteLine();
            Inof();
        }

        public void downSong()
        {
            if (index < 0)
            {
                Console.WriteLine("当前列表没有歌曲");
            }
            index = index + 1 > listLenght ? listLenght : index+1;
            if (index +1 > listLenght)
            {
                
                Console.WriteLine("当前播放已经是最后一首音乐:");
            }
            else
            {
                Console.WriteLine("正在播放:" + list[index].titile);
            }
                Console.WriteLine();
            Inof();
        }

        public void stop()
        {
            Console.WriteLine("暂停播放:"+list[index].titile);
            Console.WriteLine();
            Inof();
        }

        ///
        /// 转换时间格式 00:00:00 
        ///
        ///
        public void time(int time)
        {
            TimeSpan ts = new TimeSpan(0, 0, time);
            Console.WriteLine("  " + (ts.Minutes).ToString().PadLeft(2, '0') + ":" + (ts.Seconds).ToString().PadLeft(2, '0'));
        }

    }

}

 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈言必行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值