CMSC-341 Spring 2005

Project 1

Assigned

Monday, February 7, 2005

Due

11:59pm Sunday, February 20, 2005


Background

Abstract Data Types (ADTs) are a central idea of this course and of object-oriented programming in general. Recall that an ADT has two parts: (1) a description of the elements that make up the type, and (2) a description of the operations allowed on instances of the type. The ADT idea is implemented in C++ as a class. This project will give you some exercise in using ADTs.

Another important OOP idea, parametric polymorphism, is implemented in C++ by templates. This project will give you some practice with C++ templates.

You will be given a makefile, including headers from multiple directories, compile code from multiple directories, and use a set of class libraries. These are commonly used techniques in industry, so they're worth learning for future reference. You will be responsible for creating your own makefiles for all other projects.


Description

In this project, you will implement a simple inventory system for a hypothetical business. For simplicity, we assume that each business buys and sells only one type of products, and the  inventory list it maintains will contain only products of that type. Two types of businesses will be considered in this project: a vendor for electronics and a stock trader. There are two primary operations involved: buy and sell. For simplicity, we assume the unit purchase price for any  product (electronics or stock) never changes. However, the unit sell price will be different from the purchase price, and the sell prices for electronics and stocks are calculated differently.

You are required to maintain an inventory list for all products currently in the possession of the business. The inventory list is organized as follows. Each entry on the list represents one particular product with its unique name, and it records the number of all units of that product it owns and the unit purchase price. 


In addition, a cash account is also maintained for the business, cost of purchases will be paid from this account, and proceeds of sells will be added to this account. A buy order can be executed only if there is enough cash, and a sell order can be executed only if there are enough units of that product in the inventory.

Your program will read from a file containing a sequence of business orders (buy and sell) and other operations related to inventory maintenance, executing them if they are executable, and updating the inventory list and the cash balance.  The program shall be invoked with a command line, which, among other things, gives the  path of the file containing all operations. Details of these are given in Command Line Section and Command File Section.

Here are your specific tasks:

  1. Read the brief introduction to the STL at the following URL:
    http://www.msoe.edu/eecs/cese/resources/stl/index.htm
  2. Read and understand the description of the STL version of the vector ADT at the following URL:
    http://www.msoe.edu/eecs/cese/resources/stl/vector.htm
  3. Read and understand the description of the ANSI/ISO C++ standard library version of the string ADT at the following URL:
    http://www.msoe.edu/eecs/cese/resources/stl/string.htm
  4. Implement the following  classes that are used to form and maintain the inventory of a business.

      Electronics
      Stocks
      Inventory

ADTs for these classes and related operations are described in ADT Section.

  1. Write Proj1.cpp and any other support files needed to implement the functionality required of this project. 
  2. Copy the makefile from
    /afs/umbc.edu/users/y/p/ypeng/pub/CMSC341/Proj1s05/Makefile
    to your own directory and modify it as needed. It can be used without modification if you follow the file names in the Makefile.
  3. Answer the questions posed in 341-Spring05-proj1_questions.txt. Copy the file<>
    /afs/umbc.edu/users/y/p/ypeng/pub/CMSC341/Proj1s05/341-Spring05-p1_questions.txt to your own directory and edit it to provide your answers to the questions. Don't forget to submit the edited file; it's 10% of this project's grade.

Definition of the ADT

Overview

For each class described below, all data members must be private. Other than this requirement and any others explicitly stated in the description of a class, you have the freedom to design the class as you see fit. However, remember that you will be graded on the quality of that design. Be sure to note that it is generally a good idea to write an explicit destructor, copy constructor, and assignment operator, even if they are never called explicitly in your code, because they are often called implicitly.

Electronics

A object in this class represents a record of a particular electronics product in the inventory. It specifies

  • the name (a unique ID) of the product,
  • the number of units of that product, and
  • the unit purchase price. For simplicity, wee assume the same product (i.e., same name) will have the same purchase unit price regardless when it is bought.

This class should have a function that computes the unit sell price, which equals the purchase price times 1.1 (e.g., a 10% profit margin). It should also have an overloaded non-member function operator <<. This operator outputs the attributes of the Electronics. ( See more on this in the Employee class, p. 35 in the Weiss text on page 35). 

Stocks

A object in this class represents a record of a particular stock in the inventory. The  data members and function members are the same as for Electronics class, except the function for computing the unit sell price. To reflect the fluctuation of stock market, the following formula is used to determine the sell price for stocks:
          sell price = purchase price * r
where r is a random number between 0.9 and 1.1.  How to generate r is given in Random Number Generation Section.

Inventory:

This class shall be implemented as a template class. An  Inventory contains a list of zero or more products of the same type (either Electronics or Stocks in this project) that are currently in the possession of the business (e.g., bought but not yet sold). Entries on the inventory list must have distinct names, and the number of units must be positive. Besides the inventory list, there is also a cash account.

The following operations should be supported by Inventory class:

  • Function that buys a given number of units of a given named product for a given unit price. It updates the inventory list by either adding a new entry (if the given product name does not appear in any entry on the list) or update the existing entry for that named product. It also subtracts the cost from the cash account. The cost is computed as the product of the number of  units to buy and the given unit purchase price. Exception occurs if the cost is greater than the current cash balance, the order will not be carried out and an error message shall be printed.
  • Function that sells a given number of units of a given named product. It updates the inventory list by subtracting the number of units to sell from the entry on the list for that named product, and removes that entry if the unit number becomes zero. It also adds the proceeds of the sale to the cash account. The proceeds is computed as the product of the number of  units to sell and the unit sell price. Exception occurs if the number of units to be sold is more than you have in the inventory, the order will not be carried out and an error message shall be printed.
  • Function that sets up the start cash balance.
  • Function that returns the current cash balance.
  • Function that returns total number of items on the inventory list.
  • An overloaded non-member function operator << That outputs all items the inventory list currently holds.

 


Random Number Generation

There are many functions for random number generation in the Standard Library. In this project you MUST use functions srand and rand. Function srand sets the seed for the random number generator rand. rand generates one random integer between  0 and RAND_MAX, each time when it is called. You can use the following procedure to generate r used for stock sell price:
 

  1. Call srand(seed) only once at the beginning of each execution of your program. To make all outputs in the class consistent, use seed = 12345 in your program;
  2. Call rand for each sell opertion and convert the random number to r between 0.9 and 1.1 by r = 0.9+0.2*rand()/RAND_MAX.


Be sure to include <cstdlib>.


The Command Line
 

Project 1 will be invoked with a command line that consists of two arguments. The first argument will be "Electronics" or "Stocks", telling the program the type of the business (and the type of items in the inventory). The second argument is the  the name of a file that contains a set of operations that must be performed on the inventory in that order. The format of this file is described in the Command File Section section below.

Note that you must check command line arguments to ensure that they are valid, e.g. that the command file can be opened, and print an appropriate message if an error is found.

An example command line looks like
Proj1 Stocks /afs/umbc.edu/users/y/p/ypeng/pub/cs341s05/test/Proj1/test1


The Command File

Commands in the file specify operations to be performed on the inventory. Each line in the file represents one command. Blank lines may appear anywhere in the file and should be ignored. Otherwise, you can assume that any line containing a command is syntactically well-formed. We make this assumption so you don't have to spend lots of time making the command file parser bullet proof.

The allowed commands in command file and their formats follow:

  • CASHINIT <amount> -- initializes the cash account to the amount (of US dollars) given
  • BUY  <name> <number of units> <unit-price>  -- buys the given number of units of the named product at the given unit price.
  • SELL<name> <number of unit> -- sells the given number of units of the named product.
  • COUNTLIST <> -- prints total number of entries on the inventory list
  • PRINTLIST<> -- prints all entries on the inventory list.
  • CASHBALANCE<> -- print the current cash balance


Please note the following.

  • All commands will be well-formed and values will be of the appropriate type. For example, the names of the products contain no spaces
  • The command file can have multiple commands of all types.

Sample Output

Assuming the type of products is specified in command line as Electronics, the following command file

 

CASHINIT 10000
CASHBALANCE
PRINTLIST
BUY DVD-0001 10 149.99
BUY CANNON-5  5 399.99
PRINTLIST
CASHBALANCE
BUY CANNON-5 5 399.99
COUNTLIST
PRINTLIST
CASHBALANCE
SELL DVD-0001 6
SELL DVD-0001 6
SELL DVD-0001 4
COUNTLIST
PRINTLIST

BUY SONY-PLASMA-50 2 4500.00


 produces the following output:

 
Cmd: CASHINIT 10000
Cmd: CASHBALANCE
     10000
Cmd: PRINTLIST
     The inventory list is empty
Cmd: BUY DVD-0001 10 149.99
Cmd: BUY CANNON-5  5 399.99
Cmd: PRINTLIST
     The current inventory list is
        DVD-0001 10 149.99
        CANNON-5  5 399.99
Cmd: CASHBALANCE
     The current cash balance is 6500.15
Cmd: BUY CANNON-5 5 399.99
Cmd: COUNTLIST
     The number of distinct products in the inventory is 2
Cmd: PRINTLIST
     The current inventory list is
        DVD-0001 10 149.99
        CANNON-5 10 399.99
Cmd: CASHBALANCE
     The current cash balance is 4500.2
Cmd: SELL DVD-0001 6
Cmd: SELL DVD-0001 6
     Error message: there isn't enough units of DVD-0001 to complete this order
Cmd: SELL DVD-0001 4
Cmd: COUNTLIST
     The number of distinct products in the inventory is 1
Cmd: PRINTLIST
     The current inventory list is
       CANNON-5 10 399.99
Cmd: BUY SONY-PLASMA-50 2 4500.00
     Error message: there isn’t enough cash to complete this order


Note that this output was NOT created by executing any program, but generated artificially by hand. Note also that it is required that every command that is read from the command file is echoed as part of the output.


Files To Be Submitted

Submit all files required to build an executable named Proj1 by running make.
Also submit 341-Spring04-proj1_questions.txt - with your answers to the questions.

Submit the files using the procedure given to you in the class.
If your makefile is set up correctly, you should be able to execute the command make submit.

Submit Tools

There are a number of tools available to you to check on your submittal. It is your responsibility to ensure that the submittal is correct and will result in a successful
compilation of your project. Do not wait till the last minute to submit your files. Give yourself enough time to check that your submittal is correct.

If you don't submit a project correctly, you will not get credit for it. Why throw away all that hard work you did to write the project? Check your
submittals. Make sure they work. Do this before the due date.

Documentation for the submit program is on the web at http://www.gl.umbc.edu/submit/. One of the tools provided by the submit program is
submitls. It lists the names of the files you have submitted.

Additionally, there are two programs for use only by CMSC-341 students (not part of the UCS submit program). They are in the directory
/afs/umbc.edu/users/d/e/dennis/pub/CMSC341/ and are named submitmake and submitrun. You can use these programs to make or run your submitted projects.

The syntax is similar to that for submit:

submitmake <class> <project>

Example:  submitmake cs341 Proj1

This makes the project, and shows you the report from the make utility. It cleans up the directory after making the project (removes .o and ii_files), but leaves the
executable in place.

submitrun <class> <project> [command-line args]

Example:   submitrun cs341 Proj1 checkers checkfile.dat

This runs the project, assuming there is an executable (i.e. submitmake was run successfully).


Grading and Academic Integrity

Your project will be tested using a variety of command lines, some of which will be invalid.
Your project will also be tested using a variety of command files which will test various conditions which your code should handle.

Project grading is described in the Project Policy handout.

Your answers to 341-Spring05-proj1_questions.txt are worth 10% of your project grade.

Cheating in any form will not be tolerated. Please re-read the Project Policy handout for further details on honesty in doing projects for this course.

Remember, the due date is firm. Submittals made after midnight of the due date will not be accepted. Do not submit any files after that time.