Assignment 1 – CMPT 117 2004 (W) Used Vehicle

advertisement
Assignment 1 – CMPT 117 2004 (W)
Used Vehicle Management System
Due: 4:00 PM, Feb. 13, 2004
Objective: Classes, arrays, strings and I/O.
Description
A used vehicle dealer has many used vehicles for sale. A used vehicle
management system has to be built to manage these vehicles. You can
download the data file and run this sample solution to get a feeling of what you
need to accomplish for this assignment. The functions and design of this
assignment are described below.
Properties of a used vehicle
A used vehicle has the following properties:
Type: a string value which can be Car, Minivan, Truck, etc.
Make: a string value which can be Honda, Ford, Acura, etc.
Model: a string value which can be Civic, Cavalier, etc.
Year: an integer value which can be 1997, 1998, etc.
Price: a double value which can be 14362, 16299 etc.
Main menu
The following menu contains all the commands that need to be implemented in
this system:
1. Load vehicle information from file
2. List all vehicles
3. Search by type
4. Search by price range
5. Set a price
6. Exit
Please select a command:
Load file
An input file containing many used vehicles is provided for this assignment.
When command 1 is selected, the file name of the input file is prompted. After
the file name is inputed, information of all vehicles stored in the file are loaded
into the system, and the total number of vehicles loaded is displayed. Here is an
example (the read words are typed by the user, while the blue words are printed
by the system):
Please select a command: 1
Please input the file name: cars.txt
4 cars loaded
The first line of the file has an integer which indicates the total number of vehicles
in the file. The information of each vehicle is on one line, and the format is:
Type make model year price
Here is a short example of the input file:
4
Car
SUV
Truck
Truck
Acura
Nissan
GMC
Dodge
3.5RL
Xterra
SLT
DakotaSLT
1997
2001
1998
2000
30550
29221
28308
25880
All the following sample output assumes this file is loaded into the system.
Command: List all vehicles
When this command is selected, all the vehicles are numbered and listed on the
screen, e.g.,
Please select a command: 2
0. Car
Acura
1. SUV
Nissan
2. Truck
GMC
3. Truck
Dodge
3.5RL
Xterra
SLT
DakotaSLT
1997
2001
1998
2000
$30550
$29221
$28308
$25880
Command: Search by type
The user can search the vehicles by type. When this command is selected, the
available types are searched and printed. The user then selects the type he/she
wants to display. Finally the vehicles with this type are displayed (the order of
vehicles does not matter), e.g.,
Please select a command: 3
0. Car
1. SUV
2. Truck
Please input the type of the vehicle you want to search: 2
2. Truck
GMC
SLT
1998 $28308
3. Truck
Dodge
DakotaSLT
2000 $25880
2 vehicle(s) found.
Command: Search by price range
The user can also search by price range. When this command is selected, the
system asks the user to input the lower limit and upper limit of the price he/she
wants, and all vehicles within this price range are displayed (the order of vehicles
does not matter), e.g.,
Please select a command: 4
Please input the lower limit of the price: 28000
Please input the higher limit of the price: 30000
1. SUV
Nissan
Xterra
2. Truck
GMC
SLT
2 vehicle(s) found.
2001 $29221
1998 $28308
Command: Set a price
From time to time the dealer would like to change the price of used vehicles.
Thus user can also set the price of a vehicle. When this command is selected,
the system asks the user to input a vehicle number. Note that this number is the
index value shown at the start of a line when you do "List all vehicles" or "Search
by type" or "Search by price range". For example it will be "3" if you want to set a
different price for the following vehicle:
3. Truck
Dodge
DakotaSLT
2000 $25880
Please select a command: 5
Please input the vehicle number: 3
Please input the new price for this vehicle: 23830
New price is:
Truck
Dodge
DakotaSLT
2000 $23830
Command: Exit
This command will exit from the program by asking the user whether they would
like to save any changes made to the input data. If so, then it asks for a file name
and writes all the vehicle information into that file. After that, it also displays all
the vehicle information that it wrote into this new file. (Note that the last line in
this display is different from what we started off with)
Please select a command: 6
Would you like to save any changes (y/n)?: y
Enter a destination file name: new_cars.txt
0.
1.
2.
3.
Car
SUV
Truck
Truck
Acura
Nissan
GMC
Dodge
3.5RL
Xterra
SLT
DakotaSLT
1997
2001
1998
2000
$30550
$29221
$28308
$23830
Class and data structure design
Class: UsedVehicle
The UsedVehicle class should be defined to store all the information of a
vehicle. The following member variables should be defined in the UsedVehicle
class:
string Type
string Make
string Model
int year
double price
At least the following member functions should be defined in the UsedVehicle
class:
UsedVehicle() this is the constructor, which doesn't have any parameters.
Initialise everything to NULL or 0.
void initialise(const string& newType, const string& newMake, const
string& newModel, int newYear, double newPrice) this function initialises
the member variables of UsedVehicle class by the values of the arguments.
void print() this function prints all the information of a usedvehicle on the
screen.
void read(ifstream & f) this function reads from an input file stream f and
initialises all the member variables.
void write(ofstream &f) this function writes all the information of a
usedvehicle to an output file stream f.
void set_price(double newPrice) this function sets the price for the
usedvehicle to newPrice.
Class: VehicleContainer
The VehicleContainer class uses an array to store all the vehicles that are to be
managed in the system. A const integer MaxVehicles is defined as below and the
VehicleContainer class can store used vehicles up to MaxVehicles:
int const MaxVehicles = 100;
At least the following member variables should be defined in this class:
UsedVehicle used_vehicles[MaxVehicles] -- the array to store all the vehicle
information.
int num_vehicles -- the actual number of vehicles in the array.
At least the following member functions should be defined in this class:
void add(const UsedVehicle& vehicle) adds a new vehicle into the array.
void list_all() lists all used vehicles stored in the object.
void search_by_type(const string& type) searches all vehicles by the type
given in the argument, and print all vehicles that match the given type.
void search_by_price(int price_low, int price_high) searches all vehicles by
the price range given in the arguments, and print all vehicles where
price>=price_low and price<=price_high.
int get_list_of_types(string list[]) searches all possible types of the vehicles,
stores them in the array list[], and returns the total number of different types.
No duplicated types should be added into this list. Once this list is printed on
screen, the user can simply choose the type of vehicle and list all vehicles of
that type.
Note: if this functionality is not clear look at the output screen for the "Search
by type" command.
void set_new_price(int i, double new_price) sets the price to new_price for
vehicle number "num". (basically does
used_vehicles[num].set_price(new_price);)
void write(int i, ofstream& f) writes all information about vehicle "num" into
output file stream "f".
Class: MainMenu
The MainMenu class is used to display menu, read user commands, and process
the commands.
At least the following member variables should be defined in this class:
VehicleContainer all_vehicles -- this variable stores all used vehicle
information.
At least the following member functions should be defined in this class:
void run() is the main control function of the system. It displays the menu,
reads the input from the user, and processes the commands selected by the
user.
void display_menu() displays the menu of the system.
void cmd_load_file() processes the "load file command". It gets the file name
from the user, and opens the file. The vehicle information of the file is read,
and then added into all_vehicles by the add() member function of the class
VehicleContainer class. Finally, it prints the total number of vehicles read from
the file.
void cmd_search_by_type() processes the "search by file command". It first
gets all types of the vehicle by calling all_vehicles.get_type_list(...) and prints
all types on the screen. After the user selects the type he/she wants, it calls
all_vehicles.search_by_type(...) to display all vehicles of this type.
void cmd_search_by_price() processes the "search by price" command. It
first prompts the user inputs the lower limit and upper limit of the expected
prices, and then calls all_vehicles.search_by_price(...) to display all vehicles
within the required price range.
void cmd_set_price_of() processes the "Set a price" command. It gets the
vehicle number from the user and calls all_vehicles.set_new_price(...) to set
the new price for that vehicle.
void cmd_exit() asks whether the user would like to save any changes. If
"yes" then it inputs a file name and writes all the usedvehicles information to
that file by calling all_vehicles.write(...) repeatedly.
Programming style
The programming style contributes to 25% of the marks for the assignment,
which includes:
At-least a single sentence of comment for every function (at the start) that
explains what this function does.
Reasonable spaces, empty lines, and indentation should be used to make the
program more readable.
Variable names should not be too short and the names should have good
meaning that can help to understand the program.
The type of the member variables and member functions (e.g., public, private,
protected) should be reasonable.
Each member function should not be too long, since long functions are hard to
design, write, debug, and maintain. (create sub functions if needed).
Reasonable amount of comments on important parts of the program. This can
make the program much easier to read and maintain.
Submission
For the assignment, you should submit the following files into the assignment1
folder of E-Handin before the due date (Feb 13, Friday 4pm)
Your complete Visual C++ project files (including *.vcproj, *.cpp, *.h)
A file named Readme.doc (a MS Word file) or Readme.txt including
Your name, NSID and student number should be present at the top of this
readme file.
How to run your program
The status of your program, i.e., what functions it can perform and what
functions are not complete
Note
The marker will not look into any compile or run time errors and try to fix it. It is
your responsibility to submit error free code. Any functions that you cannot
implement should be commented out inside your code and reported so, in the
Readme.doc file. You might receive partial marks for that.
Be aware of the consequences of cheating. You have signed an Academic
honesty policy and I will hold you to it. The marker has been instructed to
actively look for any possible cheating.
Download