Oop(Object Oriented Programming) Project for final year students
Project Description:
Write the C++ Code for the following parts
A. Create a class Product. Also create two descendants (derived from Product class) Food and Clothes.
B. The class Product must have a data members Product_Id, Price and Name. It contains a constructor to initialize the data members and “getdata ()” method to store the values in data members.
C. The class " Clothes " will have additional data members Color. The Clothes class also contains a constructor to initialize the data members and “getdata ()” method to store the values in data members. It additionally contains
·
storedata ()
method to store data in file named as “Clothes.txt”
·
readdata ()
method to read all information from this file
·
deletedata () to
delete specific product from file
·
updatedata () to
update any record
·
searchdata () to
search the details of specific product entered by user.
D. Make sure duplicate Product_Ids should not be entered in files
E. Create separate header files for each class
F. Write a menu-driven program using the Switch case and do while to select any choice from multiple option to obtain desired result
Cpp File:
#include "stdafx.h"
#include"iostream"
#include"conio.h"
#include"string"
#include"Clothes.h"
#include"Food.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Product *ptr;
Clothes c;
Food f;
int choice;
char a;
do
{
cout << "\t\t<< Project
Menu Box >>" << endl;
cout << endl;
cout << endl;
cout << "1/. Enter 1 for
Clothes Details" << endl;
cout << "2/. Enter 2 for Food
Details"
<< endl;
cin >> choice;
switch (choice)
{
case 1:
{
int num;
cout << "\t\t<< Clothes order's Detail
File >>" << endl;
cout << endl;
cout << endl;
cout << "Enter 1 for add Clothes order details" << endl;
cout << "Enter 2 for read order details" << endl;
cout << "Enter 3 for delete order
details"
<< endl;
cout << "Enter 4 for search order
details"
<< endl;
cout << "Enter 5 for update order
details"
<< endl;
cin >> num;
{
case 1:
{
ptr =&c;
ptr->set_details();
ptr->store_details();
}break;
case 2:
{
ptr = &c;
ptr->read_details();
}break;
case 3:
{
ptr = &c;
ptr->delete_details();
ptr->read_details();
}break;
case 4:
{
ptr = &c;
ptr->search_details();
}break;
case 5:
{
ptr = &c;
ptr->update_details();
}break;
default:
{
cout << "Invalid command" << endl;
} break;
}
}break;
case 2:
{
int choice;
cout << "\t\t<< Food's Detail File
>>"
<< endl;
cout << endl;
cout << endl;
cout << "Enter 1 for supplier details" << endl;
cout << "Enter 2 for add supplier
details"
<< endl;
switch (choice)
{
case 1:
{
ptr = &f;
ptr->read_details();
}
break;
case 2:
{
ptr = &f;
ptr->set_details();
}break;
default:
{
cout << "Invalid command" << endl;
} break;
}
}break;
default:
{
cout << "Invalid command " << endl;
}break;
}
cout << "Do you want to
perform another task(n or y)" << endl;
cin >> a;
} while (a != 'n');
_getch();
return 0;
}
Clothes Header File:
#pragma once;
#include "stdafx.h"
#include"iostream"
#include"conio.h"
#include"string"
#include"fstream"
#include"Product.h"
using namespace std;
class Clothes:public Product
private:
string color,type,cloth_name,brand_name;
int cloth_id, cloth_price;
public:
void set_details()
{
Product::set_details();
cloth_id
= Product::get_id();
int search_id = cloth_id;
ifstream orig;
bool found = false;
orig.open("Clothes.txt", ios::in);
orig.read((char*)this, sizeof(*this));
while (!orig.eof())
{
if (this->cloth_id == search_id)
{
found
= true;
cout << "Already Record for
this id exist (Enter Again Plz)" << endl;
cin >> cloth_id;
}
else
{
cout
<< "Not
Duplicated" << endl;
}
orig.read((char*)this, sizeof(*this));
break;
}
cloth_price
= Product::get_price();
cloth_name
= Product::get_name();
cout
<< "Enter
Color of cloth :" << endl;
cin
>> color;
cout << "Enter Type of
cloth(linen,shiffon,cotton,lawn) :" << endl;
cin >> type;
cout << "Enter Brand of cloth
:" << endl;
cin >> brand_name;
}
void get_details()
{
cout << "Cloth Id :" << cloth_id <<
endl;
cout << "Cloth Name :" << cloth_name <<
endl;
cout << "Color of cloth
:" << color <<
endl;
cout << "Enter Type of
cloth(linen,shiffon,cotton,lawn) :" << type << endl;
cout << "Cloth Price :" << cloth_price
<< endl;
void read_details();
void delete_details();
void store_details();
void heading();
void show_details();
void update_details();
void search_details();
};
void Clothes::read_details()
{
ifstream readfile;
readfile.open("Clothes.txt", ios::in);
if (readfile.fail())
{
cout
<< "File
not open successfully";
exit(1);
}
heading();
readfile.read((char*)this, sizeof(*this));
while (!readfile.eof())
{
show_details();
readfile.read((char*)this, sizeof(*this));
}
readfile.close();
}
void Clothes::delete_details()
{
int id;
ifstream orig;
ofstream newfile;
bool found = false;
orig.open("Clothes.txt", ios::in);
newfile.open("temp.txt", ios::out | ios::app);
cout
<< "Enter
Cloth ID:" << endl;
cin
>> id;
orig.read((char*)this, sizeof(*this));
while (!orig.eof())
{
if (this->cloth_id == id)
{
found
= true;
}
else
newfile.write((char*)this, sizeof(*this));
}
orig.read((char*)this, sizeof(*this));
}
if (found == false)
{
cout << "Not found" << endl;
}
else
{
cout << "Record Deleted" << endl;
}
orig.close();
newfile.close(); remove("Clothes.txt");
rename("temp.txt","Clothes.txt"); remove("temp.txt");
}
void Clothes::store_details()
{
ofstream writefile;
writefile.open("Clothes.txt", ios::out | ios::app);
if (writefile.fail())
{
cout << "File not open
successfully" << endl;
exit(1);
}
writefile.write((char*)this, sizeof(*this));
writefile.close();
}
void Clothes:: heading()
{
cout << "Id" << "\t" << "Name" << "\t" << "color" << "\t" << "price" << "\t" << "Brand Name" << endl;
}
void Clothes::show_details()
{
cout << cloth_id << "\t" << cloth_name <<
"\t" << color << "\t" << cloth_price
<< "\t" << brand_name <<
endl;
}
void Clothes::update_details()
{
fstream edit;
int choice = 0, count = 0,id;
char c;
bool found = false;
edit.open("Clothes.txt", ios::out | ios::in);
cout << "Enter Cloth Id:" << endl;
cin >> id;
edit.read((char*)this, sizeof(*this));
edit.seekg(0, ios::beg);
while (!edit.eof())
{
count++;
if (this->cloth_id == id)
{
label:
cout << "Enter your choice
:" << endl;
cout << "Enter 1 to update
Cloth name: " << endl;
cout << "Enter 2 to update
Brand Name of Cloth: " << endl;
cout << "Enter 3 to update All
Information: " << endl;
cin >> choice;
{
case 1:
{
cout<<"Enter Name of Cloth:"<< endl;
cin >> cloth_name;
}break;
case 2:
{
cout<<"Enter Brand Name of
Cloth:"<<
endl;
cin >> brand_name;
}break;
case 3:
{
set_details();
}break;
default:
{
cout << "Invalid Command " << endl;
cout << "Do you want to update again (y or n)" << endl;
cin >> c;
if (c == 'y')
{
goto label;
}
else
{
exit(1);
}
}break;
}
int pos = (count - 1)*sizeof(*this);
edit.seekp(pos);
edit.write((char*)this, sizeof(*this));
found = true;
break;
}
edit.read((char*)this, sizeof(*this));
}
if (found == true)
{
cout
<< "Record
Updated"
<< endl;
}
else
{
cout
<< "Record
Not Found" << endl;
exit(1);
}
edit.close();
}
void Clothes::search_details()
{
int search_clothid;
ifstream orig;
bool found = false;
orig.open("Clothes.txt", ios::in);
cout
<< "Enter
Cloth Id:" << endl;
cin
>> search_clothid;
orig.read((char*)this, sizeof(*this));
while (!orig.eof())
{
if (this->cloth_id ==
search_clothid)
{
found
= true;
show_details();
}
else
{
cout
<< "Record
Not Found" << endl;
}
orig.read((char*)this, sizeof(*this));
break;
}
}
Product Header File:
Duplicate Record Exception
#include "stdafx.h"
#include"iostream"
#include"conio.h"
#include"string"
using namespace std;
class Product
{
public:
int product_id, price;
string name;
Product()
{
product_id
= 0;
price
= 0;
name
= "nill";
}
virtual void set_details()
{
cout<<"Enter ID of product:"<<endl;
cin >> product_id;
cout<<"Enter Price of
product:"<<endl;
cin >> price;
cout << "Enter Name of
product:"
<< endl;
cin >> name;
}
virtual void get_details()
{
cout << "ID of product:"
<<product_id<<endl;
cout << "Price of
product:"
<<price<<endl;
cout << "Name of
product:"
<<name<<endl;
}
int get_id()
{
return product_id;
}
int get_price()
{
return price;
}
string get_name()
{
return name;
virtual void read_details()=0;
virtual void delete_details()=0;
virtual void store_details()=0;
virtual void search_details()=0;
virtual void update_details()=0;
};
Food Header File:
#pragma once;
#include "stdafx.h"
#include"iostream"
#include"conio.h"
#include"string"
#include"fstream"
#include"Product.h"
using namespace std;
class Food : public Product
{
private:
int food_id, food_price;
public:
Food()
{
type
= "nill";
quantity
= "nill";
}
void set_details()
{
Product::set_details();
cout << "Enter Type of Food
:" << endl;
cin >> type;
cout << "Enter Quantity of
Food :"
<< endl;
cin >> quantity;
}
void read_details()
{
food_id = Product::get_id();
food_price = Product::get_price();
food_name = Product::get_name();
cout << "Food Id :" <<food_id <<
endl;
cout << "Food Name :" << food_name <<
endl;
cout << "Type of Food :" << type << endl;
cout<<"Quantity of Food:"<< quantity<<
endl;
cout << "Food Price :" << food_price <<
endl;
}
};
**THE END**
THANK
YOU!!!!!.......
Comments
Post a Comment