【问题标题】:display getCertifcation if a song has getCertification如果歌曲有 getCertification,则显示 getCertifcation
【发布时间】:2019-06-29 08:26:09
【问题描述】:

如果歌曲有认证(银、金、铂金),我需要显示它的认证,目前我正在调用该方法,但这并不检查歌曲是否真的有认证。

然后我将如何显示已显示详细信息的那些歌曲的“总销量”和“平均销量”。我知道这将需要为客户端代码提供可用的副本,但我将如何去做。

namespace Songs
{
    class Song
    {
        string name;
        string artist;
        int copiesSold;

        public Song(string name, string artist, int copiesSold)
        {
            this.name = name;
            this.artist = artist;
            this.copiesSold = copiesSold;
        }

        public Song() : this("my_name", "my_artist", 1000)
        {

        }

        public string GetArtist()
        {
            return artist;
        }

        public string GetDetails()
        {
            return $"Name: {name} Artist: {artist} Copies Sold: {copiesSold},";
        }

        public string GetCertification()
        {
            return copiesSold < 200000 ? null : copiesSold < 400000 ? "Silver" : copiesSold < 600000 ? "Gold" : "Platinum";
        }

        public void AddCopiesSold(int number)
        {
            copiesSold += number;
        }
    }
}

程序类

namespace Songs
{
    class Program
    {
        static void Main(string[] args)
        {

            //InputSongDetails();
            //Console.WriteLine(InputSongDetails());

            Song[] songs = new Song[4];
            for (int i = 0; i < songs.Length; i++)
            {
                songs[i] = InputSongDetails();
            }

            Console.WriteLine("Enter an artist name, or just press return for all artists");
            var name = Console.ReadLine().ToUpper();
            name = name.Trim();

            foreach (var song in songs)
            {
                if (string.IsNullOrWhiteSpace(name) || song.GetArtist().Equals(name))
                {
                    Console.WriteLine(song.GetDetails());
                    song.GetCertification();
                }
            }

        }


        static Song InputSongDetails()
        {
            Console.WriteLine("What is the name of your song");
            string name = Console.ReadLine();

            Console.WriteLine("What is the artists name");
            string artist = Console.ReadLine();

            int records;
            Console.WriteLine("How many records did it sell");
            while (!int.TryParse(Console.ReadLine(), out records) || records < 0)
            {
                Console.WriteLine("That is not valid please enter a number");
            }
            return new Song(name, artist, records);
        }


    }
}

【问题讨论】:

  • 哇,今天有很多关于Songs的问题!
  • 您遇到的具体问题是什么?什么行为不符合您的要求,或者您收到什么错误?您给了我们一堵代码墙,但在您尝试过的方法或那些尝试中没有奏效的方面并没有太多。

标签: c#


【解决方案1】:

“如果歌曲有认证(银、金、铂金),我需要显示它的认证,目前我正在调用该方法,但这并不检查歌曲是否真的有认证。”

foreach (var song in songs)
{
    if (string.IsNullOrWhiteSpace(name) || song.GetArtist().Equals(name))
    {
          Console.WriteLine(song.GetDetails());
          song.GetCertification() != null ? song.GetCertification() : /* Handle no certificate here */;
    }
}

对于您遇到的其他“问题” - 您必须更清晰地构建它,似乎您希望我们为您编写该功能。 Stackoverflow 不是代码编写服务,但这将解决您的空错误(您将 copiesSold &lt; 200000 设置为)。

【讨论】:

    【解决方案2】:

    您缺少一些代码。这应该评估传入的艺术家,然后按艺术家检索歌曲,返回歌曲的唯一实例。

    song.GetArtist().Equals(name)
    

    我没有看到 GetArtist() 定义,它应该将艺术家的名字作为参数(我认为)。

    此外,您将歌曲中的 name 重新定义为艺术家:

        Console.WriteLine("What is the name of your song");
        string name = Console.ReadLine();
    

    ...

        Console.WriteLine("Enter an artist name, or just press return for all artists");
        var name = Console.ReadLine().ToUpper();
        name = name.Trim();
    

    您应该使用唯一变量(例如:artistName、songName 等)来防止像这样的意外冲突。

    【讨论】:

      猜你喜欢
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 2013-02-19
      相关资源
      最近更新 更多