(In hurry) Import file (day.txt) Note: customer1,item1;customer2,item2;customer3
ID: 3800368 • Letter: #
Question
(In hurry) Import file (day.txt) Note: customer1,item1;customer2,item2;customer3,item3;
1. Create a class called "Purchase", this class will have two member variables:
a. Customer
b. Item
2. Create an array of type purchase. There is a combined total of 15 purchases.
3. Parse all purchase data from all three files and store into your purchase array.
4. Sort the array in ascending order(do not use STL)
The array should be sorted by the following criterea:
1. Customer Number - Customer 100 should come before customer 200, and both
of those customers should come before customer 300.
2. Item Number - If two or more purchases are made by the same customer,
then you must sort according the item number.
For example, assume you have the following:
100,1A;200,1B;100,1C;100,1B;
When parsed and sorted, your array should look like the following:
arr[0]: Customer: 100, Item: 1A
arr[1]: Customer: 100, Item: 1B
arr[2]: Customer: 100, Item: 1C
arr[3]: Customer: 200, Item: 1B
5. Print out the array onto the console
==========
day.txt
384362,9DKEE;384262,JE837;12345,4J83KE;124387,38DH3;12345,8A3BCK;12345,4YW6S3;
Explanation / Answer
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
//Class Purchase definition
class Purchase
{
public:
//Data member
int Customer;
string item;
};//End of class
//Read data from three files and store it in class object
void Input(Purchase cus_DB[], int &count)
{
//Open file in read mode
ifstream ifile1("Day1.txt");
ifstream ifile2("Day2.txt");
ifstream ifile3("Day3.txt");
char ch;
//First File
//If file does not exist show error message and exit
if (ifile1.fail())
{
cout << "Error!";
exit(0);
}//end of if
//Loops till end of file
while (!ifile1.eof())
{
//Read data from file and stores it in respective data members
ifile1>>cus_DB[count].Customer>>ch>>cus_DB[count].item>>ch;
//Increase the counter
count++;
}//end of while
//SecondFile
//If file does not exist show error message and exit
if (ifile2.fail())
{
cout << "Error!";
exit(0);
}//end of if
//Loops till end of file
while (!ifile2.eof())
{
//Read data from file and stores it in respective data members
ifile2>>cus_DB[count].Customer>>ch>>cus_DB[count].item>>ch;
//Increase the counter
count++;
}//end of while
//Third File
//If file does not exist show error message and exit
if (ifile3.fail())
{
cout << "Error!";
exit(0);
}//end of if
//Loops till end of file
while (!ifile3.eof())
{
//Read data from file and stores it in respective data members
ifile3>>cus_DB[count].Customer>>ch>>cus_DB[count].item>>ch;
//Increase the counter
count++;
}//end of while
}//End of function
//Displays the Purchase Items
void Display(Purchase cus_DB[], int &count)
{
//Loops till end
for(int x = 0; x < count; x++)
cout<<cus_DB[x].Customer<<" "<<cus_DB[x].item<<endl;
}//End of function
//Menu function
int menu()
{
//To store user choice
int ch;
cout<<" Sorting Options ";
cout<<" 1) Customer Number 2) Item Number";
//Accepts user choice
cout<<" Enter your choice: ";
cin>>ch;
//Returns user choice
return ch;
}//End of function
//Function to sort in Ascending order of Customer
void CustomerAscending(Purchase cus_DB[], int count)
{
//Loop variable
int x, y;
//Temporary object for swapping
Purchase Temp;
for(x = 0; x < count; x++)
{
for(y = 0; y < count - x - 1; y++)
{
//Swapping
if(cus_DB[y].Customer> cus_DB[y + 1].Customer)
{
Temp.Customer = cus_DB[y].Customer;
cus_DB[y].Customer = cus_DB[y + 1].Customer;
cus_DB[y + 1].Customer = Temp.Customer;
}//End of if
}//End of inner for loop
}//End of outer for loop
}//End of function
//Function to sort in Ascending order of Item
void ItemAscending(Purchase cus_DB[], int count)
{
//Loop variable
int x, y;
//Temporary object for swapping
Purchase Temp;
for(x = 0; x < count; x++)
{
for(y = 0; y < count - x - 1; y++)
{
//Swapping
if(cus_DB[y].item > cus_DB[y + 1].item)
{
Temp.item = cus_DB[y].item;
cus_DB[y].item = cus_DB[y + 1].item;
cus_DB[y + 1].item = Temp.item;
}//End of if
}//End of inner for loop
}//End of outer for loop
}//End of function
//Main function
int main()
{
//Crates an array of object
Purchase cus_DB[15];
//Counter for Customers
int count = 0;
//Reads data from file and stores it in objects
Input(cus_DB, count);
//User choice
switch(menu())
{
case 1:
CustomerAscending(cus_DB, count);
Display(cus_DB, count);
break;
case 2:
ItemAscending(cus_DB, count);
Display(cus_DB, count);
break;
default:
cout<<" Invalid Choice";
}//End of switch
}//End of main
Sample Run1:
Sorting Options
1) Customer Number
2) Item Number
Enter your choice: 1
2345 9DKEE;
2345 JE837;
2345 4J83KE;
2345 38DH3;
2345 8A3BCK;
2345 4YW6S3;
24387 9DKEE;
24387 JE837;
24387 4J83KE;
84262 38DH3;
84262 9DKEE;
84262 JE837;
384362 4J83KE;
384362 38DH3;
384362 8A3BCK;
Sample Run2:
Sorting Options
1) Customer Number
2) Item Number
Enter your choice: 2
384362 38DH3;
84262 38DH3;
2345 38DH3;
24387 4J83KE;
2345 4J83KE;
2345 4J83KE;
384362 4YW6S3;
84262 8A3BCK;
2345 8A3BCK;
24387 9DKEE;
384362 9DKEE;
84262 9DKEE;
2345 JE837;
24387 JE837;
2345 JE837;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.