the program that will allow user searc should see the list of items currently a should see the window for the rearch
the program that will allow user searc should see the list of items currently a should see the window for the rearch
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Expert Solution
Step 1
Language: C++/ CPP
Complete Source Code with proper indentation and line by line comments has been provided for better understanding.
Source Code:
--Start of Code:
/* * Create the program that will allow user search the detabaselarray) for an item.
*User should soe the list of tems currently avaliable
*User should see the window for the rearch input
*After entening 3 characters or more the search result shoukd autocomplete - offers suggestions of items that starts with user input as you type text in a search box
*The suggostion list should update if user errase characters or add new ones
*If less then 3 characters are entered -the whole list should be shown
EXAMPLE:
-----------------------------
| Search: A |
-----------------------------
| Apple |
| Banana |
| Avocado |
| Applesauce |
| Apps |
-----------------------------
| Search: App |
-----------------------------
| Apple |
| Applesauce |
| Apps |
-----------------------------
| Search: Apps |
-----------------------------
| Apps |
Create a class Bag which: has name of collection an array of items variable to keep track of number of items in the bag method displayAll method Search (for Autocomplete)
*/
#include <iostream>
// to use cout and cin
#include <string> // to use string
#include <vector> // to use vector
#include <algorithm> // to use sort
using namespace std; // to use cout and cin as std::cout and std::cin
class Bag
{
// class Bag to store all the items in the bag private:
// private variables string name;
// name of the bag vector<string> items;
// vector of items int numberOfItems; // number of items in the bag //Publice functions public: Bag(string name) //constructor { this->name = name; // set the name of the bag } // end of constructor void addItem(string item)
//add the item to the bag
{ items.push_back(item);
// add the item to the vector
}
// end of addItem
void displayAll()
// Displays all the elements in the bag
{
cout << "Bag: " << name << endl;
// display the name of the bag
for (int i = 0; i < items.size(); i++)
// loop through the vector
{
cout << items[i] << endl;
// display the item
}
// end of for loop
}
// end of displayAll
void search()
{
// search for the auto complete
/* * Autocomplete */ string search;
// string to store the search
cout<<"-----------------------------"<<endl;
// display the search box cout<<"| Search: ";
// display the search box cin>>search;
// get the search
cout<<"-----------------------------"<<endl;
// display the search box
vector<string>::iterator it;
// iterator to loop through the
vector it = find_if(items.begin(), items.end(),
[search](string item)
{
// find the item return item.find(search) == 0;
// return the item that starts with the search });
// end of find_if
//if length of search is less than 3 - show all items if (search.length() < 3)
{
displayAll();
// display all items
}
else
//if length of searching item is more than 3
{
//if search is found - show autocomplete
if (it != items.end())
{
//Check if search matches the substring of the item
if (search.length() == (*it).length())
{
cout << *it << endl;
// display the found item
}
else
//if the search dont matches
{
//if search matches the substring of the item - show autocomplete cout << *it << endl;
//search for the rest of the items it++;
// increment the iterator
while (it != items.end())
//loop through the vector of other elements
{
if ((*it).find(search) == 0)
// if the element starts with the search
{ cout << *it << endl;
// display the element
}
// end of
if it++;
// increment the iterator
}
// end of while loop
}
// end of else
}
//end of if else
// nothing found
{
cout << "No items found" << endl;
// display no items found
}
// end of else
}
// end of else
}
// end of search
};
// end of class Bag
int main()
//Main Driver code
{
Bag bag("Fruits");
// create a bag with name of fruits
bag.addItem("Apple");
// add an item to the bag
bag.addItem("Banana"); // add an item to the bag bag.addItem("Avocado"); // add an item to the bag bag.addItem("Applesauce"); // add an item to the bag bag.addItem("Apps"); // add an item to the bag
bag.displayAll(); // display all the items in the
bag do{
// do while loop to run infinitely bag.search();
// search for the item }while(true);
// end of do while loop return 0;
// end of main }
--End of Code
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 4 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education