Advertisement
AK-Zubayer

password

Mar 24th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4. /*Structure Start*/
  5. struct product
  6. {
  7. int price;
  8. char name[20]; //Product Name
  9. int number[50];//Product Number
  10. }product;
  11. /*Structure End*/
  12. void mainMenu();
  13. void empty_cart();
  14. int count_cart_item() ;
  15. /*Function For Admin*/
  16. void admin();
  17. void adm_add();
  18. void adm_delete();
  19. void adm_calculate();
  20. /*Function For User*/
  21. void user_main();
  22. void user_viewAll();
  23. void search_product();
  24. void user_order();
  25. void user_viewCart();
  26. int main()
  27. {
  28. mainMenu();
  29. }
  30. void mainMenu(){
  31. int ch;
  32. system("COLOR F1");
  33. do
  34. {
  35. printf("\n\t------------------------------------------------");
  36. printf("\n\t| AUTOMATED SHOPPING SYSTEM |");
  37. printf("\n\t------------------------------------------------");
  38. printf("\n\n");
  39. printf("\nPress <1> ADMIN");
  40. printf("\nPress <2> USER");
  41. printf("\nPress <0> EXIT");
  42. printf("\nEnter your Choice ::");
  43. scanf("%d",&ch);
  44. switch (ch)
  45. {
  46. case 1 : admin();
  47. break;
  48. case 2: user_main();
  49. break;
  50. case 0:exit(0);
  51. break;
  52. default:
  53. printf("Wrong choice !");
  54. break;
  55. }
  56. }
  57. while(ch!=-1);
  58. }
  59. void admin()
  60. {
  61. int ch;
  62. char adm_name[20]="admin";
  63. char adm_pass[20]="admin";
  64. char temp_name[20],temp_pass[20];
  65. system("COLOR F1");
  66. printf("\n\tAdmin Name:- ");
  67. scanf("%s",temp_name);
  68. printf("\n\tPassword:- ");
  69. scanf("%s",temp_pass);
  70. if(strcmp(adm_name,temp_name) == 0 && strcmp(adm_pass,temp_pass) == 0)
  71. {
  72. do
  73. {
  74. printf("\n\t------------------------------------------------");
  75. printf("\n\t| AUTOMATED SHOPPING SYSTEM |");
  76. printf("\n\t------------------------------------------------");
  77. printf("\n\n");
  78. printf("\nPress <1> Insert New Item ");
  79. printf("\nPress <2> Delete Item ");
  80. printf("\nPress <3> Checkout And Payment ");
  81. printf("\nPress <4> Main Menu");
  82. printf("\nPress <0> Exit ");
  83. printf("\nEnter your Choice ::");
  84. scanf("%d",&ch);
  85. switch (ch)
  86. {
  87. case 1: adm_add();
  88. break;
  89. case 2: adm_delete();
  90. break;
  91. case 3: adm_calculate();
  92. break;
  93. case 4: mainMenu();
  94. break;
  95. case 0: exit(0);
  96. break;
  97. default:
  98. printf("Wrong choice !");
  99. break;
  100. }
  101. }while(ch!=0);
  102. }
  103. else
  104. printf("Password and User name are not matched\n");
  105. }
  106. /*Start User Function*/
  107. void user_main()//main user side function
  108. {
  109. int ch,exsist_cart_product;
  110. char user_name[20]="user";
  111. char user_pass[20]="user";
  112. char temp_name[20],temp_pass[20];
  113. system("COLOR F1");
  114. printf("\n\tUsername:- ");
  115. scanf("%s",temp_name);
  116. printf("\n\tPassword:- ");
  117. scanf("%s",temp_pass);
  118. if(strcmp(user_name,temp_name) == 0 && strcmp(user_pass,temp_pass) == 0)
  119. {
  120. do
  121. {
  122. printf("\n\t------------------------------------------------");
  123. printf("\n\t| AUTOMATED SHOPPING SYSTEM |");
  124. printf("\n\t------------------------------------------------");
  125. printf("\n\n");
  126. exsist_cart_product = count_cart_product();
  127. printf("\nPress <1> View All Products");
  128. printf("\nPress <2> Search Needed Product");
  129. printf("\nPress <3> Order Product");
  130. printf("\nPress <4> View Your Cart (%d Items in cart)",exsist_cart_product);
  131. printf("\nPress <5> Empty cart ");
  132. printf("\nPress <6> Main Menu");
  133. printf("\nPress <0> Exit");
  134. printf("\nEnter your Choice ::");
  135. scanf("%d",&ch);
  136. switch (ch)
  137. {
  138. case 1: user_viewAll();
  139. break;
  140. case 2: search_product();
  141. break;
  142. case 3: user_order();
  143. break;
  144. case 4: user_viewCart();
  145. break;
  146. case 5: empty_cart();
  147. break;
  148. case 6: mainMenu();
  149. break;
  150. case 0: exit(0);
  151. break;
  152. default:
  153. printf("Wrong choice !");
  154. break;
  155. }
  156. }while(ch!=0);
  157. }
  158. else
  159. printf("Password and User name are not matched\n");
  160. }
  161. void adm_add()
  162. {
  163. struct product items;
  164. FILE *fp;
  165. fp=fopen("products.txt","a");
  166. printf("\n\tEnter Product Number ::");
  167. scanf("%s",product.number);
  168. setbuf(stdin,NULL);
  169. printf("\n\t Enter Product Name ::");
  170. scanf("%s",product.name);
  171. setbuf(stdin,NULL);
  172. printf("\n\t Enter Price of Product ::");
  173. scanf("%d",&product.price);
  174. if(fp == NULL)
  175. {
  176. printf("\n Error In Opening File....");
  177. }
  178. else
  179. {
  180. fprintf(fp,"\n%s %s %d \n",product.number,product.name,product.price);
  181. printf("Record Inserted ! ...... \n");
  182. fclose(fp);
  183. }
  184. }
  185. void adm_delete()
  186. {
  187. FILE *fpt,*fptr;
  188. struct product item;
  189. int found=0;
  190. char name[20];
  191. fpt=fopen("products.txt","r");
  192. if(fpt==NULL)
  193. {
  194. printf("\n Error In Opening File....");
  195. }
  196. fptr=fopen("temp.txt","w");
  197. if(fptr==NULL)
  198. {
  199. printf("\n Error In Opening File....");
  200. }
  201. setbuf(stdin,NULL);
  202. printf("\n Enter Name In Uppercase : ");
  203. scanf("%s",name);
  204. while(fscanf(fpt,"%s %s %d\n",product.number,product.name,&product.price)!=EOF)
  205. {
  206. if(product.name==0)
  207. break;
  208. if(strcmp(product.name,name)==0)
  209. {
  210. found=1;
  211. }
  212. else
  213. {
  214. fprintf(fptr,"%s ",product.number);
  215. fprintf(fptr,"%s ",product.name);
  216. fprintf(fptr,"%d ",product.price);
  217. }
  218. }
  219. fclose(fptr);
  220. fclose(fpt);
  221. if(found==0)
  222. {
  223. printf("\n\n\n *****************************************\n\n");
  224. printf("\n Record Not Found....");
  225. printf("\n\n\n *****************************************\n\n");
  226. }
  227. else
  228. {
  229. fpt=fopen("products.txt","w");
  230. if(fpt==NULL)
  231. {
  232. printf("\n Error In Opening File....");
  233. }
  234. fptr=fopen("temp.txt","r");
  235. if(fptr==NULL)
  236. {
  237. printf("\n Error In Opening File....");
  238. }
  239. while(fscanf(fptr,"%s %s %d \n",product.number,product.name,&product.price)!=EOF)
  240. {
  241. if(product.name==0)
  242. break;
  243. fprintf(fpt,"%s %s %d \n",product.number,product.name,product.price);
  244. }
  245. printf("\n Record Successfully Deleted...............................!\n");
  246. fclose(fptr);
  247. fclose(fpt);
  248. }
  249. }
  250. void adm_calculate()
  251. {
  252. FILE *cart;
  253. int amount = 0,x,y;
  254. cart = fopen("user_cart.txt","r");
  255. if(cart == NULL)
  256. {
  257. printf("file does not found !");
  258. exit(1);
  259. }
  260. else
  261. {
  262. printf("NUMBER \tNAME \tPRICE\n");
  263. while( fscanf(cart,"%s %s %d \n",product.number,product.name,&product.price) != EOF )
  264. {
  265. printf("%s\t %s\t %d \n",product.number,product.name,product.price);
  266. amount=amount + product.price;
  267. }
  268. printf("\n\t Total Amount :: %d /-TK\n",amount);
  269. printf("Enter Received Amount From Customer: ");
  270. scanf("%d",&x);
  271. printf("Your Advanced Amount Is: %d/-TK\n\n",x-amount);
  272. }
  273. printf("\nTHANKS FOR VISITING US!!\n");
  274. return 0;
  275. }
  276. /*End Admin Function*/
  277.  
  278. void user_viewAll()
  279. {
  280. char ch;
  281. FILE *fp;
  282. fp = fopen("products.txt","r");
  283. if(fp == NULL)
  284. {
  285. printf("file does not found !");
  286. exit(1);
  287. }
  288. else
  289. {
  290. printf("Number \tName \tPrice\n");
  291. while( fscanf(fp,"%s %s %d\n",product.number,product.name,&product.price) != EOF )
  292. {
  293. printf("%s \t%s \t%d \n",product.number,product.name,product.price);
  294. }
  295. }
  296. }
  297.  
  298. /*START adding selected item to the user cart */
  299. void user_order()
  300. {
  301. struct product item;
  302. FILE *fp;
  303. FILE *cart;//user cart file
  304. int flag=0,chup;
  305. char ch[20];
  306. fp = fopen("products.txt","r");
  307. printf("*** -------------------------------------------- ***\n");
  308. printf("NUMBER \tNAME \tPRICE\n");
  309. while( fscanf(fp,"%s %s %d\n",product.number,product.name,&product.price) != EOF )
  310. printf("%s \t%s \t%d \n",product.number,product.name,product.price);
  311. printf("*** -------------------------------------------- ***");
  312. printf("\n\t Enter Product Name ::");
  313. scanf("%s",ch);
  314. chup=strupr(ch);
  315. fp = fopen("products.txt","r");
  316. cart = fopen("user_cart.txt","a");
  317. if(fp == NULL)
  318. {
  319. printf("file does not found !");
  320. exit(1);
  321. }
  322. else
  323. {
  324. while(fscanf(fp,"%s %s %d\n",product.number,product.name,&product.price) != EOF)
  325. {
  326. if(strcmp(product.name,chup) == 0)
  327. { flag=1;
  328. printf("\n \n");
  329. printf("\n\t\tNumber ::%s",product.number);
  330. printf("\n\t\tProduct Name ::%s",product.name);
  331. printf("\n\t\tCost of Product ::%d",product.price);
  332. fprintf(cart,"%s %s %d \n",product.number,product.name,product.price);
  333. printf("\n\n\tItem Added To cart! \n");
  334. }
  335. }
  336. if(flag == 0)
  337. printf("\nRecord Not Found\n");
  338. }
  339. fclose(cart);
  340. fclose(fp);
  341. }
  342. /*Code For Search Needed Product*/
  343. void search_product() //Search Using Product Name
  344. {
  345. struct product items;
  346. FILE *fp;
  347. FILE *cart;
  348. int flag=0,findproduct2,name2;
  349. char findproduct[20],ans,name[20];
  350. printf("Enter Product Name ::");
  351. scanf("%s",findproduct);
  352. findproduct2=strupr(findproduct);
  353. fp = fopen("products.txt","r");
  354. cart = fopen("user_cart.txt","a");
  355. if(fp == NULL)
  356. {
  357. printf("file does not found !");
  358. exit(1);
  359. }
  360. else
  361. {
  362. while(fscanf(fp,"%s %s %d \n",product.number,product.name,&product.price) != EOF)
  363. {
  364. if(strcmp(product.name,findproduct2) == 0)
  365. { flag=1;
  366. printf("\n\t\tNumber ::%s",product.number);
  367. printf("\n\t\tProduct Name ::%s",product.name);
  368. printf("\n\t\tCost of Product ::%d",product.price);
  369. }
  370. }
  371. if(flag == 0)
  372. printf("\nRecord Not Found\n");
  373. }
  374. fclose(cart);
  375. fclose(fp);
  376. }
  377. int count_cart_product()
  378. {
  379. char ch;
  380. FILE *cart;
  381. int i,count = 0;
  382. cart = fopen("user_cart.txt ","r");
  383. if(cart == NULL)
  384. {
  385. printf("file does not found !");
  386. exit(1);
  387. }
  388. else
  389. {
  390. while( fscanf(cart,"%s %s %d \n",product.number,product.name,&product.price) != EOF )
  391. {
  392. count=count + 1;
  393. }
  394. return count;
  395. }
  396. fclose(cart);
  397. }
  398. void user_viewCart()
  399. {
  400. char ch;
  401. FILE *cart;
  402. int i,amount = 0;
  403. cart = fopen("user_cart.txt","r");
  404. if(cart == NULL)
  405. {
  406. printf("file does not found !");
  407. exit(1);
  408. }
  409. else
  410. {
  411. while( fscanf(cart,"%s %s %d \n",product.number,product.name,&product.price) != EOF )
  412. {
  413. printf(" %s %s %d \n",product.number,product.name,product.price);
  414. amount=amount + product.price;
  415. }
  416. printf("\n\t Total Amount :: %d /-TK",amount);
  417. }
  418. }
  419. void empty_cart()
  420. {
  421. FILE *filter;
  422. filter = fopen("user_cart.txt","w");
  423. char name[20],number[20];
  424. int price;
  425. if(filter == NULL)
  426. {
  427. printf("file does not found !");
  428. exit(1);
  429. }
  430. else
  431. {
  432. while( fscanf(filter,"%s %s %s %d %s \n",product.number,product.name,&product.price) != EOF )
  433. {
  434. fprintf(filter,"%s %s %d\n",number,name,price);
  435. }
  436. }
  437. fclose(filter);
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement