В продолжение этой темы добавил свое решение (Python3), ибо понравилась задачка.
Implement a solution to the following problem in Java. We are looking for clean, well-factored, OO code.
You do not need to provide any form of persistence in this program. Your project should contain some way of running automated tests to prove it works, whether you use jUnit or some other way is up to you.
The program should be an API. You can opt to put a user interface on it or not, but we will only be looking at the API portion.
Here are the requirements:
Consider a grocery market where items have prices per unit but also volume prices. For example, doughnuts may be $1.25 each or 3 for $3 dollars.
Implement a point-of-sale scanning API that accepts an arbitrary ordering of products (similar to what would happen when actually at a checkout line) then returns the correct total price for an entire shopping cart based on the per unit prices or the volume prices as applicable.
Here are the products listed by code and the prices to use (there is no sales tax):
Product Code Price
A $1.25 each or 3 for $3.00
B $4.25
C $1.00 or $5 for a six pack
D $0.75
The interface at the top level PointOfSaleTerminal service object should look something like this. You are free to design/implement the rest of the code however you wish, including how you specify the prices in the system:
PointOfSaleTerminal terminal = new PointOfSaleTerminal(); terminal.setPricing(…); terminal.scan(“A”);
terminal.scan(“C”); … etc.
BigDecimal result = terminal.calculateTotal();
Here are the minimal inputs you should use for your test cases. These test cases must be shown to work in your program: Scan these items in this order: ABCDABA; Verify the total price is $13.25. Scan these items in this order: CCCCCCC; Verify the total price is $6.00. Scan these items in this order: ABCD; Verify the total price is $7.25
Со скидками получился не совсем clean-code.
В любом случае принимается критика от Python-гуру, и всех остальных
В частности мне не нравится реализация скидок, и доступ к ним.
В общем-то предлагайте варианты. Интересно)
terminal.py
class PointOfSaleTerminal(object):
def __init__(self):
self.prices = {}
self.discounts = {}
self.products_count = {}
def set_pricing(self, prices):
self.prices = prices
def set_discount(self, discounts):
self.discounts = discounts
def scan(self, products):
self.products_count = {x: 0 for x in self.prices} # products dict with empty values
for key in products:
if key in self.prices:
self.products_count[key] += 1
# remove products with zero values
self.products_count = {x: value for x, value in self.products_count.items() if value > 0}
def calculate_total(self):
total_price = 0
for product, count in self.products_count.items():
if (product in self.discounts) and (count >= self.discounts[product][0]):
rest = count % self.discounts[product][0]
total_price += ((count - rest) / self.discounts[product][0]) * self.discounts[product][1] + (rest * self.prices[product])
else:
total_price += count * self.prices[product]
return total_price
test_terminal.py
import unittest
from terminal import PointOfSaleTerminal
prices = {
'A': 1.25,
'B': 4.25,
'C': 1,
'D': 0.75,
}
discounts = {
'A': [3, 3], # 3 for $3.00
'C': [6, 5], # 6 for $5.00
}
class BaseTestCase(unittest.TestCase):
def setUp(self):
self.terminal = PointOfSaleTerminal()
self.terminal.set_pricing(prices)
self.terminal.set_discount(discounts)
class TestCase1(BaseTestCase):
def test_ABCDABA(self):
self.terminal.scan('ABCDABA')
result_total = self.terminal.calculate_total()
self.assertEqual(13.25, result_total)
def test_CCCCCCC(self):
self.terminal.scan('CCCCCCC')
result_total = self.terminal.calculate_total()
self.assertEqual(6.00, result_total)
def test_ABCD(self):
self.terminal.scan('ABCD')
result_total = self.terminal.calculate_total()
self.assertEqual(7.25, result_total)
if __name__ == '__main__':
unittest.main(verbosity=2)
test_ABCD (__main__.TestCase1) ... ok
test_ABCDABA (__main__.TestCase1) ... ok
test_CCCCCCC (__main__.TestCase1) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK