In: Computer Science
In this assignment you are to make an inventory management system for a car dealership. The dealerships current method of tracking inventory is using a list which has the color and name of each car they have in inventory. There current inventory list will look like the example below.
RED TOYOTA
WHITE HONDA
.
.
.
BLACK BMW
The types of cars that they order are as follows:
TOYOTA, HONDA, BMW, NISSAN, TESLA, DODGE
The colors they purchase are as follows:
RED, WHITE, BLUE, BLACK, GREY, PINK
You are to ask the user for the name of the file that holds the current inventory list. Once given to you, parse the list and populate a 2D array to track the number of cars with a certain type and color. All colors and types of car may not be present in the inventory list. You should also tell the user (in the terminal, use cout to print the message) if you come across a car type or color that you are not tracking. If you come across a car or color that you are not tracking do not store it in your new inventory database only notify the user that you found it, your output for this prompt should match the below example.
Prompt to show when you find something that you are not tracking:
Unknown car or color in the input file: <UNKNOWN CAR COLOR> <UNKNOWN CAR TYPE>
Once you are finished you have parsed the input file prompt the user for the name of the output file they would like you to store the new database in. The output file should follow the format given below (yes you will need to use iomanip to get this format).
| RED | WHITE | BLUE | BLACK | GREY | PINK |
----------------------------------------------------
TOYOTA | 0 | 0 | 0 | 0 | 0 | 0 |
HONDA | 0 | 0 | 0 | 0 | 0 | 0 |
BMW | 0 | 0 | 0 | 0 | 0 | 0 |
NISSAN | 0 | 0 | 0 | 0 | 0 | 0 |
TESLA | 0 | 0 | 0 | 0 | 0 | 0 |
DODGE | 0 | 0 | 0 | 0 | 0 | 0 |
Below code written in C++ ....
#include<iostream>
#inclide<string>
#include<fstream>
using namespace std;
int car(string s) //function to map car name with array index
{
if(s.compare("TOYOTA")==0)
return 0;
else if(s.compare("HONDA")==0)
return 1;
else if(s.compare("BMW")==0)
return 2;
else if(s.compare("NISSAN")==0)
return 3;
else if(s.compare("TESLA")==0)
return 4;
else if(s.compare("DODGE")==0)
return 5;
else
return -1;
}
int color(string str) //function to map car color with array index
{
if(str.compare("RED")==0)
return 0;
else if(str.compare("WHITE")==0)
return 1;
else if(str.compare("BLUE")==0)
return 2;
else if(str.compare("BLACK")==0)
return 3;
else if(str.compare("GREY")==0)
return 4;
else if(str.compare("PINK")==0)
return 5;
else
return -1;
}
int main()
{
string inFile,outFile;
ifstream in;
ofstream out;
string str;
string CR[6]={"TOYOTA","HONDA","BMW","NISSAN","TESLA","DODGE"}; //initialization for car and color aray
string CL[6]={"RED","WHITE","BLUE","BLACK","GREY","PINK"};
int A[6][6]; //2D array to store inventory information
for(int i=0;i<6;i++) //initially 2D array is initialized with 0
for(int j=0;j<6;j++)
A[i][j]=0;
cout<<"Enter name of input file: ";
cin>>inFile;
in.open(inFile); //open input file
if(!in){
cout<<"Unable to open file "<<inFile<<endl;
return 0;
}
bool flag=false;
int row,col;
//read data from input file
while(in>>str)
{
if(flag==false)
{
row=car(str);
flag=true;
if(row==-1)
cout<<"Unknown car or color in the input file: "<<str<<endl;
}
else if(flag==true)
{
col=color(str);
flag=false;
if(row==-1 || col==-1)
cout<<"Unknown car or color in the input file: "<<str<<endl;
else
A[row][col]++;
}
}
cout<<"Enter the name of output file: ";
cin>>outFile;
out.open(outFile); //open output file
if(!out){
cout<<"Unable to open file "<<outFile<<endl;
return 0;
}
out<<"\t| ";
for(int i=0;i<6;i++) //write data to output file
out<<CL[i]<<" |";
out<<endl;
out<<"-------------------------------------------------------------"<<endl;
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
if(j==0)
out<<CR[i]<<"\t|";
out<<"\t"<<A[i][j]<<"|";
}
out<<endl;
}
in.close();
out.close();
return 0;
}
Input file : input.txt
TOYOTA RED
BMW BLUE
TOYOTA RED
BMW PINK
TOYOTA RED
NISSAN BLUE
TOYOTA RED
TESLA PINK
TOYOTA RED
BMW BLUE
TOYOTA WHITE
BMW BLACK
DODGE RED
NISSAN GREY
TOYOTA RED
TESLA PINK
MARUTI MAGENTA
Output file : output.txt
RED | WHITE | BLUE | BLACK | GREY | PINK | |
---|---|---|---|---|---|---|
TOYOTA | 6 | 1 | 0 | 0 | 0 | 0 |
HONDA | 0 | 0 | 0 | 0 | 0 | 0 |
BMW | 0 | 0 | 2 | 1 | 0 | 1 |
NISSAN | 0 | 0 | 1 | 0 | 1 | 0 |
TESLA | 0 | 0 | 0 | 0 | 0 | 2 |
DEDGE | 1 | 0 | 0 | 0 | 0 | 0 |
Sample output looks like :
Enter name of input file : input.txt
Unknown car or color in the input file : MARUTI
Unknown car or color in the input file : MAGENTA
Enter name of output file : output.txt