您可以幫我計算每種付款類型的總額,例如簽證,萬事達卡和貝寶。我已經創建了接口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;}
}
你的問題開始,當你做'order.Pay ==新萬事達()'。我誠實地不敢通過這種編程水平的代碼來處理真錢(老實說,沒有冒犯)。 – SimpleVar 2015-02-23 05:53:48
還有更多的問題。界面被美化paytype和執行它decorared財產?那太無效了? – Amit 2015-02-23 06:05:46
剛剛學習編程,這是我目前正在做的任務,並且無法弄清楚在循環條件下使用什麼。 – xyz 2015-02-23 06:06:06