【发布时间】: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