In: Computer Science
Larry is a loan officer, He scanned a pile of documents from his
desk for different mortgage applications. Some of the loan
applications are missing required documents. Write a program to
help him organize the documents, and determine which ones are
missing.
Every document contains metadata, including:
- document name("fileName"),
- document owner("owner"),
- document type ("docType"),
- loan application ID ("applicationId"), and
- content length ("contentLength")
In C++ your task is to find all the document types that are missing
for each loan application. Every loan application must include
every document type. Since you are new to Larry's work, you don't
know how many document types there are in total. You should figure
that from the pile yourself.
Input (stdin):
Take input from a file("myFile.txt"). A sample file input is given
below as an example.
A comma delimited file with the first line as field names.
Input Sample file Example:
fileName,owner,docType,applicationId,contentLength
bank_statement_1,Sunny,bank_statement,1,1000
tax_document_1,Sunny,tax_return,1,16001
tax_document_2,Jay,tax_return,2,2000
document_123,Ram,tax_return,3,1500
medical_report,Ram,medical_history,3,15000
prescription,Jason,medical_history,4,200
property_assets,Jason,bank_statement,4,4000
OutPut (stdout):
- Print out two lines for each document type. The first line is
document type. The second line are the application IDs that are
missing for this type of document.
- Delimit the application IDs by one space
- Sort document types in alphabetical order. Sort application IDs
in numerical order.
- Print only document types when they are missing for atleast one
loan application.
Sample Output :
bank_statement
2 3
medical_history
1 2
tax_return
4
Explanation:
After scanning the given file we come to know that each application
should contain bank_statement, medical_history and
tax_return.
Jay and Ram are missing bank_statement in their application
Sunny and Jay are missing medical_history in their
application
Jason is missing tax_return in his application
since Id of sunny is 1, Jay is 2, Ram is 3 and Jason is 4.
if we write all documents in alphabetical order and people who are
missing them. Then the output would look like the sample output
above.
Note: The content of "myFile.txt" will be changed while testing
your program. So make sure your program works for more documents
too.
Please turn in a working .cpp file that takes input from
"myFile.txt" and displays output to the console in the format
mentioned above.
Code:
//Include libraries
#include<iostream>
#include<fstream>
#include <stdlib.h>
#include<string>
//Use std namespace
using namespace std;
//Define a class Report
class Report
{
//Declare variable
string fileName;
//Declare variable
string owner;
//Declare variable
string docType;
//Declare variable
int applicationId;
//Declare variable
int contentLength;
//Declare variable
int numberOfRecords;
//Define access specifier
public:
//Define a method "readFile()"
void readFile(Report []);
//Define a method "display()"
void display(Report []);
//Define a method "InvalidReport()"
void InvalidReport(Report []);
//Define a method "SortRecordNumber()"
void SortRecordNumber(Report rep[]);
};
//Define a method "readFile()"
void Report::readFile(Report rec[])
{
//Define file
ifstream readf;
//Open file
readf.open ("myFile.txt");
//Declare variable
numberOfRecords = 0;
//Store data
string hed;
//Declare array
char heading[100];
//Read heading
readf>>hed;
//Loops till end
while(!readf.eof())
{
//Loop until end
for(int x = 0; x < 5; x++)
{
//Switch case
switch(x)
{
//Define case 0
case 0:
//Extract data
readf.getline(heading, 256, ',');
//Store data
rec[numberOfRecords].fileName = heading;
//Break
break;
//Define case 1
case 1:
//Extract data
readf.getline(heading, 256, ',');
//Store data
rec[numberOfRecords].owner = heading;
//Break
break;
//Define case 2
case 2:
//Extract data
readf.getline(heading, 256, ',');
//Store data
rec[numberOfRecords].docType = heading;
//Break
break;
//Define case 3
case 3:
//Extract data
readf.getline(heading, 256, ',');
//Converts data to integer
rec[numberOfRecords].applicationId = atoi(heading);
//Break
break;
//Define case 4
case 4:
//Extract data
readf>>rec[numberOfRecords].contentLength;
//break
break;
}
}
//Increment count
numberOfRecords++;
}
//Close file
readf.close();
}
//Define a function "display()"
void Report::display(Report rec[])
{
//Display message
cout<<"\n Records in the file: ";
//Loop until length
for(int x = 0; x < numberOfRecords; x++)
//Display message
cout<<rec[x].fileName<<" "<<rec[x].owner<<" "<<rec[x].docType<<" "<<rec[x].applicationId<<" "<<rec[x].contentLength;
}
//Define a function "SortRecordNumber()"
void Report::SortRecordNumber(Report rec[])
{
//Loops till end
for(int x = 0; x < numberOfRecords; x++)
{
//Loops till end
for(int y = 0; y < numberOfRecords - x - 1; y++)
{
//If id is greater
if(rec[y].applicationId > rec[y+1].applicationId)
{
//Assign record
Report temp = rec[y];
//Assign next
rec[y] = rec[y+1];
//Swap
rec[y+1] = temp;
}
}
}
}
//Define a method "InvalidReport()"
void Report::InvalidReport(Report rec[])
{
//Declare variables
int flag[100][3] = {0,0,0,0};
//Declare arrays
int bank[100], medical[100], tax[100];
//Declare variables
int cb = 0, cm = 0, ct = 0;
//Declare variable
int c = 0;
//Loops till end
for(int x = 0; x < numberOfRecords; x++)
{
//If x is 0
if(x == 0)
{
//If record is bank_statement
if(rec[x].docType == "bank_statement")
{
//Set id
flag[c][0] = rec[x].applicationId;
}
//check document type
if(rec[x].docType == "medical_history")
{
//Set record number
flag[c][1] = rec[x].applicationId;
}
//Check type
if(rec[x].docType == "tax_return")
{
//Assign id
flag[c][2] = rec[x].applicationId;
}
}
//Otherwise
else
{
//Checks id
if(rec[x].applicationId == rec[x-1].applicationId)
{
//Checks document type
if(rec[x].docType == "bank_statement")
{
//Assign id
flag[c][0] = rec[x].applicationId;
}
//Checks document type
if(rec[x].docType == "medical_history")
{
//Assign id
flag[c][1] = rec[x].applicationId;
}
//Check document type
if(rec[x].docType == "tax_return")
{
//Assign id
flag[c][2] = rec[x].applicationId;
}
}
//Otherwise
else
{
//Increment
c++;
//Checks type
if(rec[x].docType == "bank_statement")
{
//Assign id
flag[c][0] = rec[x].applicationId;
}
//Check type
if(rec[x].docType == "medical_history")
{
//Assign value
flag[c][1] = rec[x].applicationId;
}
//Check type
if(rec[x].docType == "tax_return")
{
//Assign id
flag[c][2] = rec[x].applicationId;
}
}
}
}
//Loop until end
for(int x = 0; x <= c; x++)
{
//If value is 0
if(flag[x][0] == 0)
//Assign value
bank[cb++] = (x+1);
//If value is 0
if(flag[x][1] == 0)
//Assign value
medical[cm++] = (x+1);
//If value is 0
if(flag[x][2] == 0)
//Assign value
tax[ct++] = (x+1);
}
//Display message
cout<<"\n\n bank_statement\n";
//Loop until length
for(int x = 0; x < cb; x++)
{
//Display value
cout<<bank[x]<<", ";
}
//Display message
cout<<"\n medical_history \n";
//Loop until count
for(int x = 0; x < cm; x++)
{
//Display message
cout<<medical[x]<<", ";
}
//Display message
cout<<"\n tax_return\n";
//Loops until count
for(int x = 0; x < ct; x++)
{
//Display message
cout<<tax[x]<<",";
}
}
//Define main method
int main()
{
//Create array
Report rec[100];
//Read file contents
rec[0].readFile(rec);
//Displays array
rec[0].display(rec);
//Sort array
rec[0].SortRecordNumber(rec);
//Generate document
rec[0].InvalidReport(rec);
//Pause console window
system("pause");
//Return 0
return 0;
}