2015-02-23 67 views
0

您可以帮我计算每种付款类型的总额,例如签证,万事达卡和贝宝。我已经创建了接口IPay并在Mastercard,Visa和PayPal类中继承它。它显示每个客户的详细信息以及订单数量和付款类型。我需要为每种付款类型计算总付款。谢谢。C#如何计算每种付款方式的总额

public class Program 
{ 
public static void Main() 
{ 
    Customer[] custArray = new Customer[3]; 

    // First Customer 
    custArray[0] = new Customer() { FirstName = "Adam", LastName = "Miles", Orders = new Order[2] }; 
    custArray[0].Orders[0] = new Order() { Description = "Shoes", Price = 19.99M, Quantity = 1, Pay = new MasterCard() }; 
    custArray[0].Orders[1] = new Order() { Description = "Gloves", Price = 29.99M, Quantity = 2,Pay = new Visa() }; 

    // Second Customer 
    custArray[1] = new Customer() { FirstName = "Andrew", LastName = "Hart", Orders = new Order[2] }; 
    custArray[1].Orders[0] = new Order() { Description = "Jacket", Price = 39.99M, Quantity = 1,Pay = new MasterCard() }; 
    custArray[1].Orders[1] = new Order() { Description = "Socks", Price = 49.99M, Quantity = 1,Pay = new Paypal() }; 


    foreach (var customer in custArray) 
    { 
     if (customer == null) continue; 

     Console.WriteLine("Customer:\n"); 
     Console.WriteLine("{0, 15} {1, 17}", "First Name", "Last Name"); 
     Console.WriteLine("{0, 10} {1, 20}", customer.FirstName, customer.LastName); 
     Console.WriteLine("Orders:\n"); 

     foreach (var order in customer.Orders) 
     { 
      if (order == null) continue; 

      Console.WriteLine("{0, 10} {1, 10} {2, 10}{3, 15}", order.Description, order.Price, order.Quantity, order.Pay); 
      Console.WriteLine("\n\n"); 
      decimal total = order.Price * order.Quantity; 
      Console.WriteLine("Total :", total); 

      if (order.Pay== new MasterCard()) 
      { 
       total = total++; 
       Console.WriteLine("Visa Total", total); 
      } 

      else if (order.Pay == new Visa()) 
      { 
       total = total++; 
       Console.WriteLine("Visa Total", total); 
      } 

      else if (order.Pay == new MasterCard()) 
      { 
       total = total++; 
       Console.WriteLine("Visa Total", total); 
      } 

     } 
     Console.WriteLine("\n\n"); 


    } 



     Console.ReadLine(); 
    } 
} 

class Customer 
{ 
    public string FirstName; 
    public string LastName; 
    public Order[] Orders; 


} 
class Order 
{ 
    public string Description; 
    public decimal Price; 
    public int Quantity; 
    public IPay Pay; 
    // Payment type p=new pay 

} 
interface IPay 
{ 
    void PayType(); 
} 

class MasterCard : IPay 
{ 
    public void PayType { get; set; } 
} 

    class Paypal : IPay 
    { 
     public void PayType { get; set; } 
    } 
    public class Visa : IPay 
    { 
     public void PayType {get;set;} 

    }   
+4

你的问题开始,当你做'order.Pay ==新万事达()'。我诚实地不敢通过这种编程水平的代码来处理真钱(老实说,没有冒犯)。 – SimpleVar 2015-02-23 05:53:48

+0

还有更多的问题。界面被美化paytype和执行它decorared财产?那太无效了? – Amit 2015-02-23 06:05:46

+0

刚刚学习编程,这是我目前正在做的任务,并且无法弄清楚在循环条件下使用什么。 – xyz 2015-02-23 06:06:06

回答

1

由于Yorye在评论中指出,你可能会重新考虑你的设计。

以下linq查询给出你所需要的。

var res = custArray.Where(c => c != null).SelectMany(c => c.Orders) 
     .GroupBy(c => c.Pay.GetType().ToString()) 
     .Select(c => new { PayType = c.Key, Sum = c.Sum(a => a.Price * a.Quantity) }); 

    foreach (var re in res) 
    { 
     Console.WriteLine("CardType {0}, Total : {1}", re.PayType.ToString(), re.Sum); 
    } 

既然您是个笨蛋,可能需要一段时间才能理解这些高级概念。

作为替代,你可以参考下面的代码。

var results = new Dictionary<string, decimal>(); 

foreach (var order in customer.Orders) 
{ 
    string key = order.Pay.GetType().ToString(); 
    if (!results.ContainsKey(key)) 
    { 
     results.Add(key,(decimal) 0.0); 
    } 

    results[key] += results[key] + order.Quantity*order.Price; 
} 
+0

感谢您的指导Yorye,它现在工作绝对正常。你看起来很简单。 – xyz 2015-02-23 06:15:54

+0

@Priya,StackOverflow中的第一天:-)。哈利回答了。 – Jagannath 2015-02-23 06:16:51

+0

是的,这是我的第一天。对不起,现在一切似乎都很复杂。感谢Hari。你能解释什么是c => c和c.key是什么意思,什么是 – xyz 2015-02-23 06:20:54

1

你真的有很多东西要学,从你的代码来看,了解范围,语法,Console.WriteLine命令()方法的重载方法,并且应该让你用自己的方式为这个任务,我有已经修复了一些代码。使用一个枚举(谷歌它)支付类型来区分... 内,您的主要方法:

 static void Main(string[] args) 
    { 
     Customer[] custArray = new Customer[3]; 

     // First Customer 
     custArray[0] = new Customer() { FirstName = "Adam", LastName = "Miles", Orders = new Order[2] }; 
     custArray[0].Orders[0] = new Order() { Description = "Shoes", Price = 19.99M, Quantity = 1, PayType = PayType.MasterCard }; 
     custArray[0].Orders[1] = new Order() { Description = "Gloves", Price = 29.99M, Quantity = 2, PayType = PayType.Visa }; 

     // Second Customer 
     custArray[1] = new Customer() { FirstName = "Andrew", LastName = "Hart", Orders = new Order[2] }; 
     custArray[1].Orders[0] = new Order() { Description = "Jacket", Price = 39.99M, Quantity = 1, PayType = PayType.MasterCard }; 
     custArray[1].Orders[1] = new Order() { Description = "Socks", Price = 49.99M, Quantity = 1, PayType = PayType.Visa }; 

     decimal total = 0; 
     decimal totalMaster = 0; 
     decimal totalVisa = 0; 
     decimal totalPaypal = 0; 

     foreach (var customer in custArray) 
     { 
      if (customer == null) continue; 

      Console.WriteLine("Customer:\n"); 
      Console.WriteLine("{0, 15} {1, 17}", "First Name", "Last Name"); 
      Console.WriteLine("{0, 11} {1, 16}", customer.FirstName, customer.LastName); 
      Console.WriteLine("Orders:\n"); 

      decimal cust_total = 0; 
      decimal cust_totalMaster = 0; 
      decimal cust_totalVisa = 0; 
      decimal cust_totalPaypal = 0; 

      foreach (var order in customer.Orders) 
      { 
       if (order == null) continue; 

       Console.WriteLine("{0, 10} {1, 10} {2, 10}{3, 15}", order.Description, order.Price, order.Quantity, order.PayType); 
       Console.WriteLine("\n\n"); 

       total += order.Price * order.Quantity; 
       cust_total += order.Price * order.Quantity; 

       if (order.PayType == PayType.MasterCard) 
       { 
        totalMaster += order.Price * order.Quantity; 
        cust_totalMaster += order.Price * order.Quantity; 
       } 

       else if (order.PayType == PayType.Visa) 
       { 
        totalVisa += order.Price * order.Quantity; 
        cust_totalVisa += order.Price * order.Quantity; 
       } 

       else if (order.PayType == PayType.Paypal) 
       { 
        totalPaypal += order.Price * order.Quantity; 
        cust_totalPaypal += order.Price * order.Quantity; 
       } 
      } 
      Console.WriteLine("MasterCard Total: {0, 8}", cust_totalMaster); 
      Console.WriteLine("Visa Total: {0, 13}", cust_totalVisa); 
      Console.WriteLine("Paypal Total: {0, 8}", cust_totalPaypal); 
      Console.WriteLine("Total: {0, 18}", cust_total); 
     } 
     Console.WriteLine("\n\n"); 
     Console.WriteLine("MasterCard GrandTotal: {0, 10}", totalMaster); 
     Console.WriteLine("Visa GrandTotal: {0, 13}", totalVisa); 
     Console.WriteLine("Paypal GrandTotal: {0, 10}", totalPaypal); 
     Console.WriteLine("GrandTotal: {0, 18}", total); 

     Console.ReadLine(); 
    } 

类:

class Customer 
{ 
    public string FirstName; 
    public string LastName; 
    public Order[] Orders; 

} 
class Order 
{ 
    public string Description; 
    public decimal Price; 
    public int Quantity; 
    public PayType PayType { get; set; } 
    //public IPay Pay; 
    // Payment type p=new pay 

} 

enum PayType{ 

    MasterCard, 
    Visa, 
    Paypal 
} 

我会强烈建议婴儿的步骤,这就是为什么我改变了你有点结构,所以你可以更好地理解某些概念,稍微修改一下代码,以便在潜入太深之前更好地学习语言和编程的基础知识 - 这将是压倒性的。

你只需要修复标签格式...

+0

谢谢你的回应。我确实了解这些概念,但在实施时遇到了困难。你的计划让我更好的理解。 – xyz 2015-02-23 06:34:37

+0

我对您的要求也有些困惑,这就是为什么我尝试了更广泛的覆盖。我很高兴它有帮助,一旦你更熟悉C#(这真是太棒了),花一些时间和研究LINQ(语言集成查询) - 这是哈里用来回答你的问题 - >非常非常强大... – EaziLuizi 2015-02-23 06:42:52

+0

感谢你失落了,哈里。这些解决方案非常有帮助,我实际上也通过Linq查询教程。我还有另外一个问题: – xyz 2015-02-28 20:58:46