2015-02-23 78 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