Program 6 - Lyle School of Engineering

advertisement
CSE 1342 – Programming Assignment/Lab 6: Let’s Get Shipping 2
Due by Wednesday, April 29 @ 11:59 PM
This lab will build upon the classes you wrote in Lab 5. You will implement a program that can read package
information from an input file and print mailing labels for each package to an output file. Each mailing label will
include the recipient address and the total cost for shipping the package.
Tasks
1. You will open and parse an input file named input.txt. The first line of the input file will contain an
integer that indicates the number of packages that are listed in the file. Each package’s information will
take up 6 lines in the file. The first 5 lines will be the recipient’s name, street address, city, state, and zip
code; each address element will be on its own line. The 6th line will contain a variable amount of
numbers, depending on the package type. The first number on the line will represent the package type.
The values will be 1 for overnight, 2 for two-day, and 3 for ground. If it is a ground package, the 6th line
will also have the cost per ounce to ship the package and the weight of the package in ounces,
separated by spaces. If it is a two-day package, the line will also have the flat fee or if it is an overnight
package, it will have the additional fee per ounce.
2. Implement an additional constructor in all of your package classes that does not include arguments for
fields related to the sender. This program only deals with recipient addresses, so fields related to the
sender are not necessary.
3. Print each mailing label to an output file named output.txt. Each label includes the recipient’s
address and the total cost for shipping the package. You should output each mailing label in the format
shown in the example output on the next page.
4. [10 points extra credit] Before printing each mailing label to the output file, sort the vector of packages
by the total postage of each package. You may either write your own sorting algorithm or use the sort
function in the STL algorithm header. Be sure that your algorithm works for a variable amount of
packages and not just the three provided in the sample.
You will not know what types of packages will be created in your program at compile time. This means you must
use polymorphism and dynamic memory allocation to allow the program’s behavior to be determined at
execution time. In your code, create a vector that holds base-class Package pointers (Package*). After you
read in a single package’s information from the input file and determine the package type, you can dynamically
allocate an object of the correct package class and add its pointer to the vector. After reading in all of the
packages, you will open the output file and loop through the packages vector to print the mailing labels. When
calling the virtual calculateCost function on each Package pointer, the correct version of the function will
be called based on what type of object the pointer points to.
You may use any part of the C++ standard libraries to implement this program. Try to minimize the code in your
main function, aiming instead to decompose it into smaller functions. For example, you could have separate
functions for reading from and writing to the input and output files.
Submit your source code through Blackboard in a zipped file (e.g. yourname_Lab6.zip). You should have a .h
and .cpp file for each of the 3 package classes that you wrote for this lab, in addition to the main program file.
Your Lab TA will compile your program using the g++ compiler and run it in the School of Engineering (SOE) UNIX
environment. A program that does not compile correctly will receive a zero. Be sure to refer to the grading
rubric while you are developing your code.
Sample Input File:
3
John Smith
123 Any Street
Dallas
Texas
75275
2 0.79 13.20 6.50
Mary Thompson
1121 Dublin
Plano
Texas
75075
1 0.79 12.10 0.99
Sam Brown
2256 Southwestern
Dallas
Texas
75214
3 0.19 8.20
Sample Output File:
****************************************
John Smith
123 Any Street
Dallas, Texas 75275
Total Postage: $16.93
****************************************
****************************************
Mary Thompson
1121 Dublin
Plano, Texas 75075
Total Postage: $21.54
****************************************
****************************************
Sam Brown
2256 Southwestern
Dallas, Texas 75214
Total Postage: $1.56
****************************************
Sample Output File (w/ sorted output):
****************************************
Sam Brown
2256 Southwestern
Dallas, Texas 75214
Total Postage: $1.56
****************************************
****************************************
John Smith
123 Any Street
Dallas, Texas 75275
Total Postage: $16.93
****************************************
****************************************
Mary Thompson
1121 Dublin
Plano, Texas 75075
Total Postage: $21.54
****************************************
Download