Vector
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
class ItemRecord
{
char name[10];
int quantity;
int code;
float cost;
static int count;
public:
ItemRecord(); // Default Constructer
~ItemRecord() {} // Destructer
void insertDetails(vector<ItemRecord> &);
void displayDetails(vector<ItemRecord>);
void searchDetails(vector<ItemRecord>, int);
void sortDetails(vector<ItemRecord> &);
void deleteDetails(vector<ItemRecord> &, int);
friend void print(ItemRecord);
bool operator>(const ItemRecord &);
};
// Function To Handle Incorrect User Input
template <typename T> T checkInput(T Input, string content)
{
while (!cin.good())
{
cout << "Wrong Input, Please Enter it Correctly\n";
cin.clear(); // Clears The Previous Input
cin.ignore(100, '\n'); // Ignores The Incorrect Input Upto 100 Characters
cout << content;
cin >> Input;
}
while (Input < 0)
{
cout << "This Input Cannot Be Negative\n";
cin.clear();
cin.ignore(100, '\n');
cout << content;
cin >> Input;
}
return Input;
}
int ItemRecord::count = 0;
// Default Constructer Definition
ItemRecord::ItemRecord()
{
name[10] = ' ';
quantity = 0;
code = 0;
cost = 0.0;
}
bool ItemRecord::operator>(const ItemRecord &I1)
{
if (code > I1.code)
return 1;
else
return 0;
}
void ItemRecord::insertDetails(vector<ItemRecord> &vect)
{
cout << "\nEnter The Name of Item " << ++this->count << " : ";
cin >> name;
char content1[20];
sprintf(content1, "\nEnter The Code of %s : ", name); // f string
cout << content1;
cin >> code;
code = checkInput(code, content1);
char content2[20];
sprintf(content2, "\nEnter The Quantity of %s : ", name); // f string
cout << content2;
cin >> quantity;
quantity = checkInput(quantity, content2);
char content3[20];
sprintf(content3, "\nEnter The Cost of %s : ", name); // f string
cout << content3;
cin >> cost;
cost = checkInput(cost, content2);
vect.push_back(*this);
}
void print(ItemRecord);
void ItemRecord::displayDetails(vector<ItemRecord> vect)
{
cout << "\n****** Item Lists ******\n";
for_each(vect.begin(), vect.end(), print);
}
void print(ItemRecord Ir)
{
cout << endl << "Code : " << Ir.code << endl;
cout << "Name : " << Ir.name << endl;
cout << "Quantity : " << Ir.quantity << endl;
cout << "Cost : " << Ir.cost << endl;
}
void ItemRecord::searchDetails(vector<ItemRecord> vect, int userCode)
{
for (int i = 0; i <= vect.size(); i++)
{
if (userCode == vect[i].code)
{
cout << "\nItem Found\n\n";
cout << "Code : " << vect[i].code << endl;
cout << "Name : " << vect[i].name << endl;
cout << "Quantity : " << vect[i].quantity << endl;
cout << "Cost : " << vect[i].cost << endl;
return;
}
}
cout << "\nItem Not Found\n";
}
void ItemRecord::sortDetails(vector<ItemRecord> &vect)
{
int i, j, flag = 0;
ItemRecord temp;
for (i = 0; i < vect.size(); i++)
{
for (j = 0; j < vect.size() - i - 1; j++)
{
if (vect[j] > vect[j + 1])
{
flag = 1;
temp = vect[j];
vect[j] = vect[j + 1];
vect[j + 1] = temp;
}
}
if (flag == 0)
{
break;
}
}
}
void ItemRecord::deleteDetails(vector<ItemRecord> &vect, int userCode)
{
vector<ItemRecord>::iterator iter;
for (iter = vect.begin(); iter != vect.end(); iter++)
{
if (userCode == iter->code)
{
vect.erase(iter);
cout << "\nItem Deleted\n";
return;
}
}
cout << "\nItem Not Found\n";
}
int main()
{
vector<ItemRecord> Items;
int num;
int userInput;
int userCode1;
int userCode2;
char userInput1;
string content1("\nEnter The Number of Items You Want To Insert\n");
string content2("\nPress 'c' To Continue And 'e' To Exit\n");
string content3("\n1. Press 1 To Insert\n2. Press 2 To Display\n3. Press 3 To Search\n4. Press 4 To Sort\n5. Press 5 To Delete\n6. Press 6 To Exit\n");
string content4("\nEnter The Code of Item to Search\n");
ItemRecord *it = NULL; // NULL Pointer
ItemRecord IR;
while (true)
{
cout << content3;
cin >> userInput;
userInput = checkInput(userInput, content3);
switch (userInput)
{
case 1:
cout << content1;
cin >> num;
it = new ItemRecord[num]; // Array of Objects
for (int i = 0; i < num; i++)
{
it[i].insertDetails(Items);
}
if (num > 0)
cout << "\nAll Products Successfully Inserted\n";
break;
case 2:
if (Items.empty())
{
cout << "No Items in The Record\n";
break;
}
IR.displayDetails(Items);
break;
case 3:
if (Items.empty())
{
cout << "No Items in The Record\n";
break;
}
cout << content4;
cin >> userCode1;
userCode1 = checkInput(userCode1, content4);
IR.searchDetails(Items, userCode1);
break;
case 4:
if (Items.empty())
{
cout << "No Items in The Record\n";
break;
}
IR.sortDetails(Items);
cout << "\nList Sorted\n";
break;
case 5:
if (Items.empty())
{
cout << "No Items in The Record\n";
break;
}
cout << "\nEnter The Code of Item To Delete\n";
cin >> userCode2;
IR.deleteDetails(Items, userCode2);
break;
case 6:
exit(0);
break;
default:
cout << "Please Enter Correct Input\n";
break;
}
label1:
cout << content2;
cin >> userInput1;
if (tolower(userInput1) == 'c')
continue;
else if (tolower(userInput1) == 'e')
exit(0);
else
{
cout << "Please Enter Correct Input\n";
goto label1;
}
}
delete[] it;
return 0;
}
// Output
// 1. Press 1 To Insert
// 2. Press 2 To Display
// 3. Press 3 To Search
// 4. Press 4 To Sort
// 5. Press 5 To Delete
// 6. Press 6 To Exit
// 1
// Enter The Number of Items You Want To Insert
// 4
// Enter The Name of Item 1 : Pen
// Enter The Code of Pen : 2
// Enter The Quantity of Pen : 20
// Enter The Cost of Pen : 5
// Enter The Name of Item 2 : Pencil
// Enter The Code of Pencil : 4
// Enter The Quantity of Pencil : 40
// Enter The Cost of Pencil : 6
// Enter The Name of Item 3 : Eraser
// Enter The Code of Eraser : 3
// Enter The Quantity of Eraser : 25
// Enter The Cost of Eraser : 4
// Enter The Name of Item 4 : Scale
// Enter The Code of Scale : 1
// Enter The Quantity of Scale : 45
// Enter The Cost of Scale : 10
// All Products Successfully Inserted
// Press 'c' To Continue And 'e' To Exit
// c
// 1. Press 1 To Insert
// 2. Press 2 To Display
// 3. Press 3 To Search
// 4. Press 4 To Sort
// 5. Press 5 To Delete
// 6. Press 6 To Exit
// 2
// ****** Item Lists ******
// Code : 2
// Name : Pen
// Quantity : 20
// Cost : 5
// Code : 4
// Name : Pencil
// Quantity : 40
// Cost : 6
// Code : 3
// Name : Eraser
// Quantity : 25
// Cost : 4
// Code : 1
// Name : Scale
// Quantity : 45
// Cost : 10
// Press 'c' To Continue And 'e' To Exit
// c
// 1. Press 1 To Insert
// 2. Press 2 To Display
// 3. Press 3 To Search
// 4. Press 4 To Sort
// 5. Press 5 To Delete
// 6. Press 6 To Exit
// 3
// Enter The Code of Item to Search
// 4
// Item Found
// Code : 4
// Name : Pencil
// Quantity : 40
// Cost : 6
// Press 'c' To Continue And 'e' To Exit
// c
// 1. Press 1 To Insert
// 2. Press 2 To Display
// 3. Press 3 To Search
// 4. Press 4 To Sort
// 5. Press 5 To Delete
// 6. Press 6 To Exit
// 3
// Enter The Code of Item to Search
// 5
// Item Not Found
// Press 'c' To Continue And 'e' To Exit
// c
// 1. Press 1 To Insert
// 2. Press 2 To Display
// 3. Press 3 To Search
// 4. Press 4 To Sort
// 5. Press 5 To Delete
// 6. Press 6 To Exit
// 4
// List Sorted
// Press 'c' To Continue And 'e' To Exit
// c
// 1. Press 1 To Insert
// 2. Press 2 To Display
// 3. Press 3 To Search
// 4. Press 4 To Sort
// 5. Press 5 To Delete
// 6. Press 6 To Exit
// 2
// ****** Item Lists ******
// Code : 1
// Name : Scale
// Quantity : 45
// Cost : 10
// Code : 2
// Name : Pen
// Quantity : 20
// Cost : 5
// Code : 3
// Name : Eraser
// Quantity : 25
// Cost : 4
// Code : 4
// Name : Pencil
// Quantity : 40
// Cost : 6
// Press 'c' To Continue And 'e' To Exit
// c
// 1. Press 1 To Insert
// 2. Press 2 To Display
// 3. Press 3 To Search
// 4. Press 4 To Sort
// 5. Press 5 To Delete
// 6. Press 6 To Exit
// 5
// Enter The Code of Item To Delete
// 5
// Item Not Found
// Press 'c' To Continue And 'e' To Exit
// c
// 1. Press 1 To Insert
// 2. Press 2 To Display
// 3. Press 3 To Search
// 4. Press 4 To Sort
// 5. Press 5 To Delete
// 6. Press 6 To Exit
// 5
// Enter The Code of Item To Delete
// 3
// Item Deleted
// Press 'c' To Continue And 'e' To Exit
// c
// 1. Press 1 To Insert
// 2. Press 2 To Display
// 3. Press 3 To Search
// 4. Press 4 To Sort
// 5. Press 5 To Delete
// 6. Press 6 To Exit
// 2
// ****** Item Lists ******
// Code : 1
// Name : Scale
// Quantity : 45
// Cost : 10
// Code : 2
// Name : Pen
// Quantity : 20
// Cost : 5
// Code : 4
// Name : Pencil
// Quantity : 40
// Cost : 6
// Press 'c' To Continue And 'e' To Exit
// c
// 1. Press 1 To Insert
// 2. Press 2 To Display
// 3. Press 3 To Search
// 4. Press 4 To Sort
// 5. Press 5 To Delete
// 6. Press 6 To Exit
// 5
// Enter The Code of Item To Delete
// 1
// Item Deleted
// Press 'c' To Continue And 'e' To Exit
// c
// 1. Press 1 To Insert
// 2. Press 2 To Display
// 3. Press 3 To Search
// 4. Press 4 To Sort
// 5. Press 5 To Delete
// 6. Press 6 To Exit
// 6
Comments
Post a Comment