Compare commits
10 Commits
Author | SHA1 | Date |
---|---|---|
Jason Zhu | bf4a437acd | |
Jason Zhu | e2aa8c7cba | |
Jason Zhu | a376175c76 | |
Jason Zhu | 0e57f0e2e8 | |
Jason Zhu | c95b347b15 | |
Jason Zhu | be286cffbd | |
Jason Zhu | 61cf1cbfaf | |
Jason Zhu | ec983ff21c | |
Jason Zhu | 3b938d8c61 | |
Jason Zhu | f98ec2cbc7 |
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
using automatically_implemented_properties;
|
using automatically_implemented_properties;
|
||||||
|
|
||||||
namespace automatically_implemented_properties
|
namespace automatically_implemented_properties
|
||||||
|
@ -9,35 +10,70 @@ namespace automatically_implemented_properties
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
// create and populate Shopping Cart
|
Product[] products =
|
||||||
ShoppingCart cart = new ShoppingCart
|
|
||||||
{
|
{
|
||||||
Products = new List<Product>
|
new Product {Name = "Kayak", Category = "Watersports", Price = 275M},
|
||||||
{
|
new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M},
|
||||||
new Product {Name = "Kayak", Price = 275M},
|
new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M},
|
||||||
new Product {Name = "Lifejacket", Price = 48.95M},
|
new Product {Name = "Corner flag", Category = "Soccer", Price = 34.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();
|
var results = from product in products
|
||||||
|
orderby product.Price descending
|
||||||
Console.WriteLine("Total: {0:c}", cartTotal);
|
select new
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class MyExtensionMethod
|
public static class MyExtensionMethod
|
||||||
{
|
{
|
||||||
public static decimal TotalPrices(this ShoppingCart cartParam)
|
public static decimal TotalPrices(this IEnumerable<Product> productEnum)
|
||||||
{
|
{
|
||||||
decimal total = 0;
|
decimal total = 0;
|
||||||
foreach (Product prod in cartParam.Products)
|
foreach (Product prod in productEnum)
|
||||||
{
|
{
|
||||||
total += prod.Price;
|
total += prod.Price;
|
||||||
}
|
}
|
||||||
return total;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,9 +1,20 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace automatically_implemented_properties
|
namespace automatically_implemented_properties
|
||||||
{
|
{
|
||||||
public class ShoppingCart
|
public class ShoppingCart : IEnumerable<Product>
|
||||||
{
|
{
|
||||||
public List<Product> Products { get; set; }
|
public List<Product> Products { get; set; }
|
||||||
|
|
||||||
|
public IEnumerator<Product> GetEnumerator()
|
||||||
|
{
|
||||||
|
return Products.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
return GetEnumerator();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue