Using an abstract data
type
Now that
you have written your class for money values, you can incorporate it into the
previous inventory program. You will also learn how to use file streams.
Tasks
Take your
inventory and Money ADT source code files and make the following changes:
- Remove the main function from
the file containing the Money ADTand save the file as money.h
- Include this file in the inventory
source code with #include "money.h"
- Alter every use of a money
value (you probably used float) to Money, the name of your ADT class.
- Recompile the inventory source
code to check for errors. It should behave in exactly the same way as the
original, except that the output may look a little different because of
you overloading of the insertion operator for the Money class.
- Change the I/O behavior so
that the program reads the initial values for the inventory from a file,
and writes the new values out to the same file after the transactions have
been read from the keyboard. This will turn the program into more of a
database program that preserves values across program runs. Prompt the
user for the name of the database file and then open the file whose name
is typed in.
Notes
- If your ADT for Money has
problems then it won't work in the inventory program, so take the
assignment in the two stages outlined above.
- Include <fstream> to
use the classes ifstream and ofstream.
- To read from and write to the
same file use something like the following sequence:
string fname;
cout << "File? ";
cin >> fname;
ifstream in(fname.c_str());
if (!in) {
cout << "Error opening
" << fname << endl;
exit(1);
}
// read database from file ...
...
in.close();
// process transactions
...
ofstream out(fname.c_str());
if (!out) {
cout << "Error
opening " << fname << endl;
exit(1);
}
// save database in file ...
...
out.close();
- For the initial run, your database
should contain all zeros for the quantity on hand for each screw size.
Create this file by hand using the editor.
Testing
You should
test your program with a wide variety of inputs, but please use the following
data for the final testing and submission of results:
Run 1:
4 100
6 50
12 75
6 50
4 -20
Run 2:
8 100
10 100
4 70
12 -25
Deliverables
- The source code of your inventory
program, and results of running the two tests in the sequence given above,
in hardcopy form, handed to me (RTH).
- The source code of your program,
submitted using the assignment
submitter as 'program3'.
Due Date
Friday, April
14th before 5 pm.