2017-09-05 28 views
0

假设我们有一个存储三个计数的类,并有一个返回字符串的显示方法。如果任何计数为零,则不显示。如果存在Count1或Count2,则只显示Count3。显示在单独的行每个计数它显示X共1个记录,X共2个记录等有没有这种逻辑的替代产生不同的显示字符串?

public class Item 
    { 
     public int Count1 { get; set; } 
     public int Count2 { get; set; } 
     public int Count3 { get; set; } 

     public string ShowCounts() 
     { 
      string display = string.Empty; 

      if (this.Count1 > 0 && this.Count2 > 0) 
      { 
       display = $"{this.Count1} Count1 \n {this.Count2} Count2"; 
      } 
      if (this.Count1 > 0 && this.Count2 == 0) 
      { 
       display = $"{this.Count1} Count1"; 
      } 
      if (this.Count1 == 0 && this.Count2 > 0) 
      { 
       display = $"{this.Count2} Count2"; 
      } 
      if (this.Count3 > 0 && (this.Count1 > 0 || this.Count2 > 0)) 
      { 
       display = $"{display} \n {this.Count3} Count3"; 
      } 

      return display; 
     } 
    } 
+1

问题是? –

+1

在问题标题中。有没有比这个if/else逻辑更好的方法? –

+3

如果你的代码实际工作,它可能更适合https://codereview.stackexchange.com/(奇怪的是不能在标志中提供) – user6144226

回答

1

一种可能的方法。 特别要注意,如果Count1Count2是正数,我们只包括Count3,按照原始代码。

public string ShowCounts() 
{ 
    var results = new List<string>(); 

    if (this.Count1 > 0) 
    { 
     results.Add($"{this.Count1} Count1"); 
    } 
    if (this.Count2 > 0) 
    { 
     results.Add($"{this.Count2} Count2"); 
    } 
    if (results.Any() && this.Count3 > 0) 
    { 
     results.Add($"{this.Count3} Count3"); 
    } 

    return results.Any() ? string.Join("\n", results) : ""; 
} 
+1

我认为这是一个很好的。它允许一些灵活性,例如如果您想使用HTML换行符而不是“\ n”。你可以使用string.Join(“
”,结果) –

0
public string ShowCounts() 
    { 
     string display = string.Empty; 

     if (this.Count1 > 0) 
     { 
      display = $"{this.Count1} Count1"; 
     } 
     if (this.Count2 > 0) 
     { 
      if(!string.IsNullOrEmpty(display)) display += "\n"; 
      display += $"{this.Count2} Count2"; 
     } 
     if (this.Count3 > 0) 
     { 
      if(!string.IsNullOrEmpty(display)) 
      { // == Count3 > 0 && (Count1 > 0 || Count2 > 0) 
       display += $"\n{this.Count3} Count3"; 
      } 
     } 

     return display; 
    } 

未经检验的,但应该做的伎俩,如果它真的只有那些“如果” S。

+0

这并不坏。 –

+0

谢谢:)其实,我喜欢mjwills的回答比我自己更好,但。 – Fildor

1

最可能健壮方法将添加一个属性(ShowCountAttribute)并具有反射加载属性并称之为一天。

其实,这里有一个简单的例子:

[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] 
public class ShowCountAttribute : Attribute 
{ 
    public bool Required { get; set; } 
    public int Order { get; set; } 
} 

那么你ShowCounts

var counts = 
    this.GetType() 
     .GetProperties() 
     .Where(prop => Attribute.IsDefined(prop, typeof(ShowCountAttribute))) 
     .Select(prop => new KeyValuePair<string, Tuple<ShowCountAttribute, int>>(prop.Name, new Tuple<ShowCountAttribute, int>((ShowCountAttribute)prop.GetCustomAttributes(typeof(ShowCountAttribute), true).First(), (int)prop.GetValue(this)))); 

if (counts.Any(x => x.Value.Item2 > 0 && x.Value.Item1.Required)) 
    return string.Join("\n", counts.Where(kvp => kvp.Value.Item2 > 0).OrderBy(kvp => kvp.Value.Item1.Order).Select(kvp => $"{kvp.Value.Item2} {kvp.Key}")); 
else 
    return string.Empty; 

这也是琐碎的ShowCustomAttribute类定义自己Name,如果设置将覆盖属性名称。我把这个用法留给你。如果不包含Required计数,则它会发送string.Empty

我在这里使用了Tuple<,>,因为C#7.0对它们有很好的支持。

+0

我喜欢它,但它比我需要的要复杂一点。 –

+0

@MattLaCrosse您正在执行复杂的操作。你无法思考什么是或者不是很复杂 - 你必须考虑什么是最有前途的证据。如果你在[CodeReview.SE]上,我会告诉你废除整个方法,因为它混合了两种不同的显示问题。您应该有两个步骤进行此过程:获取* valid *计数列表,并打印它们。您目前有一个*非常*复杂的步骤,该解决方案允许您将它们分成适当的步骤。 –

1

我知道使用附加线的固定数+ =比使用StringBuilder的速度更快,但我认为不必追加“\ n”个所有的时间使代码更易读

public string ShowCounts() 
{ 
    var builder = new StringBuilder(); 

    bool count1 = this.Count1 > 0; 
    bool count2 = this.Count2 > 0; 

    if (count1) 
     builder.AppendLine($"{this.Count1} Count1"); 

    if (count2) 
     builder.AppendLine($"{this.Count2} Count2"); 

    if (this.Count3 > 0 && (count1 || count2)) 
     builder.AppendLine($"{this.Count3} Count3"); 

    return builder.ToString(); 
} 
+0

这是相当不错的一个。 –

相关问题