Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.57 KB | None | 0 0
  1. /**********************************************************************************************************************
  2. This is to certify that this project is my own work, based on my personal efforts in studying and applying the concepts
  3. learned. I have constructed the functions and their respective algorithms and corresponding code by myself. The program
  4. was run, tested, and debugged by my own efforts. I further certify that I have not copied in part or whole or otherwise
  5. plagiarized the work of other students and/or persons.
  6. Fritz Edron M.Calimag, 11621206
  7. **********************************************************************************************************************/
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #define MAX_CART 100
  13.  
  14. typedef char string15[16];
  15. typedef char string20[21];
  16. typedef char string50[51];
  17. typedef char string8[9];
  18. typedef struct{
  19.  
  20. string20 first,
  21. middle,
  22. last;
  23.  
  24. }nameType;
  25. typedef struct{
  26.  
  27. nameType name;
  28. string50 address;
  29.  
  30. }userInfoType;
  31. typedef struct{
  32.  
  33. string8 code;
  34. int qty;
  35.  
  36. }prodBoughtType;
  37. typedef prodBoughtType arrBought[MAX_CART];
  38. struct userTag{
  39.  
  40. string15 username,
  41. password;
  42.  
  43. userInfoType info;
  44.  
  45. char type;
  46. float creditlimit,
  47. outstanding;
  48.  
  49. arrBought cart;
  50.  
  51. int items,
  52. nlogin,
  53. nlock;
  54.  
  55. struct userTag *pNext;
  56.  
  57. };
  58.  
  59. typedef struct userTag userType;
  60.  
  61. typedef struct userTag * ptrUser;
  62.  
  63. struct stockcateg
  64. {
  65. string15 category;
  66. struct stockinfo{
  67. string15 supplier,
  68. product;
  69. string8 pCode;
  70. int Qavail,
  71. Qsold;
  72. float purchasePrice,
  73. sellingPrice,
  74. discountRate;
  75. struct stockinfo *pNextstockinfo;
  76. };
  77. struct stockcateg *pNextstockcateg;
  78. };
  79.  
  80. typedef struct stockcateg stockcategtype;
  81.  
  82. typedef struct stockcateg * ptrStockcateg;
  83.  
  84. typedef struct stockinfo stocktype;
  85.  
  86. typedef struct stockinfo * ptrStock;
  87.  
  88. //Function Name: displayAll
  89. //Function Required Input Parameters: pUsers
  90. //Function Expected Return Data: N/A
  91. //Function Description and or Algorithim: This function displays all the users
  92.  
  93. void displayAll(ptrUser pUsers){
  94.  
  95. while (pUsers != NULL){
  96. printf("%s\n", pUsers -> username);
  97. pUsers = pUsers -> pNext;
  98. }
  99.  
  100. }
  101.  
  102. //Function Name: freeAll
  103. //Function Required Input Parameters: *pFirst
  104. //Function Expected Return Data: N/A
  105. //Function Description and or Algorithim: This function frees all the Users
  106.  
  107. void freeAll(ptrUser * pFirst){
  108.  
  109. ptrUser pDel;
  110.  
  111. while (*pFirst != NULL){
  112. pDel = *pFirst;
  113. *pFirst = (*pFirst) -> pNext;
  114. free(pDel);
  115. }
  116. }
  117.  
  118. //Function Name: getUserInfo
  119. //Function Required Input Parameters: *pInfo
  120. //Function Expected Return Data: N/A
  121. //Function Description and or Algorithim: This function gets the users information (Full name & address)
  122.  
  123. void getUserInfo(userInfoType *pInfo){
  124.  
  125. printf("Enter Name \n");
  126. printf("First Name: "); fgets(pInfo -> name.first, 21, stdin);
  127. printf("Middle Name: "); fgets(pInfo -> name.middle, 21, stdin);
  128. printf("Last Name: "); fgets(pInfo -> name.last, 21, stdin);
  129.  
  130. printf("%s,%s%s \n", pInfo -> name.last, pInfo -> name.first, pInfo -> name.middle);
  131.  
  132.  
  133. printf("Enter Address: "); fgets(pInfo -> address, 51, stdin);
  134.  
  135. pInfo -> address[strlen(pInfo -> address) - 1] = '\0';
  136. pInfo -> name.first[strlen(pInfo -> name.first) - 1] = '\0';
  137. pInfo -> name.middle[strlen(pInfo -> name.middle) - 1] = '\0';
  138. pInfo -> name.last[strlen(pInfo -> name.last) - 1] = '\0';
  139.  
  140. }
  141.  
  142. //Function Name: signUp
  143. //Function Required Input Parameters: *pUser, *pUsers
  144. //Function Expected Return Data: N/A
  145. //Function Description and or Algorithim: This function allows the user to sign up
  146.  
  147. void signUp(userType *pUser, userType *pUsers){
  148. string8 tempcode;
  149. char cDump;
  150. char acode[9]={"DLSU2017"};
  151.  
  152. do{
  153.  
  154. printf("Enter username: "); scanf("%s%c", (*pUser).username, &cDump);
  155. while(pUsers != NULL)
  156. if(strcmp((*pUser).username, pUsers->username)==0)
  157. {
  158. pUsers = pUsers->pNext;
  159. printf("Username Taken try again\n");
  160. printf("Enter username: "); scanf("%s%c", (*pUser).username, &cDump);
  161. }
  162. }while (strlen((*pUser).username) < 3 || strlen((*pUser).username) > 15);
  163.  
  164. do{
  165.  
  166. printf("Enter password: "); scanf("%s%c", pUser -> password, &cDump);
  167.  
  168. }while (strlen(pUser -> password) < 6 || strlen(pUser -> password) > 15);
  169.  
  170. do{
  171.  
  172. printf("Type A for Admin and S for Shopper: "); scanf("%c%c", &pUser->type, &cDump);
  173. if(pUser->type == 'A' || pUser->type == 'a'){
  174. printf("Enter authorization code: "); scanf("%s%c",&tempcode,&cDump);
  175. while(strcmp(tempcode, acode)!=0)
  176. printf(" Wrong Authorization Code try again");
  177. }
  178. else if(pUser->type == 'S' || pUser->type == 's'){
  179. printf("Congratulations you are now registered as a shopper!\n");
  180. pUsers->creditlimit = 5000.00;
  181. pUsers->outstanding = 0.00;
  182. }
  183. else
  184. printf("Invalid Input please try again:");
  185.  
  186. }while (pUser->type != 'A' && pUser->type != 'a' && pUser->type != 'S' && pUser->type != 's');
  187.  
  188. getUserInfo(&pUser -> info);
  189.  
  190. }
  191.  
  192. //Function Name: displayAllRecur
  193. //Function Required Input Parameters: pUsers
  194. //Function Expected Return Data: N/A
  195. //Function Description and or Algorithim: This Function display all the users
  196.  
  197. void displayAllRecur(ptrUser pUsers){
  198.  
  199. if (pUsers != NULL){
  200. printf("%s\n", pUsers -> username);
  201. displayAllRecur(pUsers -> pNext);
  202. }
  203. }
  204.  
  205. ptrUser search(ptrUser pFirst, string15 username){
  206.  
  207. ptrUser pRun;
  208. pRun = pFirst;
  209.  
  210. while (pRun != NULL && strcmp(username, pRun -> username) != 0){
  211.  
  212. pRun = pRun -> pNext;
  213.  
  214. }
  215.  
  216. return pRun;
  217.  
  218. }
  219. //Function Name: deleteNode
  220. //Function Required Input Parameters: *pFrist, username
  221. //Function Expected Return Data: N/A
  222. //Function Description and or Algorithim: This function deletes unwanted nodes
  223.  
  224. void deleteNode(ptrUser *pFirst, string15 username){
  225.  
  226. ptrUser pFind, pRun;
  227.  
  228. if (*pFirst == NULL)
  229. printf("The List is empty");
  230.  
  231. else {
  232.  
  233. pFind = search(*pFirst, username);
  234. if (pFind == NULL)
  235.  
  236. printf("%s is not in the List\n", username);
  237.  
  238. else { //found the node to be deleted
  239.  
  240. if(pFind == *pFirst){ //deleting first node
  241.  
  242. *pFirst = (*pFirst) -> pNext;
  243.  
  244. }
  245.  
  246. else{ //delete from middle or end
  247.  
  248. pRun = *pFirst;
  249. while (pRun -> pNext == pFind)
  250. pRun = pRun -> pNext;
  251.  
  252. pRun -> pNext = pFind -> pNext;
  253.  
  254. }
  255.  
  256. free(pFind);
  257. pFind = NULL;
  258.  
  259. }
  260.  
  261. }
  262.  
  263. }
  264.  
  265. //Function Name: login
  266. //Function Required Input Parameters: *pUsers
  267. //Function Expected Return Data: integer value
  268. //Function Description and or Algorithim: This functions allows the user to log in and locks the users account if he exceeds the counter(3)
  269.  
  270. int login(userType *pUsers){
  271. int ctr=0;
  272. string15 userchk, passchk;
  273.  
  274. printf("Enter Username:");scanf("%s",&userchk);
  275. printf("Enter Password:");scanf("%s",&passchk);
  276. while(pUsers != NULL){
  277. do{
  278. pUsers = pUsers->pNext;
  279. printf("Username Invalid try again\n");
  280. }while(strcmp(pUsers -> username, userchk)!=0);
  281. do{
  282. ctr++;
  283. pUsers = pUsers->pNext;
  284. if(ctr==3){
  285. printf("counter exceeded account locked... Exiting program");
  286. pUsers->nlock = 1;
  287. return 0;
  288. }
  289.  
  290. printf("Incorrect Password try again\n");
  291. printf("Enter Password:");scanf("%s",&passchk);
  292.  
  293. }while(strcmp(pUsers -> username, userchk)==0 && strcmp(pUsers -> password, passchk)!=0);
  294.  
  295. if(strcmp(pUsers -> username, userchk)==0 && strcmp(pUsers -> password, passchk)==0)
  296. {
  297. if(pUsers->type=='A' || pUsers->type == 'a')
  298. {
  299. printf("going to admin menu\n");
  300. return 2;
  301. }
  302. else if(pUsers->type=='S' || pUsers->type == 's'){
  303. printf("welcome to the shopper menu\n");
  304. return 1;
  305. }
  306. }
  307. }
  308. }
  309.  
  310. //Function Name: manageaccs
  311. //Function Required Input Parameters: *pUsers
  312. //Function Expected Return Data: N/A
  313. //Function Description and or Algorithim: to give the user access to Manage Accounts menu
  314.  
  315. void manageaccs(userType *pUsers){
  316. int g;
  317. string15 userchk;
  318.  
  319. printf("Welcome to Manage Accounts Menu");
  320.  
  321. do{
  322. printf("Type 1 to View Locked Accounts\nType 2 to Unlock Specific Account\nType 3 To Unlock All Locked Accounts\nType 4 to View Accounts with Outstanding Balance\nType 5 to Return to Administrator Menu\n");
  323. scanf("%d",&g);
  324.  
  325. switch(g){
  326. case 1:printf("Here are all the locked accounts\n");
  327. do{
  328. if(pUsers->nlock==1)
  329. printf("%s\n",pUsers->username);
  330. pUsers = pUsers->pNext;
  331. }while(pUsers!=NULL);break;
  332.  
  333. case 2:printf("Type the username of the account you wish to unlock:\n");scanf("%s", &userchk);
  334. do{
  335. pUsers = pUsers->pNext;
  336. if(strcmp(pUsers -> username, userchk)==0)
  337. pUsers->nlock = 0;
  338. printf("User unlocked");
  339. }while(pUsers!=NULL);break;
  340.  
  341. case 3:printf("Unlocking All Locked Accounts");
  342. do{
  343. pUsers = pUsers->pNext;
  344. pUsers->nlock = 0;
  345. }while(pUsers!=NULL && pUsers->nlock==1);break;
  346. case 4:printf("Displaying All Accounts with Outstanding Balance");
  347. do{
  348. pUsers = pUsers->pNext;
  349. if(pUsers->outstanding<0)
  350. printf("%s", pUsers->username);
  351. }while(pUsers!=NULL);break;
  352. case 5:printf("Returning to Admin Menu");break;
  353. }
  354. }while(g!=5);
  355. }
  356.  
  357. //Function Name: addstock
  358. //Function Required Input Parameters: *pNewStock, *pStock, *pStockcateg, *pLastStock, *pNewStcateg, *pLastStcateg
  359. //Function Expected Return Data: N/A
  360. //Function Description and or Algorithim: This Function adds new stocks as long as you dont end the loop
  361.  
  362. void addstock(stocktype *pNewStock, stocktype *pStock, stockcategtype *pStockcateg, stocktype *pLastStock, stockcategtype *pNewStcateg, stockcategtype *pLastStcateg){
  363. int n,nqa,nqs;
  364. float fpp,fusp,fdr;
  365. char cDump;
  366.  
  367. do{//add stockcategtypes later to account for unique categories and str pNewStock for others
  368. printf("Enter Category:");fgets(pStockcateg->category, 16, stdin);
  369.  
  370. printf("You entered %s Category\n",pStockcateg->category);
  371.  
  372. printf("Input the name of the supplier:");scanf("%s%c",pNewStock->supplier,&cDump);
  373.  
  374. printf("Input name of product:");scanf("%s%c",pNewStock->product,&cDump);
  375.  
  376. printf("Enter Available Product Quantity: "); scanf("%d%c", &nqa, &cDump);pNewStock->Qavail = nqa;
  377.  
  378. printf("Input Price of the Product: ");scanf("%f%c", &fpp, &cDump);pNewStock->purchasePrice = fpp;
  379.  
  380. printf("Input the Unit Selling Price of the product: ");scanf("%f%c", &fusp, &cDump);pNewStock->sellingPrice = fusp;
  381.  
  382. printf("Input the Discount Rate of the product: ");scanf("%f%c", &fdr, &cDump);pNewStock->discountRate = fdr;
  383.  
  384. pNewStock->Qsold = 0;
  385.  
  386. printf("type 1 to add new stock\n0 to return to Manage Stocks Menu:");scanf("%d",&n);
  387. if(n!=1 || n!=0 )
  388. printf("Invalid Input... Returning you to Manage Stocks Menu");
  389.  
  390. }while(n!=0);
  391. }
  392.  
  393. //Function Name: managestock
  394. //Function Required Input Parameters: *pStock, *pNewStock, *pStockcateg, *pLastStock, *pNewStcateg, *pLastStcateg
  395. //Function Expected Return Data: N/A
  396. //Function Description and or Algorithim: To give the user access to Manage Stocks Menu
  397.  
  398. void managestock(stocktype *pStock, stocktype *pNewStock, stockcategtype *pStockcateg, stocktype *pLastStock, stockcategtype *pNewStcateg, stockcategtype *pLastStcateg){
  399. int g,a;
  400.  
  401. printf("Welcome to Manage Stocks Menu");
  402.  
  403. do{
  404. printf("Type 1 to Add new stock\nType 2 to View All Stocks\nType 3 To View stocks by category\nType 4 to View stocks to reorder\nType 5 to Modify Stock Info\n Type 6 to Restock\nType 7 to Save Inventory\n Type 8 to Update Inventory from file\nType 9 to return to Admin Menu:\n");
  405. scanf("%d",&g);
  406.  
  407. switch(g){
  408. case 1:printf("Adding New Stock");
  409. do{
  410. pNewStock = malloc(sizeof(stocktype));//Based almost everything from signup
  411.  
  412. addstock(pNewStock,pStock,pStockcateg,pLastStock,pNewStcateg,pLastStcateg);
  413. pNewStock->pNextstockinfo=NULL;
  414. pNewStcateg->pNextstockcateg=NULL;
  415. if(pStock==NULL && pStockcateg){
  416. pStock=pNewStock;
  417. pStockcateg=pNewStcateg;
  418. }
  419. else
  420. pLastStock->pNextstockinfo = pNewStock;
  421. pLastStcateg->pNextstockcateg = pNewStcateg;
  422. pLastStock = pNewStock;
  423. pLastStcateg = pNewStcateg;
  424. }while(a!=0);break;
  425. case 2:printf("Viewing All Stocks");break;
  426. case 3:printf("Viewing Stocks by Category");break;
  427. case 4:printf("View Stocks to reorder");break;
  428. case 5:printf("Modifying Stock Info");break;
  429. case 6:printf("Restocking");break;
  430. case 7:printf("Saving Inventory");break;
  431. case 8:printf("Updating Inventory from file");break;
  432. case 9:printf("Returning to Admin menu");break;
  433. }
  434. }while(g!=9);
  435. }
  436.  
  437. //Function Name: AdminMenu
  438. //Function Required Input Parameters: *pUsers, *pInfo, *pNew, *pLast, *end, *pStock, *pNewStock, *pStcateg, *pLastStock, *pNewStcateg, *pLastStcateg
  439. //Function Expected Return Data: N/A
  440. //Function Description and or Algorithim: To give the user access to admin menu
  441.  
  442. void AdminMenu (userType *pUsers, userInfoType *pInfo, userType *pNew, userType *pLast, int *end, stocktype *pStock, stocktype *pNewStock, stockcategtype *pStcateg, int *n, stocktype *pLastStock, stockcategtype *pNewStcateg, stockcategtype *pLastStcateg){
  443. int i;
  444.  
  445. printf("Welcome to admin menu\n");
  446.  
  447. do{
  448. printf("Type 1 to go to Manage Accounts Menu\nType 2 to go to Manage Stocks Menu\nType 3 To Prepare Delivery Receipt\nType 4 to Shutdown Kiosk\nType 5 to Log out");
  449. scanf("%d",&i);
  450.  
  451. switch(i){
  452. case 1:printf("Going to Manage Accounts Menu please wait\n");
  453. manageaccs(pUsers);break;
  454. case 2:printf("Going to Manage Stocks Menu please wait\n");
  455. managestock(pStock, pNewStock, pStcateg, pLastStock,pNewStcateg,pLastStcateg);break;
  456. case 3:printf("Preparing Delivery Receipt Please Wait\n")/*phase 2*/;break;
  457. case 4:printf("Shutting Down\n");*end=1;break;//passes 1 to end so that it'll terminate the program
  458. case 5:printf("Logging Out Goodbye\n");*n=1;break;//assign 1 to n so that it'll return to sign up
  459. }
  460. }while(i!=4 && i!=5);
  461.  
  462. }
  463.  
  464. //Function Name: ModifyInfo
  465. //Function Required Input Parameters: *pInfo
  466. //Function Expected Return Data: N/A
  467. //Function Description and or Algorithim: To let the user modify his information
  468.  
  469. void ModifyInfo(userInfoType *pInfo){
  470.  
  471. printf("First Name: "); fgets(pInfo -> name.first, 21, stdin);
  472. printf("Middle Name: "); fgets(pInfo -> name.middle, 21, stdin);
  473. printf("Last Name: "); fgets(pInfo -> name.last, 21, stdin);
  474.  
  475. printf("Enter Address: "); fgets(pInfo -> address, 51, stdin);
  476.  
  477. printf("Your new name 'Last Name,First Name, Middle Name\n'%s,%s%s \n", pInfo -> name.last, pInfo -> name.first, pInfo -> name.middle);
  478. printf("Your new Address\n%s", pInfo->address);
  479.  
  480. pInfo -> address[strlen(pInfo -> address) - 1] = '\0';
  481. pInfo -> name.first[strlen(pInfo -> name.first) - 1] = '\0';
  482. pInfo -> name.middle[strlen(pInfo -> name.middle) - 1] = '\0';
  483. pInfo -> name.last[strlen(pInfo -> name.last) - 1] = '\0';
  484. }
  485.  
  486. //Function Name: ShopperMenu
  487. //Function Required Input Parameters: *pUsers, *pInfo, *n
  488. //Function Expected Return Data: N/A
  489. //Function Description and or Algorithim: To access shopper menu
  490.  
  491. void ShopperMenu(userType *pUsers, userInfoType *pInfo, int *n){
  492. int g;
  493.  
  494. printf("Welcome to shopper menu\n");
  495.  
  496. printf("Type 1 to Modify User Info\nType 2 to Browse All Products\nType 3 To Browse Products by category\nType 4 to Browse Products on Sale\nType 5 to Add to Cart\n Type 6 to View Cart\nType 7 to Settle OutStanding Balance\n Type 8 to Log Out:");
  497. scanf("%d",&g);
  498.  
  499. switch(g){
  500. case 1:printf("Modify your info\n");
  501. ModifyInfo(&pUsers->info);break;
  502. case 2:printf("Browsing All Products");break;
  503. case 3:printf("Browse Products by Category");break;
  504. case 4:printf("Browse Products on Sale");break;
  505. case 5:printf("Adding to cart");break;
  506. case 6:printf("Viewing Cart");break;
  507. case 7:printf("Settling Outstanding Balance");break;
  508. case 8:printf("Logging Out Goodbye\n");*n=1 ;break;
  509. }
  510. }
  511.  
  512. int main(){
  513.  
  514. /*
  515. userType user;
  516. userType *pUsers = NULL,
  517. *pNew;
  518. */
  519. ptrUser pUsers = NULL,
  520. pNew, pLast;
  521.  
  522. ptrStock pStock = NULL,
  523. pNewStock,pLastStock;
  524.  
  525. ptrStockcateg pStcateg = NULL,
  526. pNewStcateg,pLastStcateg;
  527.  
  528. int end=1,n;
  529. char cDump;
  530. ptrUser pRun, pTrail;
  531.  
  532. do{
  533. printf("Welcome to the shopping kiosk\n\n");
  534. printf("type 2 if you would like to log in\n1 if you would like to sign up\n0 if you would like to exit the program: ");
  535. scanf("%d",&n);
  536.  
  537. switch(n){
  538.  
  539. case 2:if(login(pUsers)==2)
  540. AdminMenu(pUsers, &pUsers->info, pNew, pLast, &end, pStock, pNewStock, pStcateg, &n, pLastStock, pNewStcateg, pLastStcateg);
  541. else if(login(pUsers)==1)
  542. ShopperMenu(pUsers, &pUsers->info, &n);
  543. else if(login(pUsers)==0)
  544. end=0;break;
  545.  
  546.  
  547. default:
  548. /*
  549. pNew = malloc(sizeof(userType));
  550.  
  551. signUp(pNew);
  552. pNew -> pNext = NULL;
  553.  
  554. if (pUsers == NULL)
  555. pUsers = pNew;
  556.  
  557. else
  558. pLast -> pNext = pNew;
  559.  
  560. pLast = pNew;
  561. */
  562.  
  563. pNew = malloc(sizeof(userType));
  564.  
  565. signUp(pNew,pUsers);
  566. pNew -> pNext = NULL;
  567.  
  568. if (pUsers == NULL) //if list is empty
  569. pUsers = pNew;
  570.  
  571. else if (strcmp(pUsers -> username, pNew -> username) > 0){ //conect as first node
  572.  
  573. pNew -> pNext = pUsers;
  574. pUsers = pNew;
  575.  
  576. }
  577.  
  578. else { //modifying middle of list
  579.  
  580. pRun = pUsers;
  581. while (pRun != NULL && strcmp(pRun -> username, pNew -> username) < 0){
  582.  
  583. pTrail = pRun;
  584. pRun = pRun -> pNext;
  585.  
  586. }
  587.  
  588. pTrail -> pNext = pNew;
  589. pNew -> pNext = pRun;
  590.  
  591. }
  592. printf("Type '1' to Return to Log in/Sign up menu or '0' To exit the program. "); scanf("%d%c", &end, &cDump);
  593.  
  594. break;
  595. //use for view locked accounts displayAllRecur(pUsers);
  596. //displayAll(pUsers);
  597.  
  598. }
  599. } while (end != 0);
  600. freeAll(&pUsers);
  601.  
  602. return 0;
  603. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement