Reputation: 1
Can you please help me calculate the total of each payment type such as visa, mastercard and paypal. I have created interface IPay and inherited from it in Mastercard, Visa and PayPal class. It displays each customer details along with number of orders and payment type. I need to calculate Total Payment for each payment type. Thanks.
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;}
}
Upvotes: 0
Views: 2150
Reputation: 16956
As Yorye stated in the comments, you might rethink your design.
Following linq query gives what you need.
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);
}
Since you are a bignner it might take a while to understand some of these advanced concepts.
As an alternative you can refer below code.
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;
}
Upvotes: 1
Reputation: 1615
You really have a lot to learn, judging from your code, learn about scope, syntax, overloaded methods of the Console.Writeline() method and that should get you on your way for this assignment, I have fixed your code a little already. Using an enum (google it) to differentiate between payment types... Inside your main method:
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();
}
Classes:
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
}
I would strongly recommend baby steps, that is why I changed your structure a bit so you can hopefully understand certain concepts better, alter the code slightly to learn the basics of the language and programming better before diving in too deep - It will be overwhelming.
You just need to fix the tab formatting...
Upvotes: 1