PHP, lesson 15
Time to use everything from this course in one program. You'll build a small inventory manager for a corner shop. It reads commands, keeps track of stock, refuses impossible requests with clear messages and prints a sorted report. Nothing here is new; the challenge is fitting the pieces together cleanly.
Split the program by responsibility:
Product class holds one product's name, quantity and price, and protects its own rules
(you can't sell more than you have).Inventory class holds the products in an associative array keyed by name, so finding
one is a single lookup.run() function, and catches
exceptions to print error messages.Keeping input parsing, business rules and printing apart means each part stays small enough to reason about.
Give each kind of failure its own exception class, all sharing a common parent:
One exercise to try what you just read.
The code deep inside Product or Inventory simply throws with a message. The main loop
catches InventoryException (which covers both subclasses) and prints the message. Code that
detects a problem doesn't need to know how it will be reported, and the main loop doesn't need
to know every rule.
Bad commands, such as a missing argument or a quantity that isn't a number, are a different
kind of problem: the input is wrong. InvalidArgumentException fits those, and the loop can
catch both kinds with a multi-catch.
Remember that (int) "abc" is 0 rather than an error. A strict check is
ctype_digit($text), which is true only for a non-empty string of digits, so it rejects
negative numbers, decimals and words. For the price, is_numeric() is the right check.
The product's name never changes after creation, so make it public readonly. The quantity
changes only through methods that check the rules, so make it private and offer a getter.
The inventory array is keyed by product name, so ksort() puts it in alphabetical order
while keeping each product attached to its key. Use printf() with %.2f for prices, and
array_reduce() or a simple loop to add up the stock value.
You now know the core of PHP: values, control flow, functions, arrays, strings, exceptions, classes, recursion, sorting and the array function toolbox. Good next steps are interfaces and abstract classes, enums, namespaces and Composer for organising bigger projects, and PHPUnit for testing. If you're heading for the web, look at how PHP handles requests, forms and sessions, or try a framework.
Process the commands, one per line, and print a response to each:
add NAME QTY PRICE: add QTY to the product (creating it if new) and set its price to
PRICE. Print added QTY NAME.sell NAME QTY: if the product doesn't exist print error: unknown product NAME; if there
isn't enough stock print error: only N NAME left; otherwise reduce the stock and print
sold QTY NAME for T, where T is QTY × price with two decimals.report: print NAME: QTY @ PRICE for every product in alphabetical order (price with two
decimals), then stock value: V, the sum of QTY × price with two decimals.QTY that isn't a whole number made of digits,
or a PRICE that isn't numeric: print error: bad command.Blank lines are ignored. Products with 0 stock stay in the report.
Sample input:
Sample output:
add NAME QTY PRICE put stock on the shelf (a new product, or more of an existing one)
sell NAME QTY sell some stock
report list every product and the total stock value<?php
class InventoryException extends RuntimeException {}
class UnknownProductException extends InventoryException {}
class OutOfStockException extends InventoryException {}add apple 10 0.50
add pear 4 0.80
sell apple 3
sell pear 9
sell kiwi 1
fly away
reportadded 10 apple
added 4 pear
sold 3 apple for 1.50
error: only 4 pear left
error: unknown product kiwi
error: bad command
apple: 7 @ 0.50
pear: 4 @ 0.80
stock value: 6.70