Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $items = [];
- while(true) {
- $input = explode(" ", readline());
- if($input[0] == "buy") {
- break;
- }
- $name = $input[0];
- $price = $input[1];
- $qty = $input[2];
- if(!key_exists($name, $items)) {
- $items[$name] = [];
- $items[$name]["price"] = 0.0;
- $items[$name]["qty"] = 0;
- }
- $items[$name]["price"] = (float) $price;
- $items[$name]["qty"] += $qty;
- }
- //Math
- foreach($items as $item => $values) {
- $total = $values['price'] * $values['qty'];
- $items[$item] = $total;
- }
- // Output
- foreach($items as $item => $total) {
- printf("%s -> %.2f\n", $item, $total);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement