-1

Consider a store where items have prices per unit but also volume prices. For example, apples may be $1.00 each or 4 for $3.00.

Implement a point-of-sale scanning API that accepts an arbitrary ordering of products (similar to what would happen at a checkout line) and 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 | $2.00 each or 4 for $7.00

B | $12.00

C | $1.25 or $6 for a six pack

D | $0.15

There should be a top level point of sale terminal service object that looks something like the pseudo-code below. You are free to design and implement the rest of the code however you wish, including how you specify the prices in the system:

terminal.setPricing(...) terminal.scan("A") terminal.scan("C") ... etc. result = terminal.total

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: ABCDABAA; Verify the total price is $32.40. Scan these items in this order: CCCCCCC; Verify the total price is $7.25. Scan these items in this order: ABCD; Verify the total price is $15.40.

2
  • At a high-level, what was your suggested solution? Commented Apr 12, 2012 at 23:46
  • do you just need to write something that works, are there any restrictions on how fast it has to be or how it has to be designed? Commented Apr 12, 2012 at 23:47

1 Answer 1

4

1) For each item store the unit price, the group price and the units per group.

2) During the scanning phase simply keep track of the number of units per item.

3) For each item, increment the cost by:

(number units) / (units per group for item) * group price + (number units) % (units per group for item) * unit price 

A linear amount of space is used on the order of the number of items and a linear amount used to track the counts of the items scanned. Runtime is also linear.

You can implement In PHP : test.php

<?php echo "Please input the product code string:"; $handle = fopen ("php://stdin","r"); $line = fgets($handle); echo 'input : ' .$line. "n"; $inputProducts = rtrim($line); $total = 0; $inputArray = str_split($inputProducts, 1); $counts = array_count_values($inputArray); $productsprice = array('A'=>array('1'=>2.00, '4'=>7.00), 'B'=>array('1'=>12.00), 'C'=>array('1'=>1.25, '6'=>6.00), 'D'=>array('1'=>0.15)); foreach($counts as $code=>$amount) { echo "Code : " . $code . "n"; if(isset($productsprice[$code]) && count($productsprice[$code]) > 1) { $groupUnit = max(array_keys($productsprice[$code])); $subtotal = intval($amount / $groupUnit) * $productsprice[$code][$groupUnit] + fmod($amount, $groupUnit) * $productsprice[$code]['1']; $total += $subtotal; } elseif (isset($productsprice[$code])) { $subtotal = $amount * $productsprice[$code]['1']; $total += $subtotal; } echo "Subtotal: " . $subtotal . "n"; } echo 'Final Total: $' . number_format($total, 2). "n"; ?> 

Execute CLI: php test.php

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.