In: Computer Science
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project.
After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and at least 7 STL algorithms with an option to exit. Your application must contain at least 8 menu options. You will decide how to implement these menu selections.
Her is the sample code how this project suppose to be.
A local library requested a program to help them organize their book collection. Their collection consists of 3500 books. They would like to be able to display all books by the author, so users could find specific book easier.
For this project, a menu-driven console was created to read data from a text file, and store the data in the vector container.
Each field in the text file was stored in a separate line. Starting the application triggered the creation of a vector and structs, where each struct will contain one book. Furthermore, the structs will contained the following information: author, title, publisher, description, ISBN, and year published fields which are pushed at the back of the vector.
When started, the application presents a welcome message with the current date, and a menu with the following menu items:
Add new book: program adds the book to the vector and to the text file
Remove book: program removes the book from the vector and from the text file
Remove All: program deletes vector
Search by author: Program prompts the user for the author’s name, search a vector for an author, and if found, the author’s name with the book title will be displayed to the user
Search by ISBN Program prompts user for the ISBN number, searches a vector for given ISBN, and if found, the author’s name with the book title and ISBN number will be displayed to the user
Sample code
#include <iostream> #include <fstream> #include <string> #include <vector>
using namespace std;
struct book { string author;
string title; string publisher; string description; string ISBN; string yearPub;
};
int main() {
string tempStr;
book tempBook;
string choiceString = ""; int choiceInt = 0; vector<book>
library; bool found;
ifstream inFile("library.txt"); while (getline(inFile, tempStr)) {
tempBook.author = tempStr; getline(inFile, tempStr); tempBook.title = tempStr; getline(inFile, tempStr); tempBook.publisher = tempStr; getline(inFile, tempStr); tempBook.description = tempStr; getline(inFile, tempStr); tempBook.ISBN = tempStr; getline(inFile, tempStr); tempBook.yearPub = tempStr; library.push_back(tempBook);
} inFile.close();
while (choiceInt != 7) {
// Need this for screen // Need this for files // Need this for strings // Need this for vectors
// Data structure of book type
found = false;
cout << "1. Add new book\n";
cout << "2. Remove book by ISBN\n"; cout << "3. Remove
All\n";
cout << "4. Display book list\n"; cout << "5. Search by
Author\n"; cout << "6. Search by ISBN\n";
cout << "7. Exit\n\n";
getline(cin, choiceString);
cout << endl;
choiceInt = stoi(choiceString);
switch (choiceInt) { case 1:
cout << "Author name? "; getline(cin, tempBook.author); cout << "Title? ";
// Print menu
// Get choice // Change choice to integer for switch
// Add new book
// Open file for reading // While there is still data to import
// Put line items into book type
// Add book to library // Close file // Begin user input portion of program
getline(cin, tempBook.title);
cout << "Publisher? "; getline(cin, tempBook.publisher); cout
<< "Description? "; getline(cin, tempBook.description); cout
<< "ISBN? ";
getline(cin, tempBook.ISBN); cout << "Year published? "; getline(cin, tempBook.yearPub); library.push_back(tempBook); break;
case 2:
cout << "What is the ISBN? "; getline(cin, tempStr);
for (int i = 0; i < library.size(); i++)
if (library[i].ISBN == tempStr) {
library.erase(library.begin()+i);
found = true; }
if (!found)
cout << "ISBN not found\n";
break; case 3:
library.clear();
break; case 4:
for
// Remove all items from library // Print all books
// Put line items into book type // Add book to library
// Delete book by ISBN // For loop to iterate through vector
(int i = 0; i < library.size(); i++) // For loop to iterate through vector
{
cout << "Author : " << library[i].author << " |
Title : " << library[i].title;
cout << "\nISBN : " << library[i].ISBN << " |
Publisher : " << library[i].publisher; cout <<
"\nDescription : " << library[i].description << " |
Year Published : " <<
library[i].yearPub << endl << endl; }
break; case 5:
// Search by author
cout << "Author name? ";
getline(cin, tempStr);
for (int i = 0; i < library.size(); i++) // For loop to iterate
through vector
if (library[i].author == tempStr) {
cout << "Author : " << library[i].author << " | Title : " << library[i].title;
cout << "\nISBN : " << library[i].ISBN << " | Publisher : " << library[i].publisher;
cout << "\nDescription : " << library[i].description << " | Year Published : " << library[i].yearPub << endl << endl;
found = true; }
if (!found)
cout << "Author not found\n";
break; case 6:
// Search by ISBN
cout << "ISBN? ";
getline(cin, tempStr);
for (int i = 0; i < library.size(); i++) // For loop to iterate
through vector
if (library[i].ISBN == tempStr) {
cout << "Author : " << library[i].author << " | Title : " << library[i].title;
cout << "\nISBN : " << library[i].ISBN << " | Publisher : " << library[i].publisher;
cout << "\nDescription : " << library[i].description << " | Year Published : " << library[i].yearPub << endl << endl;
found = true; }
}
if (!found)
cout << "ISBN not found\n";
break; case 7:
break; default:
// Exit
break; }
}
cout << "Not a valid selection.\n\n";
ofstream outFile("library.txt"); while (!library.empty())
{
// Open out file stream // While there are still books in the library
// Get the data from the last book // Delete that book from the linked list
// Output book data to file by line
// Close out file
tempBook = library.front();
library.erase(library.begin());
outFile << tempBook.author << endl; outFile <<
tempBook.title << endl; outFile << tempBook.publisher
<< endl; outFile << tempBook.description << endl;
outFile << tempBook.ISBN << endl; outFile <<
tempBook.yearPub << endl;
} outFile.close();
Here is Solution for above Program:
STL is used for data management, scheduling and other such causes in real-world, Here online order maintenance is done with the help of vectors and pair.This program allows us to maintain database of cutomers,items and orders placed by the cutomers.
// OnlineShopping.cpp : This file contains the 'main'
function. Program execution begin()s and ends there.
//
#include <iostream>
#include<vector>
#include<string>
using namespace std;
int cust_count = 0,item_id=0,order_id=0;
class customer {
public:
string name;
int id;
string address;
customer(string name_temp,string add_temp)
{
name = name_temp;
address = add_temp;
id = cust_count;
cust_count++;
}
string toString()
{
string str = "";
str = "Name : " + name + "\nAddress : " + address + "\nID : " +
to_string(id) + "\n";
return str;
}
};
class item {
public:
string name;
int id;
float price;
item(string name_temp,float price_temp)
{
name = name_temp;
price = price_temp;
id = item_id;
item_id++;
}
string toString()
{
string str = "";
str = "Name : " + name + "\nID : " + to_string(id) +"\nPrice :
"+to_string(price)+"\n";
return str;
}
};
vector<customer> cust_database;
vector<item> item_database;
class order {
public:
int cust_id;
vector<pair<int,int>> item_list;
float sum;
int id;
order(int custid)
{
cust_id = custid;
sum = 0.0;
id = order_id;
order_id++;
}
void add(int id,int quan)
{
item_list.push_back(pair<int,int>(id,quan));
}
float total()
{
sum = 0.0;
for (auto i = item_list.begin(); i != item_list.end(); i++)
{
for (auto j = item_database.begin(); j != item_database.end();
j++)
{
if (i->first == j->id)
{
sum += (float)((j->price)*(i->second));
}
}
}
return sum;
}
void toString() {
cout<<"Order ID :"<<id<<endl;
cout << "Customer Details :" << endl;
for (auto i = cust_database.begin(); i != cust_database.end();
i++)
{
if (i->id == cust_id)
{
cout << i->toString();
break;
}
}
cout << "Item Details :" << endl;
for (auto i = item_list.begin(); i != item_list.end(); i++)
{
for (auto j = item_database.begin(); j != item_database.end();
j++)
{
if (i->first == j->id)
{
cout << j->toString();
cout << "Quantity : " << i->second <<
endl;
}
}
}
cout << "TOTAL SUM : " << total()<<endl;
cout << endl;
}
};
int main()
{
vector <order>order_list;
int choice = 0;
while (true)
{
cout << "Enter choice\n1)Add customer\n2)Removing customer
\n3)Adding item \n4)Removing item \n5)Adding order \n6)Removing
order \n7)Maximum order total \n8)Minimum order total
\n9)Displaying all order \n10)Search customer by ID \n11) Search
item by ID \n12)Search order by ID \n13)Clearing all orders\n or
any other number to Exit\n";
cin >> choice;
switch (choice)
{
case 1:
{
string name, address;
cin.ignore(1, '\n');
cout << "Enter Cust Name :";
getline(cin, name);
cout << endl;
cout << "Enter Addr :";
getline(cin, address);
cout << endl;
customer temp(name, address);
cust_database.push_back(temp);
cout << "Customer Added" << endl;
cout << temp.toString();
break;
}
case 2:
{
int idnum;
cout << "Enter Cust id : ";
cin >> idnum;
for (auto i = cust_database.begin(); i!= cust_database.end();
i++)
{
if (i->id == idnum)
{
cust_database.erase(i);
break;
}
}
break;
}
case 3:
{
string name;
cin.ignore(1, '\n');
cout << "Enter Item Name :";
getline(cin, name);
cout << endl;
cout << "Enter price :";
float price;
cin >> price;
cout << endl;
item temp(name, price);
item_database.push_back(temp);
cout << "Item Added" << endl;
cout << temp.toString();
break;
}
case 4:
{
int idnum;
cout << "Enter Item id : ";
cin >> idnum;
for (auto i = item_database.begin(); i!= item_database.end();
i++)
{
if (i->id == idnum)
{
item_database.erase(i);
break;
}
}
break;
}
case 5:
{
int custid, itemid, quan;
cout << "Enter Cust Id :";
cin >> custid;
cout << endl;
order temp(custid);
char loop = 'y';
do {
cout << "Enter Item Id:";
cin >> itemid;
cout << endl;
cout << "Enter Quantity:";
cin >> quan;
cout << endl;
temp.add(itemid,quan);
cout << "Press Y to cont...";
cin >> loop;
cout << endl;
} while (loop == 'y' || loop == 'Y');
order_list.push_back(temp);
cout << "Order Added\n";
temp.toString();
break;
}
case 6:
{
int idnum;
cout << "Enter Order id : ";
cin >> idnum;
for (auto i = order_list.begin(); i!= order_list.end(); i++)
{
if (i->id == idnum)
{
order_list.erase(i);
break;
}
}
break;
}
case 7:
{
float max = 0.0;
int max_pos=-1;
for (int i = 0; i < order_list.size(); i++)
{
if (order_list[i].total()> max)
{
max = order_list[i].total();
max_pos = i;
}
}
order_list[max_pos].toString();
break;
}
case 8:
{
float min = order_list[0].total();
int min_pos = 0;
for (int i = 0; i < order_list.size(); i++)
{
if (order_list[i].total() < min)
{
min = order_list[i].total();
min_pos = i;
}
}
order_list[min_pos].toString();
break;
}
case 9:
{
for (int i = 0; i < order_list.size(); i++)
{
order_list[i].toString();
}
break;
}
case 10:
{
int idnum;
cout << "Enter Cust id : ";
cin >> idnum;
for (int i = 0; i < cust_database.size(); i++)
{
if (cust_database[i].id == idnum)
{
cout<<cust_database[i].toString();
break;
}
}
break;
}
case 11:
{
int idnum;
cout << "Enter Item id : ";
cin >> idnum;
for (int i = 0; i < item_database.size(); i++)
{
if (item_database[i].id == idnum)
{
cout << item_database[i].toString();
break;
}
}
break;
}
case 12:
{
int idnum;
cout << "Enter Order id : ";
cin >> idnum;
for (int i = 0; i < order_list.size(); i++)
{
if (order_list[i].id == idnum)
{
order_list[i].toString();
break;
}
}
break;
}
case 13:
{
order_list.clear();
break;
}
default:return 1;
}
}
return 0;
}
OUTPUT:
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
1
Enter Cust Name :A
Enter Addr :XYZ
Customer Added
Name : A
Address : XYZ
ID : 0
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
1
Enter Cust Name :B
Enter Addr :GHI
Customer Added
Name : B
Address : GHI
ID : 1
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
3
Enter Item Name :Pen
Enter price :4
Item Added
Name : Pen
ID : 0
Price : 4.000000
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
3
Enter Item Name :Pencil
Enter price :2
Item Added
Name : Pencil
ID : 1
Price : 2.000000
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
5
Enter Cust Id :1
Enter Item Id:1
Enter Quantity:334
Press Y to cont...n
Order Added
Order ID :0
Customer Details :
Name : B
Address : GHI
ID : 1
Item Details :
Name : Pencil
ID : 1
Price : 2.000000
Quantity : 334
TOTAL SUM : 668
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
5
Enter Cust Id :0
Enter Item Id:0
Enter Quantity:45
Press Y to cont...y
Enter Item Id:1
Enter Quantity:2
Press Y to cont...n
Order Added
Order ID :1
Customer Details :
Name : A
Address : XYZ
ID : 0
Item Details :
Name : Pen
ID : 0
Price : 4.000000
Quantity : 45
Name : Pencil
ID : 1
Price : 2.000000
Quantity : 2
TOTAL SUM : 184
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
9
Order ID :0
Customer Details :
Name : B
Address : GHI
ID : 1
Item Details :
Name : Pencil
ID : 1
Price : 2.000000
Quantity : 334
TOTAL SUM : 668
Order ID :1
Customer Details :
Name : A
Address : XYZ
ID : 0
Item Details :
Name : Pen
ID : 0
Price : 4.000000
Quantity : 45
Name : Pencil
ID : 1
Price : 2.000000
Quantity : 2
TOTAL SUM : 184
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
10
Enter Cust id : 1
Name : B
Address : GHI
ID : 1
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
11
Enter Item id : 1
Name : Pencil
ID : 1
Price : 2.000000
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
12
Enter Order id : 1
Order ID :1
Customer Details :
Name : A
Address : XYZ
ID : 0
Item Details :
Name : Pen
ID : 0
Price : 4.000000
Quantity : 45
Name : Pencil
ID : 1
Price : 2.000000
Quantity : 2
TOTAL SUM : 184
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
2
Enter Cust id : 1
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
10
Enter Cust id : 1
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
4
Enter Item id : 1
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
13
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
9
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
5
Enter Cust Id :0
Enter Item Id:0
Enter Quantity:54
Press Y to cont...n
Order Added
Order ID :2
Customer Details :
Name : A
Address : XYZ
ID : 0
Item Details :
Name : Pen
ID : 0
Price : 4.000000
Quantity : 54
TOTAL SUM : 216
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
5
Enter Cust Id :0
Enter Item Id:0
Enter Quantity:3434
Press Y to cont...n
Order Added
Order ID :3
Customer Details :
Name : A
Address : XYZ
ID : 0
Item Details :
Name : Pen
ID : 0
Price : 4.000000
Quantity : 3434
TOTAL SUM : 13736
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
7
Order ID :3
Customer Details :
Name : A
Address : XYZ
ID : 0
Item Details :
Name : Pen
ID : 0
Price : 4.000000
Quantity : 3434
TOTAL SUM : 13736
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
8
Order ID :2
Customer Details :
Name : A
Address : XYZ
ID : 0
Item Details :
Name : Pen
ID : 0
Price : 4.000000
Quantity : 54
TOTAL SUM : 216
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
9
Order ID :2
Customer Details :
Name : A
Address : XYZ
ID : 0
Item Details :
Name : Pen
ID : 0
Price : 4.000000
Quantity : 54
TOTAL SUM : 216
Order ID :3
Customer Details :
Name : A
Address : XYZ
ID : 0
Item Details :
Name : Pen
ID : 0
Price : 4.000000
Quantity : 3434
TOTAL SUM : 13736
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
6
Enter Order id : 2
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
9
Order ID :3
Customer Details :
Name : A
Address : XYZ
ID : 0
Item Details :
Name : Pen
ID : 0
Price : 4.000000
Quantity : 3434
TOTAL SUM : 13736
Enter choice
1)Add customer
2)Removing customer
3)Adding item
4)Removing item
5)Adding order
6)Removing order
7)Maximum order total
8)Minimum order total
9)Displaying all order
10)Search customer by ID
11) Search item by ID
12)Search order by ID
13)Clearing all orders
or any other number to Exit
0