Homework 7: Grocery List

Assigned
  • April 17, 2024
Due
  • April 22, 2024 11:59pm

For this assignment, you will implement a simple grocery list program. The program will use a dynamic array to keep track of items on the list, which include both a name and a count. Review all of the sections below for specific requirements.

Basic Operation

When you start the program, it should prompt the user to enter a command like this:

$ ./grocery-list
What do you want to do? Type one of the following commands:
  add: add an item
  lookup: look up an item
  print: print the list
  exit: exit the program

Command: 

The user can then enter one of the four operations.

The add Command

When the user types the command add, the program should ask the user to enter an item name and count like this:

What item would you like to add?
carrots
How many would you like?
3

The program should add the item to the list and then return to the main menu and ask for a new command.

Invalid Inputs

If the user gives an invalid count (a negative number, or an input that isn’t a number), the program should print an error and ask the user to try again:

What item would you like to add?
avocados
How many would you like?
x
Invalid number. Try again.
How many would you like?

Duplicate Items

If the user enters an item that is already on the list you can handle this however you like. You can return an error, ask for a new count and add to the existing count, or add the item again with a new count. Just make sure your program handles this case without an error.

The lookup Command

When the user types the command lookup, the program should ask the user to enter an item name. If the list contains a matching item it should work like this:

What item are you looking for?
carrots
You need 3 carrots

But, if the list does not contain a matching item the output should be:

What item are you looking for?
avocados
I didn't find avocados on the list.

In either case, return to the main menu and ask for a new command.

The print Command

When the user types the command print, print the entire list. If the list is empty, the output should be:

Grocery List:
  The list is empty

But, if the list contains items it should print them in the order they were added:

Grocery List:
  carrots: 3
  avocados: 9
  cheese: 14

Return to the main menu and ask for a new command after printing the list.

The exit Command

When the user types exit, the program should free all allocated memory and exit without printing any additional output.

Invalid Commands

If the user enters an invalid command, print an error and try again:

What do you want to do? Type one of the following commands:
  add: add an item
  lookup: look up an item
  print: print the list
  exit: exit the program

Command: remove
Unrecognized command.

What do you want to do? Type one of the following commands:
...

Requirements and Assumptions

As you complete your implementation, you may assume that no line of input from the user will be longer than 128 characters. That includes command inputs, item names, and text the user types to specify a number.

Your implementation must meet the following requirements:

  1. You must use a heap-allocated array to hold the grocery list
  2. You may not use any global variables in your implementation.
  3. You must free all allocated memory before the program exits.
  4. Your program must run correctly when built with AddressSanitizer (there should be no reported errors).

Hints

You may find it useful to create a struct to represent one grocery item. An item has a name and a count. Your list would be an array of these items.

You should break your program up into functions to make the code readable. One good way to do this is to create a function for each command (except exit, perhaps). That separates the code you write to deal with command input from the behavior of each command. You’ll need to think carefully about how the add command can modify the grocery list array, though.

Grading

Your assignment will be graded on a scale of 0–-100 points based on the following criteria:

  • The program includes a working Makefile with correctly-defined targets all, clean, and grocery-list (10 points)
  • The program can add items to the grocery list (25 points)
  • The program can look up items in the grocery list (20 points)
  • The program can print the grocery list (20 points)
  • The program can exit cleanly (10 points)
  • The program includes a thorough testing plan in a file named tests.txt (15 points)
    • Describe how you would test all the behaviors specified in this assignment
    • Test at least two additional edge cases to ensure your implementation handles them gracefully (i.e. it does not crash)
    • Use the tests.txt format from homework 4.

Code Quality

There will be potential deductions for any code quality violations. There are eight requirements for quality code on this assignment:

  1. Indent the body of all loops and conditionals one level deeper than the code just outside the loop. Be consistent with your indentation style.
  2. Use curly braces for every loop or conditional body, regardless of the number of lines.
  3. Include comments that explain what your code does. There should be at least one comment by the start of each loop and ifelse block that explains what the purpose of that code is. The comment should not simply restate the code; it should add information for a human who reads the code and is confused.
  4. Give variables descriptive names that help a reader understand what they will be used for.
  5. Do not read from or display the value of any variable without first initializing it.
  6. Your code must compile with no errors and no warnings.
  7. Your code may not use any global variables.
  8. Your code must not contain errors such as memory leaks, use-after-free errors, double frees, or buffer overruns. You can check for this by building the program with AddressSanitizer and running through your test cases.