您能否帮我计算一下每种付款方式的总额,如签证,万事达卡和贝宝。我创建了接口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;}
}
答案 0 :(得分: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);
}
由于您是一名bignner,因此可能需要一段时间才能了解其中的一些高级概念。
作为替代方案,您可以参考以下代码。
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;
}
答案 1 :(得分: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
}
我强烈推荐宝贝步骤,这就是为什么我改变你的结构,以便你可以更好地理解某些概念,稍微改变代码以学习语言的基础知识和编程,然后再深入潜水 - 它会压倒性的。
您只需要修复标签格式...