Exercise 2: Container Inventory

During this lab, you will create and hand in 1 file:

  • inventory.py - This program may be completed in pairs if you wish.

Background: Setting the stage

You work for a specialty shipping company and have many containers that are shaped as either spheres, rectangular prisms, cones, or cylinders. You notice that your boss is performing inventory on paper and keeping the results in a file cabinet (yikes!). Beyond counting, it’s also important to the business to understand the cumulative volume and surface area of the containers of each shape. Since your boss is doing this by hand, it’s prone to human error and you think this is the perfect opportunity to create a program to do the calculation for you and keep track of the results.

Program Requirements

In high level terms, your program is going to:

  1. Welcome the user with a message.
  2. Prompt the user for a command. The user can take one of 4 actions:
    1. Add a new container. The program should prompt the user for the shape and appropriate dimensions, and calculate volume and surface area of the container.
    2. Print the volumes. The program should print the cumulative volume for each possible shape. That is, the program should print the volume of all spheres, all rectangular prisms, etc.
    3. Print the surface area. The program should print the cumulative surface area for each possible shape as above.
    4. Print the container counts. The program should print the number of objects that were added for each shape.

Some specific requirements:

  1. The commands your program recognizes must be: add, printV, printSA, printNum, and quit.
  2. The shapes your program accepts must be: sphere, rect, cone, and cylinder.
  3. User prompts must be exactly the same as the prompts and output messages in the Example Usage section. This includes letter case (a is not the same as A) and spacing.

    Following this instruction is very important. Please ask questions if you are unsure of something. You will lose points if you do not follow the sample exactly.
  4. You must comment your code! This will always be a requirement, and it's a great way to get some easy points even if your program doesn't work exactly right. (And it's a silly way to lose points on an otherwise-working program.)

Example Usage

User input is bold blue.

Welcome to the container inventory program!

What would you like to do? (add, printV, printSA, printNum, quit): add
What shape is the container? (sphere, rect, cone, cylinder): rect
Length (m): 0.5
Width (m): 0.5
Height (m): 1.0
What would you like to do? (add, printV, printSA, printNum, quit): add
What shape is the container? (sphere, rect, cone, cylinder): sphere
Radius (m): 2
What would you like to do? (add, printV, printSA, printNum, quit): add
What shape is the container? (sphere, rect, cone, cylinder): rect
Length (m): 3.5
Width (m): 9.2222
Height (m): 11
What would you like to do? (add, printV, printSA, printNum, quit): add
What shape is the container? (sphere, rect, cone, cylinder): cylinder
Radius (m): 9
Height (m): 9.2
What would you like to do? (add, printV, printSA, printNum, quit): printV
Total volumes (m^3):
Sphere: 33.5103216383
Rectangular Prism: 355.3047
Cone: 0
Cylinder: 2341.11484546
What would you like to do? (add, printV, printSA, printNum, quit): printSA
Total surface areas (m^2):
Sphere: 50.2654824574
Rectangular Prism: 346.9438
Cone: 0
Cylinder: 1029.18575332
What would you like to do? (add, printV, printSA, printNum, quit): printNum
Object counts:
Sphere: 1
Rectangular Prism: 2
Cone: 0
Cylinder: 1
What would you like to do? (add, printV, printSA, printNum, quit): quit
Goodbye!

Note: cones should be assumed to be right circular cones, and would be prompted as follows:

What would you like to do? (add, printV, printSA, printNum, quit): add
What shape is the container? (sphere, rect, cone, cylinder): cone
Radius (m): 2.55
Height (m): 3
What would you like to do? (add, printV, printSA, printNum, quit): printV
Total volumes (m^3):
Sphere: 0
Rectangular Prism: 0
Cone: 20.42820623
Cylinder: 0
What would you like to do? (add, printV, printSA, printNum, quit): printSA
Total surface areas (m^2):
Sphere: 0
Rectangular Prism: 0
Cone: 51.9703293883
Cylinder: 0

How do we approach this?

There are always multiple ways to accomplish the same thing in a program. You are free to approach this however you’d like although as mentioned above your program’s output needs to be exactly as specified and shown in this document. We will walk through one potential approach in the below steps.

Note: we will periodically show sample output while walking through these steps. These outputs are just for checking that your program is correct as you incrementally progress but should be removed before submitting the final program.

Step 1: Shape calculations

Our program will need to be able to calculate the volume and surface area of four different 3D shapes. A good start would be to create two functions for each shape: one to calculate the volume, and one to calculate the surface area.

For example (feel free to use this code!), the function for calculating a sphere’s volume could be:

def calc_sphere(r):
    return (4.0/3.0) * math.pi * math.pow(r,3)

Create a function for calculating a sphere’s surface area, and do the same for the other three shape types.

Next, we will add some user interactivity. Prompt the user to enter a shape. Time to use those fancy conditional statements! Based on the shape, prompt the user to enter the parameters required to calculate that shape’s surface area and volume. You can now call your volume and surface area functions for that shape and print the results.

An example of the program now:

What shape is the container? (sphere, rect, cone, cylinder): sphere
Radius (m): 9.2
Volume: 3261.76066698
Surface area: 1063.6176088

Step 2: Put this in a loop

Since our program should accept more than a single shape, let’s put the code from Step 1 (except the function definitions – these should always be SEPARATE from your main code!) in a while loop! One possible approach is to place it in an infinite loop. This means the condition that is checked before executing the code inside the loop is ALWAYS true.

Now, your program should be repeatedly performing prompting for a container shape, parameters, and printing the results.

Since we want to keep track of the cumulative volume and surface area for all containers added of each shape, think about how you would do this (hint: it requires a new variable for each!) and make that change.

An example of the program now:

What shape is the container? (sphere, rect, cone, cylinder): sphere
Radius (m): 1.3
Cumulative volume of spheres: 9.20277207992
Cumulative surface area of sphere: 21.2371663383
What shape is the container? (sphere, rect, cone, cylinder): sphere
Radius (m): 1.3
Cumulative volume of spheres: 18.4055441598
Cumulative surface area of sphere: 42.4743326765
What shape is the container? (sphere, rect, cone, cylinder): rect
Length (m): 9.5
Width (m): 1.5
Height (m): 5.6778
Cumulative volume of rectangular prisms: 80.90865
Cumulative surface area of rectangular prisms: 153.4116
What shape is the container? (sphere, rect, cone, cylinder):

Notice how the loop just keeps going! Make sure to stop your program using the stop button; if you leave it going, it'll start eating up your RAM and you'll notice some serious slow-down in your computer's performance:

Step 3: Add the overall menu

We want this program to do more than just continuously add shapes. Add a user prompt to ask for a command. The user prompt has to be 'What would you like to do? (add, printV, printSA, printNum, quit): '

Based on the command, we want to take different actions. See them below:

  • Command 'add' should ask for a shape, then the appropriate parameters, and add the resulting volume and surface area to a cumulative variable and increment the count. (exactly what we were doing in step 2, but for a SINGLE shape (shouldn’t continuously be asking for new shapes)
  • Command 'quit' should display the message 'Goodbye!' and end the loop, thus terminating the program. Think about how to exit out of a loop.
  • Command 'printV' should display the message 'Total volumes (m^3): ' and then the cumulative volumes for all four shape types.
  • Command 'printSA' should display the message 'Total surface areas (m^2): ' and then the cumulative surface area of all four shape types.
  • Command 'printNum' should display the message 'Object counts: ' and then the counts of all four shape types (the count is the number of shapes of that type that were added).

Add the functionality for each command.

Now, add a welcome message to the program, 'Welcome to the container inventory program!'.

Congratulations! Your program should now be complete. Look at the Example Usage section and test your program with the exact inputs shown. Your console output should look exactly like it. Try other cases and make sure your program works as expected.

Submitting your files

As usual, you'll be handing in your lab work via the course Learn@UW dropboxes. Navigate to our 301 course page, and click the Dropbox link in the top navigation bar. You should see a dropbox for Program 2 - this is where you should hand in your inventory.py file. (If you worked in a pair, only one person will need to hand in the code - but make sure both names are in the file.)

Note that the dropbox will close at 12 noon on 11 February, so be sure to submit your files before then.