Note - Double Click to Copy Code Contact Us!

Stock Management System using C++

Tech Doubility
Stock Management System using C++



 
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<fstream>
  4. #include<string.h>
  5. #include<stdio.h>
  6. #include<ctype.h>
  7. using namespace std;
  8. char ch;
  9.  
  10. class Stock{
  11. //Stock Information
  12. char item_name[80];
  13. int q_item;
  14. int item_code;
  15. char c_name[80];
  16. float price;
  17. float total_price;
  18.  
  19. //Customer Information
  20. char consign_no[18];
  21. int cus_no;
  22. int i_code;
  23. char cus_name[80];
  24. char date[80];
  25. char address[80];
  26. int q_cus;
  27.  
  28.  
  29. public:
  30. void getdata_s();
  31. void getdata_c();
  32. void display_s();
  33.  
  34. void modify_s();
  35.  
  36. char * getname() { return item_name;}
  37. int getcode() { return item_code;}
  38. char * getcname() { return c_name;}
  39. int getcodec() { return i_code;}
  40. int getq_item(){ return q_item;}
  41. int getq_cus(){ return q_cus;}
  42. void change(int, int);
  43. }s1, s2;
  44. void Stock::change(int x, int y)
  45. {
  46. s2.q_item= y-x;
  47. }
  48.  
  49. void Stock::getdata_s()
  50. {
  51. cout<<"Enter Item Code :"; cin>>item_code; cin.get(ch);
  52. cout<<"Enter Item name :"; cin.getline(item_name,80);
  53. cout<<"Enter Quantity :"; cin>>q_item; cin.get(ch);
  54. cout<<"Enter Company name :"; cin.getline(c_name,80);
  55. cout<<"Enter price :"; cin>>price;
  56. }
  57.  
  58.  
  59. void Stock::getdata_c()
  60. {
  61. cout<<"Enter customer number :"; cin>>cus_no; cin.get(ch);
  62. cout<<"Enter customer name :"; cin.getline(cus_name, 80);
  63. cout<<"Enter consignment number :"; cin.getline(consign_no,18);
  64. cout<<"Enter date of purchase :"; cin.getline(date, 80);
  65. cout<<"Enter code of the item :"; cin>>i_code; cin.get(ch);
  66. cout<<"Enter address :"; cin.getline(address,80);
  67. cout<<"Enter quantity required :"; cin>>q_cus;
  68. }
  69.  
  70. void Stock::display_s()
  71. { cout<<endl;
  72. cout<<endl;cin.get(ch);
  73. cout<<"Item code :"<<item_code<<endl;
  74. cout<<"Item name :"<<item_name<<endl;
  75. cout<<"Quantity :"<<q_item<<endl;
  76. cout<<"Company name :"<<c_name<<endl;
  77. cout<<"Price :"<<price<<endl;
  78. total_price=price*q_item;
  79. cout<<"Total price :"<<total_price<<endl; cin.get(ch);
  80. }
  81.  
  82.  
  83. /* void Stock::display_c()
  84.   {
  85.   cout<<"Customer number :"<<cus_no<<endl;
  86.   cout<<"Customer name :"<<cus_name<<endl;
  87.   cout<<"Consignment no :"<<consign_no<<endl;
  88.   cout<<"Item code :"<<i_code;
  89.   cout<<"Purchased date :"<<date;
  90.   cout<<"Quantity purchased:"<<q_cus;
  91.   cout<<"Address :"<<address;
  92.   }*/
  93.  
  94.  
  95. void Stock::modify_s()
  96. { cout<<"Item code :"<<item_code<<endl;
  97. cout<<"Item name :"<<item_name<<endl;
  98. cout<<"Quantity :"<<q_item<<endl;
  99. cout<<"Company name :"<<c_name<<endl;
  100. cout<<"Price :"<<price<<endl;
  101. cout<<"Enter new details to modify"<<endl;
  102. char nm[80]="", c[80]=""; int ncode, nq, np;
  103. cout<<"New code (Press -1 to retain old one):" ;
  104. cin>>ncode; cin.get(ch);
  105. cout<<"New name (Enter'.' to retain old one):";
  106. cin.getline(nm,80);
  107. cout<<"New quantity (Press -1 to retain old one):";
  108. cin>>nq; cin.get(ch);
  109. cout<<"New company name (Enter '.' to retain old one);";
  110. cin.getline(c,80);
  111. cout<<"New price (Press -1 to retain old one):";
  112. cin>>np;
  113.  
  114. if(strcmp(nm, ".")!=0)
  115. strcpy(item_name, nm);
  116. if(ncode!=-1)
  117. item_code=ncode;
  118. if(nq!=-1)
  119. q_item=nq;
  120. if(np!=-1)
  121. price=np;
  122.  
  123.  
  124. }
  125.  
  126.  
  127.  
  128. int main()
  129. { Stock s3;
  130. cout<<" ___________________________________________________________________________ "<<endl;
  131. cout<<" | ----- ----- ----- ---- | | |"<<endl;
  132. cout<<" | | | | | | | - |"<<endl;
  133. cout<<" | ----- | | | | | - |"<<endl;
  134. cout<<" | | | | | | | - |"<<endl;
  135. cout<<" | ----- | ----- ---- | | |"<<endl;
  136. cout<<" | |"<<endl;
  137. cout<<" | -- -- ---- - - ---- ---- ---- -- -- ---- - - ----- |"<<endl;
  138. cout<<" | | | | | | | - | | | | | | | | | | - | | |"<<endl;
  139. cout<<" | | | | |____| | - | |____| | ___ |---- | | | |---- | - | | |"<<endl;
  140. cout<<" | | | | | | | - | | | | | | | | | | | - | | |"<<endl;
  141. cout<<" | | | | | | -| | | ---- ---- | | ---- | -| | |"<<endl;
  142. cout<<" | |"<<endl;
  143. cout<<" |___________________________________________________________________________|"<<endl;
  144. cout<<endl;
  145. cout<<endl;
  146. cout<<" -----Press any key to return to main menu----- "; cout<<endl;
  147. int choice1, choice2;
  148. do
  149. {
  150.  
  151. cout<<" ********** Enter your choice *********** "<<endl;
  152. cout<<" *** 1. Login to Stock information *** "<<endl;
  153. cout<<" *** 2. Login to customer information *** "<<endl;
  154. cout<<" *** 3. To Exit *** "<<endl;
  155. cout<<" **************************************** "<<endl;
  156. cout<<"Enter your choice(1/2/3)"<<endl;
  157. cin>>choice1;
  158. switch(choice1)
  159. {
  160. case 1: {
  161.  
  162. do
  163. {
  164.  
  165. cout<<" **------------- Stock Database ---------**"<<endl;
  166. cout<<" **-------------- Enter Choice ----------** "<<endl;
  167. cout<<" ** 1.Enter the details ** "<<endl;
  168. cout<<" ** 2.Show record of Stock ** "<<endl;
  169. cout<<" ** 3.Search record in Stock ** "<<endl;
  170. cout<<" ** 4.Insert record in Stock ** "<<endl;
  171. cout<<" ** 5.Delete record from Stock ** "<<endl;
  172. cout<<" ** 6.Modify record from Stock ** "<<endl;
  173. cout<<" ** 7.To Exit ** "<<endl;
  174. cout<<" **--------------------------------------** "<<endl;
  175. cout<<"Enter your choice:";
  176. cin>>choice2;
  177.  
  178. switch(choice2)
  179. {
  180.  
  181. case 1:
  182. {char an='y';
  183. ofstream fout1("Stock", ios::out|ios::binary);
  184. while (an=='y')
  185. {
  186. s1.getdata_s();
  187. cin.get(ch);
  188. fout1.write((char*)&s1, sizeof(s1));
  189. cout<<"Do you want to add more"<<endl;
  190. cin>>an;
  191. }
  192.  
  193. fout1.close();
  194.  
  195. break;
  196. }
  197. case 2: { ifstream fin1("Stock", ios::in|ios::binary);
  198. if(!fin1){ cout<<"Cannot open file"<<endl;}
  199. cout<<"\t The details of Stock are:"<<endl;
  200. while(!fin1.eof())
  201. {
  202. fin1.read((char*)&s1, sizeof(s1));
  203. if(fin1.eof()) break;
  204. s1.display_s();
  205.  
  206.  
  207. }
  208. fin1.close();
  209. cout<<"Press any key to return to main menu"; cout<<endl;
  210. break;}
  211. /*case 3:
  212. ofstream fout2("Stock", ios::app|ios::binary);
  213. char more;
  214. cout<<"Enter new record in stock database (y/n):";
  215. cin>>more;
  216. while(more=='y')
  217. {
  218. s1.getdata_s();
  219.  
  220. fout2.write((char*)&s1, sizeof(s1));
  221. cout<<"Want to enter more:";
  222. cin>>more;
  223. }
  224. fout2.close();
  225. cout<<"Press any key to return to main menu"; getch();
  226. break; */
  227. case 3: { int choice3;
  228. do
  229. {
  230.  
  231. cout<<" **-------------Search By------------**"<<endl;
  232. cout<<" ** 1.Item name **"<<endl;
  233. cout<<" ** 2.Item code **"<<endl;
  234. cout<<" ** 3.Company name **"<<endl;
  235. cout<<" ** 4.To exit **"<<endl;
  236. cout<<" **************************************"<<endl;
  237. cout<<"Enter your choice(1/2/3/4):"<<endl;
  238. cin>>choice3;
  239. switch(choice3)
  240. {
  241.  
  242. case 1:{
  243. char name[80], found1='n';
  244. ifstream fin2("Stock", ios::in|ios::binary);
  245. cout<<"Enter the item name to search:"; cin.get(ch);
  246. cin.getline(name, 80);
  247. while(!fin2.eof())
  248. {
  249. fin2.read((char*)&s1, sizeof(s1));
  250. if(strcmpi(s1.getname(), name)==0)
  251. {
  252. s1.display_s();
  253. found1='y';
  254. break;
  255. }
  256. }
  257. if(found1=='n') cout<<"Record not found"<<endl;
  258. fin2.close();
  259. cout<<"Press any key to return to main menu"; getch();
  260. break;}
  261. case 2: { int code1, found2='n';
  262. ifstream fin3("Stock", ios::in|ios::binary);
  263. cout<<"Enter the item code to search:";
  264. cin>>code1;
  265. while(!fin3.eof())
  266. {
  267. fin3.read((char*)&s1, sizeof(s1));
  268. if(s1.getcode()==code1)
  269. {
  270. s1.display_s();
  271. found2='y';
  272. break;
  273.  
  274. }
  275. }
  276. if(found2=='n') cout<<"Record not found"<<endl;
  277. fin3.close();
  278. cout<<"Press any key to return to main menu"; cout<<endl;
  279. break;}
  280. case 3:{
  281. char company[80], found3='n';
  282. ifstream fin4("Stock", ios::in|ios::binary);
  283. cout<<"Enter company name to search:"; cin.get(ch);
  284. cin.getline(company, 80);
  285. while(!fin4.eof())
  286. {
  287. fin4.read((char*)&s1, sizeof(s1));
  288. if(fin4.eof()) break;
  289. if(strcmpi(s1.getcname(), company)==0)
  290. {
  291. s1.display_s();
  292. found3='y';
  293. }
  294. }
  295. if(found3=='n') cout<<"Record not found";
  296. fin4.close();
  297. cout<<"Press any key to return to main menu"; cout<<endl;
  298. break;}
  299. case 4:{
  300. break;}
  301. }
  302. }while(choice3!=4); cout<<"Press any key to return to main menu"; cout<<endl;
  303. break;}
  304. case 4:{
  305.  
  306. ofstream fout3("temp.dat", ios::out|ios::binary);
  307. ifstream fin5("Stock", ios::in|ios::binary);
  308. char last='y';
  309. cout<<"Enter the details whose record is to be inserted"<<endl;
  310. s1.getdata_s(); cin.get(ch);
  311. while(!fin5.eof())
  312. {
  313. fin5.read((char*)&s2, sizeof(s2));
  314. if(fin5.eof()) break;
  315. if(s1.getcode()<=s2.getcode())
  316. {
  317. fout3.write((char*)&s1, sizeof(s1));
  318. last='n';
  319. break;
  320. }
  321. else
  322. fout3.write((char*)&s2, sizeof(s2));
  323.  
  324. }
  325. if(last=='y')
  326. fout3.write((char*)&s1, sizeof(s1));
  327. else if(!fin5.eof())
  328. {
  329. while(!fin5.eof())
  330. {
  331. fin5.read((char*)&s2, sizeof(s2));
  332. fout3.write((char*)&s2, sizeof(s2));
  333. }
  334. }
  335. fin5.close();
  336. fout3.close();
  337. remove("Stock");
  338. rename("temp.dat", "Stock");
  339. cout<<"New data inserted properly"<<endl;
  340. cout<<"Press any key to return to main menu"; getch();
  341. break;}
  342.  
  343. case 5: {ifstream fin6("Stock", ios::in|ios::binary);
  344. ofstream fout4("temp.dat", ios::out|ios::binary);
  345. int code2; char f='f', confirm='y';
  346. cout<<"Enter item code of stock to be deleted:";
  347. cin>>code2;
  348. while(!fin6.eof())
  349. {
  350. fin6.read((char*)&s1, sizeof(s1));
  351. if(fin6.eof()) break;
  352. if(s1.getcode()==code2)
  353. {
  354. s1.display_s();
  355. f='t';
  356. cout<<"\nAre you sure, you want to delete:";
  357. cin>>confirm;
  358. if(confirm=='n') fout4.write((char*)&s1, sizeof(s1));
  359. }
  360. else
  361. fout4.write((char*)&s1, sizeof(s1));
  362. }
  363. if(f=='f')
  364. cout<<"Reocrd not found\n";
  365. fin6.close();
  366. fout4.close();
  367. remove("Stock");
  368. rename("temp.dat", "Stock");
  369. if(f=='t'&& confirm=='y')
  370. cout<<"Record deleted from the database\n";
  371. else if(f=='f'|| confirm=='n')
  372. cout<<"No record deleted from the database\n";
  373. cout<<"Press any key to return to main menu"; getch();
  374. break;}
  375. case 6: {fstream fio("Stock", ios::in|ios::out|ios::binary);
  376. long pos; char fod='f'; int code3;
  377. cout<<"Enter the item code of stock to be modified:";
  378. cin>>code3;
  379. while(!fio.eof())
  380. {
  381. pos=fio.tellg();
  382. fio.read((char*)&s1, sizeof(s1));
  383. if(fio.eof())
  384. break;
  385. if(s1.getcode()==code3)
  386. {
  387. s1.modify_s();
  388. fio.seekg(pos);
  389. fio.write((char*)&s1, sizeof(s1));
  390. fod='t';
  391. break;
  392. }
  393. }
  394. if(fod=='f') cout<<"Record not found\n";
  395. fio.close();
  396. if(fod=='t') cout<<"Record modified from database\n"<<endl;
  397. cout<<"Press any key to return to main menu"; getch();
  398. break;}
  399. case 7: break;
  400. }
  401.  
  402. }while(choice2!=7);
  403. cout<<"Press any key to return to main menu"; getch(); break;}
  404.  
  405.  
  406.  
  407.  
  408. case 2:
  409. {
  410. ofstream fileout1("Customer", ios::out|ios::binary);
  411. ifstream filein1("Customer", ios::in|ios::binary);
  412. ofstream fileout2("tempdata", ios::out|ios::binary);
  413. ifstream filein2("Stock", ios::in|ios::binary);
  414. char purchase;
  415.  
  416. s1.getdata_c();
  417. cin.get(ch);
  418. fileout1.write((char*)&s1, sizeof(s1));
  419.  
  420.  
  421.  
  422. cout<<"Do you want to purchase the items";
  423. cin>>purchase;
  424. if(purchase=='y')
  425. {
  426.  
  427.  
  428. filein1.read((char*)&s1, sizeof(s1));
  429. while(!filein2.eof())
  430. {
  431. if(filein2.eof()) break;
  432. filein2.read((char*)&s2, sizeof(s2));
  433. if(s1.getcodec()==s2.getcode())
  434. {
  435. s2.change(s1.getq_cus(),s2.getq_item());
  436. if(filein2.eof()) break;
  437. fileout2.write((char*)&s2, sizeof(s2));
  438.  
  439. }
  440.  
  441. else
  442. {
  443. if(filein2.eof()) break;
  444.  
  445. fileout2.write((char*)&s2, sizeof(s2));
  446.  
  447. }
  448. cout<<"Item purchased"<<endl;
  449. }
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456. fileout1.close();
  457. filein1.close();
  458. filein2.close();
  459. fileout2.close();
  460. remove("Stock");
  461. rename("tempdata","Stock");
  462.  
  463. }
  464. else
  465. cout<<"you don't want to purchase"<<endl;
  466.  
  467. cout<<"Press any key to return to main menu"<<endl; getch();
  468. break;}
  469.  
  470. }
  471. }while(choice1!=3);
  472. return 0;
  473. }

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.