Compare commits

..

No commits in common. "chap5" and "master" have entirely different histories.

2 changed files with 16 additions and 63 deletions

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using automatically_implemented_properties;
namespace automatically_implemented_properties
@ -10,70 +9,35 @@ namespace automatically_implemented_properties
{
static void Main(string[] args)
{
Product[] products =
// create and populate Shopping Cart
ShoppingCart cart = new ShoppingCart
{
new Product {Name = "Kayak", Category = "Watersports", Price = 275M},
new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M},
new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M},
new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95M}
};
var results = from product in products
orderby product.Price descending
select new
Products = new List<Product>
{
product.Name,
product.Price
};
int count = 0;
// print out the names
foreach (var p in results)
{
Console.WriteLine("Item: {0}, Cost: {1}", p.Name, p.Price);
if (++count == 3)
{
break;
new Product {Name = "Kayak", Price = 275M},
new Product {Name = "Lifejacket", Price = 48.95M},
new Product {Name = "Soccer ball", Price = 19.50M},
new Product {Name = "Corner flag", Price = 34.95M}
}
}
};
// get total value of the products in cart
decimal cartTotal = cart.TotalPrices();
Console.WriteLine("Total: {0:c}", cartTotal);
}
}
public static class MyExtensionMethod
{
public static decimal TotalPrices(this IEnumerable<Product> productEnum)
public static decimal TotalPrices(this ShoppingCart cartParam)
{
decimal total = 0;
foreach (Product prod in productEnum)
foreach (Product prod in cartParam.Products)
{
total += prod.Price;
}
return total;
}
public static IEnumerable<Product> FilterByCategory(this IEnumerable<Product> productEnum,
string categoryParam)
{
foreach (Product prod in productEnum)
{
if (prod.Category == categoryParam)
{
yield return prod;
}
}
}
public static IEnumerable<Product> Filter(this IEnumerable<Product> productEnum,
Func<Product, bool> selectorParam)
{
foreach (Product prod in productEnum)
{
if (selectorParam(prod))
{
yield return prod;
}
}
}
}
}

View File

@ -1,20 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
namespace automatically_implemented_properties
{
public class ShoppingCart : IEnumerable<Product>
public class ShoppingCart
{
public List<Product> Products { get; set; }
public IEnumerator<Product> GetEnumerator()
{
return Products.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}