In this chapter, we will use modular programming techniques to implement a non-trivial program. Along the way, we will:
Learn about the divide and conquer approach to program design
Examine the tasks our program needs to perform
Look at the information our program will need to store
Apply modular techniques to break our program down into individual parts
Figure out how each part can be implemented as a separate Python module
See how the various modules work together to implement our program's functionality
Follow this process to implement a simple but complete inventory control system
See how modular techniques allow you to add functionality to your program while minimizing the changes that need to be made
The inventory control system
Imagine that you have been asked to write a program that allows the user to keep track of the company's inventory—that is, the various items the company has available for sale. For each inventory item, you have been asked to keep track of the product code and the item's current location. New items will be added as they are received, and existing items will be removed once they have been sold. Your program will also need to generate two types of reports: a report listing the company's current inventory, including how many of each type of item there are in each location, and a report that is used to re-order inventory items once they have been sold.
Looking at these requirements, it is clear that there are three different types of information that we will need to store:
A list of the different types of products that the company has for sale. For each product type, we will need to know the product code (sometimes called an SKU number), a description, and the desired number of items that the company should have in its inventory for that type of product.
A list of the locations where inventory items can be held. These locations might be individual shops, warehouses, or storerooms. Alternatively, a location might identify a particular shelf or aisle within a shop. For each location, we need to have a location code and a description identifying that location.
Finally, a list of the inventory items that the company currently holds. Each inventory item has a product code and a location code; these identify the type of product and where the item is currently held.
When running the program, the end user should be able to perform the following actions:
Add a new item to the inventory
Remove an item from the inventory
Generate a report of the current inventory items
Generate a report of the inventory items that need to be re-ordered
Quit the program
While this program is not too complicated, there are enough features here to benefit from a modular design, while still keeping our discussion relatively brief. Now that we have taken a look at what our program needs to do and the information we need to store, let's start applying modular programming techniques to the design of our system.