//*This program to assist a user to input the inventory information management
//into a file that has created from the program itself only once and the data will be saved automatically
#include <iostream>
#include <fstream>
#include <string>
#define Max_num 50
#define ITEM_FILE "C:\\Users\\Administrator\\Desktop\\DD\\Assignment3.txt"
using namespace std;
class item {
private:
// declaring private variables
// Data Members
unsigned long ID;
string name;
float cost;
int quantity;
public:
item(){}
item(unsigned long ID,string name,float cost,int quantity);
unsigned long getID(void);
void setID(unsigned long val);
string getName(void);
void setName(string val);
float getCost(void);
void setCost(float val);
int getquantity(void);
void setquantity(int val);
void PrintIteminfo(void);
void ReadIteminfo(void);
};
class itemlist {
private:
item data[Max_num];
int count;
public:
itemlist() { count = 0; }
void addItem();
void printItem();
void findItemById(unsigned long ID);
void findItemByName(string name);
void storeItemFile(char* );
void loadItemFile(char* );
};
int main() {
itemlist list; // construct the object
int choice;
unsigned long ID;
string name;
list.loadItemFile((char*)ITEM_FILE);
list.printItem();
do {
//To ask the user to select from the menu
cout << "\n\n\t\t\t Welcome to Inventory Management System Menu";
cout << "\n\n1. Add new Item";
cout << "\n2. Print item List";
cout << "\n3. Find item by ID";
cout << "\n4. Find item by name";
cout << "\n5. Quit";
cout << "\nSelect: ";
cin >> choice;
switch (choice){
case 1:
list.addItem();
break;
case 2:
list.printItem();
break;
case 3: //allow user to enter the ID
cout <<"\nPlease enter the id of the item to be found: ";
cin >> ID;
list.findItemById(ID);
break;
case 4: //allow user to enter the Name
cout <<"\nPlease enter the name of the item to be found: ";
getline(cin >>ws, name);
list.findItemByName(name);
break;
case 5:
cout <<"\nThank you Good bye!!!" << endl;
break;
default:
cout << endl << "Incorrect Input, please enter correct input.";
}
} while (choice != 5);
list.storeItemFile((char*)ITEM_FILE);
return 0;
}
void itemlist::loadItemFile(char* file_name){
ifstream myin(file_name);
unsigned long ID; // Data Members
string name;
float cost;
int quantity;
if(myin.is_open()){ // if file can be opened
for(;;){
myin>>ID; //Read the ID
myin.ignore(1); // ignore the ',' by myin
getline(myin>>ws, name,',');
myin>>cost;
myin.ignore(1); // ignore the ',' by myin
myin>>quantity;
if(myin.good()){
item temp(ID,name,cost,quantity);
data[(count)]= temp;
++count; // increment the count
}else if(myin.eof()){
//reached end of file
break;
}else{
cout<<"Error: Not be able to read from file, probably file is corrupted"<<endl;
break;
}
}
} else{
cout<<"ERROR: Not be able to write information to file"<<endl;
}
myin.close();
}
void itemlist::storeItemFile(char* file_name){
ofstream myout(file_name);
if(myout.is_open()){
for(int i=0;i< count;++i){
myout<< data[i].getID()<<','<< data[i].getName()<<','<<data[i].getCost()<<','<<data[i].getquantity()<<endl;
}
}else{
cout<<"ERROR: Not be able to write information to file!!!"<<endl;
}
myout.close(); // close the file
}
void item::ReadIteminfo(void) { // Function to read item
cout << "\n\n\t\t\t Welcome to Inventory Management System Menu";
cout <<"\nEnter item id: ";
cin >> ID;
cout <<"\nEnter item name: ";
getline(cin >>ws, name);
cout <<"\nEnter item cost: ";
cin >> cost;
cout <<"\nEnter quantity: ";
cin >> quantity;
}
void item::PrintIteminfo(void) { //To display the Item
cout << endl;
cout << "\nItem ID: " << ID;
cout << "\nItem name: " << name;
cout << "\nItem cost: " << cost;
cout << "\nItem quantity: " << quantity;
cout << endl;
}
void itemlist::addItem() {
if (count == Max_num){
cout << "list is full" << endl;
}
else{
data[count].ReadIteminfo();
count++;
}
}
void itemlist::printItem() {
for (int i = 0; i < count; i++){
data[i].PrintIteminfo();
}
}
void itemlist::findItemById(unsigned long ID) { //To Take input Item by ID number and display it.
for (int i = 0; i < count; i++){
if (data[i].getID() == ID){
data[i].PrintIteminfo();
return;
}
}
cout << "\nSorry !!! Item not found";
}
void itemlist::findItemByName(string name) { //Take input item name and display it
for (int i = 0; i < count; i++){
if (name == data[i].getName()){
data[i].PrintIteminfo();
break;
}else {
cout << "\nSorry !!! Item not found";
}
}
}
item::item(unsigned long id,string Name,float Cost,int Quantity){
ID=id;
name=Name;
cost=Cost;
quantity=Quantity;
}
unsigned long item::getID(void) {
return ID;
}
void item::setID(unsigned long val) {
ID = val;
}
string item::getName(void) {
return name;
}
void item::setName(string val) {
name = val;
}
float item::getCost(void) {
return cost;
}
void item::setCost(float val) {
cost = val;
}
int item::getquantity(void) {
return quantity;
}
void item::setquantity(int val) {
quantity = val;
}