feriatos / sigma-test-task
Sigma Software 的测试任务
v0.0.1
2020-05-17 11:07 UTC
Requires
- php: >=7.2.0
Requires (Dev)
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-09-17 20:27:58 UTC
README
此库允许创建POS终端,并根据不同的定价策略计算购物车的价格。
如何安装
composer require "feriatos/sigma-test-task:*"
如何使用
首先创建定价策略并添加产品
$pricing = new OneTierPricing(); $pricing->addProduct(new OneTierProduct('ZA', 2, 7, 4)); $pricing->addProduct(new OneTierProduct('YB', 12)); $pricing->addProduct(new OneTierProduct('FC', 1.25, 6, 6)); $pricing->addProduct(new OneTierProduct('GD', 0.15));
然后创建终端并设置定价
$terminal = new POSTerminal(); $terminal->setPricing($pricing);
扫描项目
$terminal->scanItem('ZA'); $terminal->scanItem('YB'); $terminal->scanItem('FC'); $terminal->scanItem('GD'); $terminal->scanItem('ZA'); $terminal->scanItem('YB'); $terminal->scanItem('ZA'); $terminal->scanItem('ZA');
并获得结果
$totalPrice = $terminal->getTotal();
要清理项目,请调用
$terminal->resetItems();
如何扩展
此库利用策略设计模式。您只需实现 PricingInterface
和 ProductInterface
,并添加您自己的定价策略(例如两阶或三阶定价策略)即可扩展它。