CSE 143 Autumn 2000

Homework 3

Due: Electronic submission by 10 pm, Wednesday, Oct. 25. 
Paper receipt due in quiz section on Thursday Oct 26.

 

Note: BookInfo.h and Inventory.h have been changed. Please download them again if you have not done so. Some const lines have been changed.

Overview

You run a small-town bookstore, and so far, you've been able to keep track of your inventory on 3x5 cards. However, with the Harry Potter craze, among other things, you've decided to change your inventory system to the computer. The task is to build a program that tracks bookstore inventory for a day. To make it interesting, the sample inventory file is for July 8, the date Harry Potter 4 was released to the public.

Concepts

The purpose of this assignment is to gain experience with the following new concepts:

Program Synopsis

The program handles transactions for a day's business at a small bookstore. It does NOT deal with finances, just with inventory. (You don't need to worry about how much books cost.) When it starts, it reads an inventory file listing stock on hand. Then it repeatedly processes five types of transactions:

  1. Accept a shipment of books with a given title, author and genre (Sci-Fi/Fantasy, Fiction, Classics, or Childrens).
  2. Sell a book. The customer must know the exact title desired.
  3. Display a list of all books by a given author.
  4. Display a list of all books in a given genre.
  5. Quit for the day.
At the end of the day, the program writes an updated inventory file. It also prints to the screen a list of all the books that have been sold out, so they can be re-ordered for the next day. A sample inventory list is here. It is just a text file with entries for a set of books.

All the actual file i/o and input prompts have been writen for you in Bookstore.cpp. In addition, we have writen starter header files for two classes, Inventory.h and BookInfo.h. You must add whatever private members and data storage you would like to Inventory.h and BookInfo.h. You may also add public functions to BookInfo.h that you would like to call in Inventory.cpp. You must write from scratch book Inventory.cpp and BookInfo.cpp. (If for some reason you need the OLD, OBSOLETE, files, click here)

Program Details

  1. When the program starts, it prompts the user for the name of the file containing the previous day's inventory list, and reads it in. Your tasks here are to implement BookInfo::reset, that merely resets all the data in a BookInfo object, and to implement Inventory::insert, so we can insert an unlimited number of BookInfo objects.

  2. The program will use code letters to specify the different kinds of transactions (it doesn't have to handle natural language input).  There are 5 possible codes.  If the user enters a code other than the ones below, we print an appropriate error message and go on to the next command.  The codes and their meanings are:
    d - deliver books
    b - buy a book (title known)
    a - print a list of books in the bookstore given the author's name
    g - print a list of books that the bookstore has in a given genre
    q - close the shop and quit for the day (writing an updated inventory file and a re-order list)
    All of this is implemented for you.

  3. If the transaction is to deliver books, the program asks the user how many books they are delivering, then it asks for the title, author and genre in that order. (NB: This assumes a very knowledgeable delivery person, but perhaps it's written on the outside of the carton.) Only one book can be delivered at one time, though arbitrarily many copies can come in a shipment.  The author's name is given with last name first, a comma then first name or initials. The title and author must be followed by a pound sign (with white space on both sides).   Example (user input is underlined):

    Please enter how many books you have to deliver:
    22
    Please enter the title of the books followed by a pound sign:
    Ozma of Oz #
    Please enter the author of the books (last name, initials or first name) followed by a pound sign:
    Baum, L. Frank # 
    Please enter book genre: 1 = Sci-Fi/Fantasy, 2 = Fiction, 3 = Classics, 4 = Childrens.
    4
    Your task here is to create a BookInfo constructor that takes in the title, author, genre and #copies, and to make sure that you can use Inventory::insert here as well as when reading the inventory initially.

  4. If the transaction is to buy a book, the program asks for the title. Tell the user how many copies you have, and if you have any, ask them how many copies they want to buy. NB: If they ask to buy more copies than you have, you can only sell as many as you have. If you sell the last copy of a book, remove it from your inventory list but keep track of it so you can reorder it at the end of the day.  Example:

    Please enter the title of the book you'd like to buy:
    Family Tree
    We have 10 copies. How many would you like to buy? (0-10)
    1
    Here you go. Have a nice day.

    Your task here is to implement Inventory::title_search that returns a copy of the BookInfo object with given title or, if it's not in stock, a BookInfo object with the genre ERROR and the number of copies 0. You must also implement BookInfo::getGenre, bookInfo::getNumCopies and Inventory::sell. (NB: Inventory::insert will be called here too.) Inventory::sell should decrease the number of copies you have in the inventory of that book or remove it from the inventory if you sold the last copy.

  5. If a customer wants to check your inventory for a given author, you should return a list of all the books you have by that author with both their title and the number of copies you have. The list should be sorted by title.  Example:

    Please enter the author (last name, first name):
    Rowling, J.K.
    We have:
    Harry Potter and the Chamber of Secrets by Rowling, J.K.; 21 copies
    Harry Potter and the Goblet of Fire by Rowling, J.K.; 144 copies
    Harry Potter and the Prisoner of Azkaban by Rowling, J.K.; 42 copies
    Harry Potter and the Sorcerer's Stone by Rowling, J.K.; 54 copies

    For this, you must implement Inventory::author_search (putting copies of all the BookInfo objects with one author into another inventory). You must also implement Inventory::sort and Inventory::print.
    NB: Don't worry about people asking for Rowling, J.K. vs. just Rowling. Though it's obviously wrong, if someone asks for books by Rowling, you can just say you have nothing by that author if the author's name was given as Rowling, J.K. in the original inventory, or when the book was delivered.

  6. If a customer wants to see what you have in a given genre, it works similarly, but there are only a few genres you can use, which are coded by numbers. The output should be sorted by author and then by title.  Example:

    Please select the genre: 1 = Sci-Fi/Fantasy, 2 = Fiction, 3 = Classics, 4 = Childrens.
    1
    Our inventory is:
    Dragonsinger by McCaffrey, Anne; 3 copies
    Dragondrums by McCaffrey, Anne; 3 copies
    Dragonsong by McCaffrey, Anne; 4 copies
    Masterharper of Pern by McCaffrey, Anne; 1 copies
    Firebrand by Bradley, Marion Zimmer; 8 copies
    Mists of Avalon by Bradley, Marion Zimmer; 6 copies
    Family Tree by Tepper, Sheri S.; 10 copies
    For this, you must implement Inventory::genre_search (putting copies of all the BookInfo objects in one genre into a second inventory). You must also have implemented Inventory::sort (sorting by author and then by title) and Inventory::print.

  7. When the user types (q) quit, they will be queried the new inventory file's name. It will write the current inventory list to the new file in the same format as the original inventory file. (A good test is to run the program for a while, quite and create an updated inventory file, then rerun the program using the new file as input and verify that it works correctly. You should also compare the two files directly to see see that all the correct changes occurred.)  In addition to the new inventory file, the program will display on the screen a list of all books that have been sold out and need to be reordered, sorted by author then by title. Note that sold out books should not be listed in the new inventory file, since you have no copies until they are delivered.
    For this to work, you must successfully implement Inventory::sort (required twice before), Inventory::numBooks and Inventory::getBook.

  8. Although destructors are never explicitly called in your code, you will also need to implement a destructor for your Inventory class.

Implementation Requirements

A key objective of this assignment is to gain experience with dynamic memory allocation in classes that contain constructors, destructors, and appropriate use of public, private, const, and other ingredients of a proper C++ class.

For this assignment, we have begun to define at least the following two classes:

Class BookInfo will have multiple constructors, but it probably does not need a destructor, since it is does not contain explicit pointers to dynamically allocated data. Feel free to add more public methods in this class if your inventory class needs them. You must also define all the private data and methods, and implement all the public ones we have defined.

Class Inventory will need a proper destructor but only one constructor.  The Inventory should maintain an array of BookInfo objects, but it must not have a fixed maximum capacity.  When an attempt is made to add a new book to the Inventory, if the array is already full, then it's capacity should be doubled and the new book added.  That is, if the array is already full, dynamically allocate a BookInfo array with twice the number of elements as the current one, copy everything from the old array to the new one, delete the old array, and add the new book. You will need to define private data and methods in this class, but you should not need any new public ones.

Other Implementation Requirements

Implementation Hints

Download Files

Bookstore.cpp DO NOT MODIFY
BookInfo.h to be modified
Inventory.h to be modified
bookstore executable sample solution. Your program should work just like this one does.

Electronic Submission

When you've finished your program, turn in your modified BookInfo.h, your modified Inventory.h and your implemented BookInfo.cpp and Inventory.cpp. You cannot turn in Bookstore.cpp, since you are not allowed to modify it. Use this turnin form.  Print out the receipt that appears, staple it, and hand it in during quiz section.


Here a copies of the original Inventory.h and BookInfo.h. These are obsolete and should not be used.