#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
using namespace std;
class invoice
{
int cNo;
char cName[40];
char cAddr[200];
char sDate[8];
int iNo;
char ides[50];
int iQty;
int iPrice;
public:
void write();
void search();
void modify();
void del();
void show();
};
void invoice::modify()
{
START:
system("cls");
cout<<"-------------------MODIFY---------------------";
cout<<"\n\n1. Search \n2. Enter Customer Number \n3. Go Back\n... ";
int ch;
cin>>ch;
if(ch==1){
search();
goto START;
}
if(ch==2)
{
system("cls");
cout<<"\n\nEnter customer Number : ";
int mCno;
cin>>mCno;
fstream file;
file.open("invoice.dat",ios::in|ios::out|ios::binary);
while(file.read((char*)this ,sizeof(invoice)))
{
if(mCno==cNo)
{
show();
cout<<"\n\nDo you want to modify this : ?";
char ma;
cin>>ma;
if(ma=='y'||ma=='Y')
{
cout<<"\n\nEnter New Data .";
cout<<"\nCustomer Name (enter . to keep it same) : ";
char mCname[50];
fflush(stdin);
gets(mCname);
if(mCname[0]!='.') strcpy(cName,mCname);
cout<<"Customer Address (enter . to keep it same) : ";
char mCadd[200];
fflush(stdin);
gets(mCadd);
if(mCadd[0]!='.') strcpy(cAddr,mCadd);
cout<<"Date of Sale : ";
cout<<"Date of Sale (DD/MM/YYYY) : __/__/____\b\b\b\b\b\b\b\b\b\b";
char a;
int i=0;
for(i;;)
{
a=getch();
if (a>='0'&&a<='9'&&i<8)
{
cout<<a;
sDate[i]=a;
++i;
if (i==2 || i == 4) cout<<"/";
}
else if(a=='\b'&&i>0&&i<9)
{
if(i==2||i==4)
{
cout<<"\b\b_\b";
}
else
cout<<"\b_\b";
--i;
}
else if(a==13)
{
if(i==8)
{
sDate[8]='\0';
break;
}
}
}
cout<<"\nItem Number (enter -1 to keep it same) : ";
int miNo;
cin>>miNo;
if(miNo != -1) iNo=miNo;
cout<<"Description (enter . to keep it same) : ";
char mdes[50];
fflush(stdin);
gets(mdes);
if(mdes[0]!='.') strcpy(ides,mdes);
cout<<"Quantity (enter -1 to keep it same) : ";
int iq;
cin>>iq;
if(iq != -1) iQty = iq;
cout<<"Item Price (enter -1 to keep it same) : ";
int price;
cin>>price;
if(price!=-1) iPrice = price;
int pos = -1*sizeof(invoice);
file.seekp(pos,ios::cur);
file.write((char*)this,sizeof(invoice));
cout<<"\n\nNew Data ";
show();
getch();
break;
}
else continue;
}
}
file.close();
goto START;
}
}
void invoice::del()
{
START:
system("cls");
cout<<"-------------------DELETE----------------------";
cout<<"\n\n1. Search \n2. Enter Customer Number \n3. Go Back\n... ";
int ch;
cin>>ch;
if(ch==1){
search();
goto IN;}
if(ch==2) {
IN:
fstream file;
char d;
ofstream temp;
char ag='y';
while(ag=='y'||ag=='Y')
{
system("cls");
cout<<"\n\nEnter Customer Number : ";
int cn;
cin>>cn;
file.open("invoice.dat",ios::in|ios::binary);
temp.open("temp.dat",ios::binary|ios::app);
temp.seekp(0);
file.seekg(0);
int found=0;
while(file.read((char *)this,sizeof(invoice)))
{
if(cn==cNo)
{
cout<<"--------------------FOUND---------------------\n";
show();
cout<<"\n\nDo you want to delete this entry (y/n): ";
cin>>d;
found = 1;
if(d=='y' ||d == 'Y')
{
continue;
}
else temp.write((char*)this,sizeof(invoice));
}
else temp.write((char*)this,sizeof(invoice));
}
file.close();
temp.close();
remove("invoice.dat");
rename("temp.dat","invoice.dat");
if (found == 1)
{
cout<<"\nDelete successful";
cout<<"\nDelete More? (y/n): ";
}
else
{
cout<<"\nNo results found.\nTry again(y/n) : ";
}
cin>>ag;
}
goto START;
}
}
void invoice::search()
{
ifstream file;
char ch='y';
char ch1='y';
while(ch=='y'||ch=='Y')
{
START:
system("cls");
cout<<"-------------------------------SEARCH---------------------------";
cout<<"\nSearch by : \n1. Customer Number\n2. Customer Name\n3. Date of Sale\n4. Item Number\n5. Cancel\n\n...";
int s;
cin>>s;
if(s==1)
{
ch1='y';
while(ch1=='y'||ch1=='Y')
{
system("cls");
cout<<"Enter Customer Number : ";
int cn;
cin>>cn;
int found = 0;
file.open("invoice.dat",ios::binary);
file.seekg(0);
while(file.read((char*)this,sizeof(invoice)))
{
if(cNo==cn)
{
cout<<"\n-------------------------------FOUND---------------------------\n\n";
show();
getch();
found = 1;
}
else found = 0;
}
file.close();
if(found == 0)
{
cout<<"\nNo Record Found";
}
cout<<"\n\nSearch Again (y/n) : ";
cin>>ch1;
}
goto START;
}
if(s==2)
{
ch1='y';
while(ch1=='y'||ch1=='Y')
{
system("cls");
cout<<"Enter Customer Name : ";
char nSearch[40];
fflush(stdin);
gets(nSearch);
file.open("invoice.dat",ios::binary);
file.seekg(0);
int ffound = 0;
while(file.read((char*)this,sizeof(invoice)))
{
int i=0,j=0;
int found = 1;
for(i,j;cName[i]!='\0'&&nSearch[j]!='\0';)
{
if(toupper(nSearch[j])==toupper(cName[i]))
{
++i;
++j;
ffound =1;
continue;
}
else
{
++i;
found =0;
}
}
if (found == 1)
{
system("cls");
cout<<"-------------------------------FOUND---------------------------\n\n";
show();
getch();
}
}
file.close();
if(ffound == 0)
{
cout<<"\nNo Record Found";
}
cout<<"\n\nSearch Again (y/n) : ";
cin>>ch1;
}
goto START;
}
if(s==3)
{
ch1='y';
while(ch1=='y'||ch1=='Y')
{
system("cls");
cout<<"Date of Sale (DD/MM/YYYY) : __/__/____\b\b\b\b\b\b\b\b\b\b";
char a;
char cDate[8];
int i=0;
for(i;;)
{
a=getch();
if (a>='0'&&a<='9'&&i<8)
{
cout<<a;
cDate[i]=a;
++i;
if (i==2 || i == 4) cout<<"/";
}
else if(a=='\b'&&i>0&&i<9)
{
if(i==2||i==4)
{
cout<<"\b\b_\b";
}
else
cout<<"\b_\b";
--i;
}
else if(a==13)
{
if(i==8)
{
cDate[8]='\0';
break;
}
}
}
int ffound = 0;
file.open("invoice.dat",ios::binary);
file.seekg(0);
while( file.read((char*)this,sizeof(invoice)))
{
int found = 0;
for(i=0;cDate[i]!='\0'&&sDate[i]!='\0';++i)
{
if(cDate[i]!=sDate[i]) {
found = 1;
break;
}
}
if(found == 0)
{
cout<<"\n-------------------------------FOUND---------------------------\n\n";
show();
getch();
ffound = 1;
break;
}
else ffound = 0;
}
file.close();
if(ffound == 0)
{
cout<<"\nNo Record Found";
}
cout<<"\n\nSearch Again (y/n) : ";
cin>>ch1;
}
goto START;
}
if(s==4)
{
ch1='y';
while(ch1=='y'||ch1=='Y')
{
system("cls");
cout<<"Enter Item Number : ";
int in;
cin>>in;
int found = 0;
file.open("invoice.dat",ios::binary);
file.seekg(0);
while(file.read((char*)this,sizeof(invoice)))
{
if(iNo==in)
{
cout<<"\n-------------------------------FOUND---------------------------\n\n";
show();
getch();
found = 1;
}
else found = 0;
}
file.close();
if(found == 0)
{
cout<<"\nNo Record Found";
}
cout<<"\n\nSearch Again (y/n) : ";
cin>>ch1;
}
goto START;
}
if(s==5)
break;
cout<<"\nSearch More : (y/n)...";
cin>>ch;
}
}
void invoice::show()
{
cout<<"Customer No : "<<cNo;
cout<<"\nCustomer Name : "<<cName;
cout<<"\nCustomer Address : "<<cAddr;
cout<<"\nDate of Sale : ";
int j=0;
for(int i=0;i<10;++i)
{
if (i==2||i==5)
cout<<"/";
else
{
cout<<sDate[j];
++j;
}
}
cout<<"\nItem Number : "<<iNo;
cout<<"\nDescription : "<<ides;
cout<<"\nQuantity : "<<iQty;
cout<<"\nPrice : "<<iPrice;
}
void invoice::write()
{
char ch ='y';
ofstream file;
file.open("invoice.dat",ios::app|ios::binary);
while(ch=='y'||ch=='Y')
{
system("cls");
cout<<"-------------------------------Entry---------------------------";
cout<<"\nCustomer Number : ";
cin>>cNo;
cout<<"Customer Name : ";
fflush(stdin);
gets(cName);
cout<<"Customer Address : ";
fflush(stdin);
gets(cAddr);
cout<<"Date of Sale (DD/MM/YYYY) : __/__/____\b\b\b\b\b\b\b\b\b\b";
char a;
int i=0;
for(i;;)
{
a=getch();
if (a>='0'&&a<='9'&&i<8)
{
cout<<a;
sDate[i]=a;
++i;
if (i==2 || i == 4) cout<<"/";
}
else if(a=='\b'&&i>0&&i<9)
{
if(i==2||i==4)
{
cout<<"\b\b_\b";
}
else
cout<<"\b_\b";
--i;
}
else if(a==13)
{
if(i==8)
{
sDate[8]='\0';
break;
}
}
}
cout<<"\nItem Number : ";
cin>>iNo;
cout<<"Description : ";
fflush(stdin);
gets(ides);
cout<<"Quantity Sold : ";
cin>>iQty;
cout<<"Price of Item : ";
cin>>iPrice;
file.write((char *)this,sizeof(invoice));
cout<<"\nEntry successful";
cout<<"\n\nEnter again (y/n)...";
cin>>ch;
}
file.close();
}
int main()
{
START:
system("cls");
cout<<"------------------------CUSTOMER INVOICE--------------------";
cout<<"\n\n1. Enter New Data\n2. Search\n3. Modify\n4. Delete\n5. View Data\n\n...";
int a;
cin>>a;
invoice in;
if(a==1)
{
system("cls");
in.write();
goto START;
}
if(a==2)
{
in.search();
goto START;
}
if(a==3)
{
in.modify();
goto START;
}
if(a==4)
{
in.del();
goto START;
}
if(a==5)
{
ifstream file;
file.open("invoice.dat",ios::binary);
invoice c;
file.seekg(0);
if(!file.read((char*)&c,sizeof(c)))
{
cout<<"No records Presents.";
getch();
goto START;
}
file.seekg(0);
while(file.read((char*)&c,sizeof(c)))
{
system("cls");
c.show();
getch();
}
file.close();
goto START;
}
return 0;
}