【发布时间】: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#