【问题标题】:C# How to calculate total for each payment methodC#如何计算每种付款方式的总额
【发布时间】:2015-02-23 05:43:41
【问题描述】:

你能帮我计算一下签证、万事达卡和贝宝等每种支付类型的总和吗?我已经创建了接口 IPay 并在 Mastercard、Visa 和 PayPal 类中继承了它。它显示每个客户的详细信息以及订单数量和付款类型。我需要计算每种付款类型的总付款。谢谢。

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;}

    }        

【问题讨论】:

  • 当您执行order.Pay== new MasterCard() 时,您的问题就开始了。老实说,我不敢在这种级别的编程中通过代码处理真钱(老实说,没有冒犯)。
  • 还有更多的问题。接口贴有成员支付类型,在实现中它是属性?那也太虚了吧?
  • 目前只是学习编程,这是我目前正在做的作业,不知道在循环条件下使用什么。
  • 谢谢,这个程序我改了很多次,错过了一次改界面方法。谢谢。
  • 好的,我明白了。谢谢哈里。

标签: c# loops methods interface


【解决方案1】:

你真的有很多东西要学,从你的代码来看,了解 Console.Writeline() 方法的范围、语法、重载方法,这应该让你在完成这个任务的路上,我已经修复了你的代码已经很少了。使用枚举(google it)来区分付款类型...... 在你的主要方法中:

        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
}

我强烈推荐小步骤,这就是为什么我稍微改变了你的结构,这样你就可以更好地理解某些概念,稍微改变代码以更好地学习语言和编程的基础知识,然后再深入研究 - 它会势不可挡。

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

【讨论】:

  • 感谢您的回复。我确实理解这些概念,但在实施它们时遇到了麻烦。你的程序让我更好地理解了。
  • 我也对您的要求感到有些困惑,这就是为什么我尝试了更广泛的覆盖范围。我很高兴它有帮助,一旦你对 C#(这很棒)感到更舒服,花一些时间研究 LINQ(语言集成查询) - 这是 Hari 用来回答你问题的 -> 非常非常强大......跨度>
  • 谢谢迷失和哈里。这些解决方案非常有帮助,实际上我也学习了 Linq 查询教程。我还有一个问题:
  • 公共字符串Customer(Customer[] cust)
  • 抱歉,我还不擅长发帖提问。如果我有一个方法 public string Customers(Customer[] cust),其中对象 cust 是另一个类类型 Customer,我想在 display() 中使用 cust。如何从另一个方法调用类类型的数组对象。我在 display() 中调用方法 Customers() 但它不识别 cust 对象。请帮忙。
【解决方案2】:

正如 Yorye 在 cmets 中所述,您可能会重新考虑您的设计。

以下 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;
}

【讨论】:

  • 感谢您的指导,Yorye,它现在工作得很好。你让它看起来很简单。
  • @Priya,在 StackOverflow 的第一天 :-)。是哈里回答的。
  • 是的,这是我的第一天。抱歉,目前一切似乎都很复杂。谢谢哈里。你能解释一下什么是c=>c,c.key是什么意思,什么是a
  • @Priya 我已经为您编辑了答案以使用简单的方法,稍后您可以阅读有助于理解这些高级概念的在线文章/材料。
猜你喜欢
  • 1970-01-01
  • 2015-08-31
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-18
相关资源
最近更新 更多