我有一个问题陈述,我的解决方案满足了需求,但我不认为它是好的解决方案,因为它打破了单一的责任,requirement.Can,有人建议任何更好的方式来实现这个结果。
我对编程很陌生,但我甚至可以说,我不是一个支持OOPS.Kindly的好解决方案,提出了任何可以提高解决方案可用性的建议。
问题:
需要为结帐过程实现一个简单的升级引擎。我们的购物车包含一个单字符SKU ids列表(A,B,C.)促销引擎需要运行的位置。
促销引擎在应用2种促销类型以固定价格购买SKU的“n”项(3A为130)、以固定价格购买SKU 1和SKU 2(C+D= 30 )后,需要计算订单总价值。
促销引擎应该是模块化的,以便在稍后的日期添加更多的促销类型(例如,将来的促销可能是SKU单价的x% )。在这个编码练习中,您可以假设促销是相互排斥的;换句话说,如果应用了一个,那么其他的促销就不适用了。
测试设置
Unit price for SKU IDs
A 50
B 30
C 20
D 15积极推进
3 of A's for 130
2 of B's for 45
C & D for 30设想A
1 * A 50
1 * B 30
1 * C 20
======
Total 100设想方案B
5 * A 130 + 2*50
5 * B 45 + 45 + 30
1 * C 20
======
Total 370设想方案C
3 * A 130
5 * B 45 + 45 + 1 * 30
1 * C -
1 * D 30
======
Total 280我的解决方案:
产品类:
namespace ConsoleApp1
{
public class Product
{
public string Id { get; set; }
public decimal Price { get; set; }
public Product(string id)
{
this.Id = id;
switch (id)
{
case "A":
this.Price = 50m;
break;
case "B":
this.Price = 30m;
break;
case "C":
this.Price = 20m;
break;
case "D":
this.Price = 2015m;
break;
}
}
}
}Program.cs
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Product> products = new List<Product>();
Console.WriteLine("total number of order");
int a = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < a; i++)
{
Console.WriteLine("enter the type of product:A,B,C or D");
string type= Console.ReadLine();
Product p = new Product(type);
products.Add(p);
}
int totalPrice = GetTotalPrice(products);
Console.WriteLine(totalPrice);
Console.ReadLine();
}
private static int GetTotalPrice(List<Product> products)
{
int counterofA = 0;
int priceofA = 50;
int counterofB = 0;
int priceofB = 30;
int CounterofC = 0;
int priceofC = 20;
int CounterofD = 0;
int priceofD = 15;
foreach (Product pr in products)
{
if (pr.Id == "A"||pr.Id=="a")
{
counterofA=counterofA + 1;
}
if (pr.Id == "B"||pr.Id=="b")
{
counterofB = counterofB + 1;
}
if (pr.Id == "C"||pr.Id=="c")
{
CounterofC = CounterofC + 1;
}
if (pr.Id == "D" || pr.Id=="d")
{
CounterofD = CounterofD + 1;
}
}
int totalPriceofA = (counterofA / 3) * 130 + (counterofA % 3 * priceofA);
int totalPriceofB = (counterofB / 2) * 45 + (counterofB % 2 * priceofB);
int totalPriceofC = (CounterofC * priceofC);
int totalPriceofD = (CounterofD * priceofD);
return totalPriceofA + totalPriceofB + totalPriceofC + totalPriceofD;
}
}
}工作解决方案:
https://dotnetfiddle.net/ELP2gV发布于 2020-06-17 19:19:14
不久,我将根据产品模型的类型进行价格计算,并将其移到ProductService。我还将把GetTotalPrice移到ProductService。
ProductService还将获得一个名为IProductService的接口。
最后,我将使小代码清理和造型。
通过这种方式,您可以更容易地在ProductService中测试您的方法,并在ProductService中收集方法逻辑。
让我们看看代码:
产品模型
public class Product
{
public string Id { get; set; }
public decimal Price { get; set; }
}ProductService接口:
public interface IProductService
{
void GetPriceByType(Product product);
int GetTotalPrice(List<Product> products);
}ProductService类:
public void GetPriceByType(Product product)
{
switch (product.Id)
{
case "A":
product.Price = 50m;
break;
case "B":
product.Price = 30m;
break;
case "C":
product.Price = 20m;
break;
case "D":
product.Price = 2015m;
break;
}
}
public int GetTotalPrice(List<Product> products)
{
int counterOfA = 0;
int priceOfA = 50;
int counterOfB = 0;
int priceOfB = 30;
int counterOfC = 0;
int priceOfC = 20;
int counterOfD = 0;
int priceOfD = 15;
foreach (Product pr in products)
{
switch (pr.Id)
{
case "A":
case "a":
counterOfA += 1;
break;
case "B":
case "b":
counterOfB += 1;
break;
case "C":
case "c":
counterOfC += 1;
break;
case "D":
case "d":
counterOfD += 1;
break;
}
}
int totalPriceOfA = (counterOfA / 3) * 130 + (counterOfA % 3 * priceOfA);
int totalPriceOfB = (counterOfB / 2) * 45 + (counterOfB % 2 * priceOfB);
int totalPriceOfC = (counterOfC * priceOfC);
int totalPriceOfD = (counterOfD * priceOfD);
return totalPriceOfA + totalPriceOfB + totalPriceOfC + totalPriceOfD;
}还有一些不是问题的一部分,关于您的切换条件,您可以使用C# 8开关表达式并将其缩短如下:
product.Price = product.Id switch
{
"A" => 50m,
"B" => 30m,
"C" => 20m,
"D" => 2015m,
_ => product.Price
};发布于 2020-06-18 09:30:29
如果要创建一个Promotion Engine,则需要添加额外的类,例如:
Promotion -存储product.id和使用促销价格出现的次数Order -存储消费者购买的产品然后,你需要决定是否可以将晋升联系起来。如果是,那么下面的LinqPad脚本就可以使用了。如果没有,那么您将需要相应地更改algoritm。如果只有单一的促销可以申请,它应该是最高的回扣,那么decimal promoprice = promoprices.Max()就足够了。;)
我增加了额外的评论。请读一读。
void Main()
{
//create list of promotions
//we need to add information about Product's count
Dictionary<String, int> d1 = new Dictionary<String, int>();
d1.Add("A", 3);
Dictionary<String, int> d2 = new Dictionary<String, int>();
d2.Add("B", 2);
Dictionary<String, int> d3 = new Dictionary<String, int>();
d3.Add("C", 1);
d3.Add("D", 1);
List<Promotion> promotions = new List<Promotion>()
{
new Promotion(1, d1, 130),
new Promotion(2, d2, 45),
new Promotion(3, d3, 30)
};
//create orders
List<Order> orders = new List<Order>();
Order order1 = new Order(1, new List<Product>(){new Product("A"), new Product("A"), new Product("B"), new Product("B"), new Product("C"), new Product("D")});
Order order2 = new Order(2, new List<Product>(){ new Product("A"), new Product("A"), new Product("A"), new Product("A"), new Product("A"), new Product("A"), new Product("B")});
Order order3 = new Order(3, new List<Product>(){ new Product("A"), new Product("A"), new Product("D"), new Product("B"), new Product("B")});
orders.AddRange(new Order[]{order1, order2, order3});
//check if order meets promotion
foreach(Order ord in orders)
{
List<decimal> promoprices = promotions
.Select(promo => PromotionChecker.GetTotalPrice(ord, promo))
.ToList();
decimal origprice = ord.Products.Sum(x=>x.Price);
decimal promoprice = promoprices.Sum();
Console.WriteLine($"OrderID: {ord.OrderID} => Original price: {origprice.ToString("0.00")} | Rebate: {promoprice.ToString("0.00")} | Final price: {(origprice - promoprice).ToString("0.00")}");
}
}
// Define other methods and classes here
public class Product
{
public string Id { get; set; }
public decimal Price { get; set; }
public Product(string id)
{
this.Id = id;
switch (id)
{
case "A":
this.Price = 50m;
break;
case "B":
this.Price = 30m;
break;
case "C":
this.Price = 20m;
break;
case "D":
this.Price = 15m;
break;
}
}
}
public class Promotion
{
public int PromotionID { get; set; }
public Dictionary<string, int> ProductInfo { get; set; }
public decimal PromoPrice { get; set; }
public Promotion(int _promID, Dictionary<string, int> _prodInfo, decimal _pp)
{
this.PromotionID = _promID;
this.ProductInfo = _prodInfo;
this.PromoPrice = _pp;
}
}
public class Order
{
public int OrderID { get; set; }
public List<Product> Products { get; set; }
public Order(int _oid, List<Product> _prods)
{
this.OrderID = _oid;
this.Products = _prods;
}
}
public static class PromotionChecker
{
//returns PromotionID and count of promotions
public static decimal GetTotalPrice(Order ord, Promotion prom)
{
decimal d = 0M;
//get count of promoted products in order
var copp = ord.Products
.GroupBy(x=> x.Id)
.Where(grp=> prom.ProductInfo.Any(y=>grp.Key==y.Key && grp.Count()>=y.Value))
.Select(grp=> grp.Count())
.Sum();
//get count of promoted products from promotion
int ppc = prom.ProductInfo.Sum(kvp=> kvp.Value);
while(copp>= ppc)
{
d += prom.PromoPrice;
copp -= ppc;
}
return d;
}
}结果:
//promotion: (2*B) and (C+D) => 45+30=75
OrderID: 1 => Original price: 195,00 | Rebate: 75,00 | Final price: 120,00
//promotion: (3*A) => 130*2=260
OrderID: 2 => Original price: 330,00 | Rebate: 260,00 | Final price: 70,00
//promotion: (2*B) => 45
OrderID: 3 => Original price: 175,00 | Rebate: 45,00 | Final price: 130,00最后注:,这只是一个主意!
还有很多需要改进的地方!我会创建Promotions类,它将实现IList接口来存储List<Promotion>。Orders也是如此。最后,我将GetTotalPrice方法移到Orders类中。
祝好运!
发布于 2020-06-18 09:59:08
听起来像是可以用复合图案解决的
就你的情况而言,它可以是:
interface IPack { decimal GetPrice(); }
class Cart : IPack
{
AProductPack APack = new AProductPack();
...
IEnumerable<IPack> packs = new []{ APack, ... };
public decimal GetPrice() => packs.Sum(p=>p.GetPrice());
public void AddProduct(Product p)
{
switch(p.Id)
{
case "A": APack.ProductsCount++; break;
...
}
}
}
class AProductPack : IPack
{
public int ProductsCount { get; set; }
public decimal GetPrice() => 130 * (ProductsCount / 3) + 50 * (ProductsCount % 3);
}它可以在将来用任何你想要的实现来扩展。例如。
class WeightPriceProductPack : IPack
{
public WeightPriceProductPack(decimal itemPrice, decimal packPrice, int packSize){ ... }
public int ProductsCount { get; set; }
public decimal GetPrice()
=> packPrice * (ProductsCount / packSize) + itemPrice * (ProductsCount % packSize);
}https://stackoverflow.com/questions/61778975
复制相似问题