CODE
//GROUP1: Isaac Bernal, Ralph Mejia, Carlos Molina
//DATE: 4/21/22
//DESC: This program will used by staff to find store items by ID number or item name. Also it will give
the option to display by unsorterd or by ascending order
//CLASS: 2022/SP COSC-1437-T01 Fundamentals of Prgramming 2
// *--- This file is the CLIENT PROGRAM FILE ---*
// *--- main.cpp ---*
#include <iostream>
#include <fstream>//to access textfile
#include <string>
#include <iomanip>
#include "Menu.h"//to access specification file
using namespace std;
//Function Prototypes
void welcome();
void menuOption();
int getData(Menu[], const int);
void listUnasorted(Menu[], int&);
void listAscendId(Menu[], int&);
void listAscendName(Menu[], int&);
void listAscendPrice(Menu[], int&);
int searchName(Menu[], int&, string);
int searchId(Menu[], int&, int);
void searchPrint(Menu[], int&);
void validation(int);
int main()
const int INDEX = 50;
Menu items_menu[INDEX];// the class Object of array
int listCount;//use to hold the count number of object of arrays
char option;//the letter option for how to search
char redo;//variable for redoing the program
string userName;// holds the user input for the name being search for
int userID;// holds the user input for the ID being search for
int location;//variable returns counter from the search functions
listCount = getData(items_menu, INDEX);//calls and returns counter from the function
welcome();
do
menuOption();
cin >> option;
if(option == 'A' ||option == 'a' || option == 'B' ||option == 'b' || option == 'C' ||option == 'c' || option
== 'D' ||option == 'd' || option == 'E' ||option == 'e' || option == 'F' ||option == 'f' || option == 'G' ||
option == 'g')
// Option display all the inventory list in a unsorted
if(option == 'A' ||option == 'a')
{
listUnasorted(items_menu, listCount);
//Option for searching and displaying the inventory by name
else if(option == 'B'|| option == 'b')
listAscendName(items_menu, listCount);
cout << "What is the Name of the item: ";
cin.ignore();
getline(cin, userName);
location = searchName(items_menu, listCount, userName);
if (location == -1)
validation(location);
else if (location >=0)
searchPrint(items_menu, location);
//Option searches and displayID the user inputted
else if(option == 'C'||option == 'c')
listAscendId(items_menu, listCount);
cout << "What ID: ";
cin.ignore();
cin >> userID;
location = searchId(items_menu, listCount, userID);
if (location == -1)
validation(location);
else if (location >=0)
searchPrint(items_menu, location);
//Option sets Name by ascending order
else if(option == 'D' || option == 'd')
listAscendName(items_menu, listCount);
listUnasorted(items_menu, listCount);
//Option sets ID by ascending order
else if(option == 'E' || option == 'e')
listAscendId(items_menu, listCount);
listUnasorted(items_menu, listCount);
//Option sets price by ascending order
else if(option == 'F' || option == 'f')
listAscendPrice(items_menu, listCount);
listUnasorted(items_menu, listCount);
else if(option == 'G' || option == 'g')
break;
else
cout << "\nWrong Option Input, Make sure its the appropriate letter. ";
//Asks the user if they want to go again
cout << "\n\nWould you like to do anything else?(Y/N): ";
cin.ignore();
cin >> redo;
cout << endl;
//Looped for if the User wants to try again
while (redo == 'y' || redo == 'Y');
cout << "\n\n\t-* Thank You For Your Time *-";
return 0;//End of program
//Only Displays
void welcome()
{
cout << "★──────────★─────────★──────────★─────────★──────────★";
cout << endl;
cout << "\n ✦Welcome to Isaac's General Store✦\n\n";
cout << "★──────────★─────────★──────────★─────────★──────────★";
//Only Displays
void menuOption()
cout << "\n\nSelect an Option from below\n\n";
cout << "╒═══════════════════════════════════╕";
cout << "\n Option A: Unassorted Display\n";
cout << "╘═══════════════════════════════════╛\n\n";
cout << "╒═══════════════════════════════════╕";
cout << "\n Option B: Search by Name\n";
cout << "╘═══════════════════════════════════╛\n\n";
cout << "╒═══════════════════════════════════╕";
cout << "\n Option C: Search by ID\n";
cout << "╘═══════════════════════════════════╛\n\n";
cout << "╒═══════════════════════════════════╕";
cout << "\n Option D: Ascending by Name\n";
cout << "╘═══════════════════════════════════╛\n\n";
cout << "╒═══════════════════════════════════╕";
cout << "\n Option E: Ascending by ID\n";
cout << "╘═══════════════════════════════════╛\n\n";
cout << "╒═══════════════════════════════════╕";
cout << "\n Option F: Ascending by Price\n";
cout << "╘═══════════════════════════════════╛\n\n";
cout << "╒═══════════════════════════════════╕";
cout << "\n Option G: Last Minute Quit\n";
cout << "╘═══════════════════════════════════╛\n\n";
cout << "\nEnter your option: ";
//Recieves: class Array and constant
//Returns: filled class array and counter of total number of items
int getData(Menu mArray[], const int INDEX)
ifstream inputFile;
inputFile.open("menu.txt");
if(inputFile.fail())//if file doesnt exist
cout << "ERROR reading file. OR file not found.";
string itemName;//temporary variable hold
string itemDescription;//temporary variable hold
float itemPrice;//temporary variable hold
int itemId;//temporary variable hold
int counter = 0;
while(inputFile >> itemId)//for every id loop recieves, loop continues
{
mArray[counter].setID(itemId);//stores in class function
inputFile.ignore(1, '\n');//keeps reading char. return, so added ignore
getline(inputFile, itemName);
mArray[counter].setName(itemName);//stores in class function
inputFile.clear();
getline(inputFile, itemDescription);
mArray[counter].setDescrip(itemDescription);//stores in class function
inputFile.clear();
inputFile >> itemPrice;
mArray[counter].setPrice(itemPrice);//stores in class function
inputFile.clear();
counter++;
inputFile.close();// closes textfile
return counter;
}
//---------List Functions---------------
//Recieves: Class Array and counter
//Returns; Nothing
void listUnasorted(Menu mArray[], int& INDEX)
for (int c = 0; c < INDEX ; c++ )
cout << "\n Item " << c + 1;
cout << "\n ID: " << mArray[c].getID();
cout << "\n Name: " << mArray[c].getName();
cout << "\n Description: " << mArray[c].getDescrip();
cout << "\n Price: $" << fixed << setprecision(2) << mArray[c].getPrice();
cout << endl;
//Recieves: Class Array and counter
//Returns; Nothing
void listAscendId(Menu mArray[], int& INDEX)
int i;
int j;
Menu temp; // Temporary variable for swap
for (i = 1; i < INDEX; ++i)
{
j = i;
// Insert mArray[i] into sorted part
// stopping once mArray[i] in correct position
while (j > 0 && mArray[j].getID() < mArray[j - 1].getID())
// Swap numbers[j] and numbers[j - 1]
temp = mArray[j];
mArray[j] = mArray[j - 1];
mArray[j - 1] = temp;
--j;
//Recieves: Class Array and counter
//Returns; Nothing
void listAscendName(Menu mArray[], int& INDEX)
int i;
int j;
Menu temp; // Temporary variable for swap
for (i = 1; i < INDEX; ++i)
j = i;
// Insert mArray[i] into sorted part
// stopping once mArray[i] in correct position
while (j > 0 && mArray[j].getName() < mArray[j - 1].getName())
// Swap mArray[j] and mArray[j - 1]
temp = mArray[j];
mArray[j] = mArray[j - 1];
mArray[j - 1] = temp;
--j;
//Recieves: Class Array and counter
//Returns; Nothing
void listAscendPrice(Menu mArray[], int& INDEX)
int i;
int j;
Menu temp; // Temporary variable for swap
for (i = 1; i < INDEX; ++i)
j = i;
// Insert mArray[i] into sorted part
// stopping once mArray[i] in correct position
while (j > 0 && mArray[j].getPrice() < mArray[j - 1].getPrice())
// Swap mArray[j] and mArray[j - 1]
temp = mArray[j];
mArray[j] = mArray[j - 1];
mArray[j - 1] = temp;
--j;
//---------Search Functions-----------------
//Recieves: Class Array, counter, and user's input
//Returns: counter of the location of searched item
int searchName(Menu mArray[], int& INDEX, string userSearch)
int mid;// middle of the array
int low;// lowest point in the array
int high;// highest poitn in the array
low = 0; // sets the first subscript in the array at [0]
high = INDEX - 1; // sets the last subscript number of the array
while (high >= low) //while there are still numbers in the list
mid = (high + low) / 2; // find the middle point in the list
if (mArray[mid].getName() < userSearch) // if the middle value in the list is smaller than the number
we are looking for
low = mid + 1; // make the middle point the new low
}
else if (mArray[mid].getName() > userSearch) // if the middle value in the list is greater than the
number we're looking for
high = mid - 1; // make the middle point the new high
else // do this when the middle value is the number we're looking for
return mid; // return the position of the middle value in the array
} // end of while loop
return -1; // Value not found, return -1
//Recieves: Class Array, counter, and user's input
//Returns: counter of the location of searched item
int searchId(Menu mArray[], int& INDEX, int userSearch)
int mid;// middle of the array
int low;// lowest point in the array
int high;// highest poitn in the array
low = 0; //Sets the first subscript in the array at [0]
high = INDEX - 1; //Sets the last subscript number of the array
while (high >= low) //While there are still numbers in the list
{
mid = (high + low) / 2; //Find the middle point in the list
if (mArray[mid].getID() < userSearch) //If the middle value in the list is smaller than the number we
are looking for
low = mid + 1; //Make the middle point the new low
else if (mArray[mid].getID() > userSearch) //If the middle value in the list is greater than the number
we're looking for
high = mid - 1; //Make the middle point the new high
else //Do this when the middle value is the number we're looking for
return mid; //Return the position of the middle value in the array
} //End of while loop
return -1; //Value not found, return -1
//Recieves: Class Array and counter
//Returns: Nothing
void searchPrint(Menu mArray[], int& INDEX)
{
cout << "\n Item " << INDEX + 1;
cout << "\n ID: " << mArray[INDEX].getID();
cout << "\n Name: " << mArray[INDEX].getName();
cout << "\n Description: " << mArray[INDEX].getDescrip();
cout << "\n Price: $" << fixed << setprecision(2) << mArray[INDEX].getPrice();
//----------------Validation---------------
//Only Displays
void validation(int lcounter)
cout << "\nNot In Inventory List ";
/*
*--- This is the IMPLEMENTATION FILE ---*
*--- menu.cpp ---*
*/
#include <string>
using namespace std;
#include "Menu.h"
// Class Public Functions Definitions
// Mutator function
void Menu::setID(int i)
id = i;
void Menu::setName(string n)
name = n;
void Menu::setDescrip(string d)
description = d;
void Menu::setPrice(float p)
price = p;
int Menu::getID()
return id;
string Menu::getName()
return name;
}
string Menu::getDescrip()
return description;
float Menu::getPrice()
return price;
*--- This file is the SPECIFICATION FILE ---*
*--- Menu.h ---*
*/
#include <string>
using namespace std;
#ifndef MENU_H
#define MENU_H
class Menu//Class
private:
int id;
string name;
string description;
float price;
public:
void setID(int i);
void setName(string n);
void setDescrip(string d);
void setPrice(float p);
int getID();
string getName();
string getDescrip();
float getPrice();
};
#endif
Screen Captures
List Unsorted
Displays Search by Name
Displays Search by ID
Displays Ascending by Name
Displays Ascending by ID
Displays Ascending by Price
Displays Options to Quit