Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.37 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. void displayAll(ptrUser pUsers){
  89.  
  90. while (pUsers != NULL){
  91. printf("%s\n", pUsers -> username);
  92. pUsers = pUsers -> pNext;
  93. }
  94.  
  95. }
  96.  
  97. void freeAll(ptrUser * pFirst){
  98.  
  99. ptrUser pDel;
  100.  
  101. while (*pFirst != NULL){
  102. pDel = *pFirst;
  103. *pFirst = (*pFirst) -> pNext;
  104. free(pDel);
  105. }
  106. }
  107.  
  108. void freeAllstockver(ptrStock * pFirststock){
  109.  
  110. ptrStock pDelete;
  111.  
  112. while (*pFirststock != NULL){
  113. pDelete = *pFirststock;
  114. *pFirststock = (*pFirststock) -> pNextstockinfo;
  115. free(pDelete);
  116. }
  117. }
  118.  
  119. void getUserInfo(userInfoType *pInfo){
  120.  
  121. printf("Enter Name \n");
  122. printf("First Name: "); fgets(pInfo -> name.first, 21, stdin);
  123. printf("Middle Name: "); fgets(pInfo -> name.middle, 21, stdin);
  124. printf("Last Name: "); fgets(pInfo -> name.last, 21, stdin);
  125.  
  126. printf("%s,%s%s \n", pInfo -> name.last, pInfo -> name.first, pInfo -> name.middle);
  127.  
  128.  
  129. printf("Enter Address: "); fgets(pInfo -> address, 51, stdin);
  130.  
  131. pInfo -> address[strlen(pInfo -> address) - 1] = '\0';
  132. pInfo -> name.first[strlen(pInfo -> name.first) - 1] = '\0';
  133. pInfo -> name.middle[strlen(pInfo -> name.middle) - 1] = '\0';
  134. pInfo -> name.last[strlen(pInfo -> name.last) - 1] = '\0';
  135.  
  136. }
  137.  
  138. void signUp(userType *pUser, userType *pUsers){
  139. string8 tempcode;
  140. char cDump;
  141. char acode[9]="DLSU2017";
  142.  
  143. do{
  144.  
  145. printf("Enter username: "); scanf("%s%c", (*pUser).username, &cDump);
  146. while(pUsers != NULL)
  147. if(strcmp((*pUser).username, pUsers->username)==0)
  148. {
  149. pUsers = pUsers->pNext;
  150. printf("Username Taken try again\n");
  151. printf("Enter username: "); scanf("%s%c", (*pUser).username, &cDump);
  152. }
  153. }while (strlen((*pUser).username) < 3 || strlen((*pUser).username) > 15);
  154.  
  155. do{
  156.  
  157. printf("Enter password: "); scanf("%s%c", pUser -> password, &cDump);
  158.  
  159. }while (strlen(pUser -> password) < 6 || strlen(pUser -> password) > 15);
  160.  
  161. do{
  162.  
  163. printf("Type A for Admin and S for Shopper: "); scanf("%c%c", &pUser->type, &cDump);
  164. if(pUser->type == 'A' || pUser->type == 'a'){
  165. printf("Enter authorization code: "); scanf("%s%c",tempcode,&cDump);
  166. while(strcmp(tempcode, acode)!=0)
  167. printf(" Wrong Authorization Code try again");
  168. printf("Enter authorization code: "); scanf("%s%c",tempcode,&cDump);
  169. }
  170. else if(pUser->type == 'S' || pUser->type == 's'){
  171. printf("Congratulations you are now registered as a shopper!\n");
  172. pUsers->creditlimit = 5000.00;
  173. pUsers->outstanding = 0.00;
  174. }
  175. else
  176. printf("Invalid Input please try again:");
  177.  
  178. }while (pUser->type != 'A' && pUser->type != 'a' && pUser->type != 'S' && pUser->type != 's');
  179.  
  180. getUserInfo(&pUser -> info);
  181.  
  182. }
  183.  
  184. void displayAllRecur(ptrUser pUsers){
  185.  
  186. if (pUsers != NULL){
  187. printf("%s\n", pUsers -> username);
  188. displayAllRecur(pUsers -> pNext);
  189. }
  190. }
  191.  
  192. ptrUser search(ptrUser pFirst, string15 username){
  193.  
  194. ptrUser pRun;
  195. pRun = pFirst;
  196.  
  197. while (pRun != NULL && strcmp(username, pRun -> username) != 0){
  198.  
  199. pRun = pRun -> pNext;
  200.  
  201. }
  202.  
  203. return pRun;
  204.  
  205. }
  206.  
  207. void deleteNode(ptrUser *pFirst, string15 username){
  208.  
  209. ptrUser pFind, pRun;
  210.  
  211. if (*pFirst == NULL)
  212. printf("The List is empty");
  213.  
  214. else {
  215.  
  216. pFind = search(*pFirst, username);
  217. if (pFind == NULL)
  218.  
  219. printf("%s is not in the List\n", username);
  220.  
  221. else { //found the node to be deleted
  222.  
  223. if(pFind == *pFirst){ //deleting first node
  224.  
  225. *pFirst = (*pFirst) -> pNext;
  226.  
  227. }
  228.  
  229. else{ //delete from middle or end
  230.  
  231. pRun = *pFirst;
  232. while (pRun -> pNext == pFind)
  233. pRun = pRun -> pNext;
  234.  
  235. pRun -> pNext = pFind -> pNext;
  236.  
  237. }
  238.  
  239. free(pFind);
  240. pFind = NULL;
  241.  
  242. }
  243.  
  244. }
  245.  
  246. }
  247.  
  248. int login(userType *pUsers){
  249. int ctr=0;
  250. string15 userchk, passchk;
  251.  
  252. printf("Enter Username:");scanf("%s",userchk);
  253. printf("Enter Password:");scanf("%s",passchk);
  254. while(pUsers != NULL){
  255. while(strcmp(pUsers -> username, userchk)!=0){
  256. pUsers = pUsers->pNext;
  257. printf("Username Invalid try again\n");
  258. }
  259. while(strcmp(pUsers -> username, userchk)==0 && strcmp(pUsers -> password, passchk)!=0)
  260. {
  261. ctr++;
  262. pUsers = pUsers->pNext;
  263. if(ctr==3){
  264. printf("counter exceeded account locked... Exiting program");
  265. pUsers->nlock = 1;
  266. return 0;
  267. }
  268.  
  269. printf("Incorrect Password try again\n");
  270. printf("Enter Password:");scanf("%s",&passchk);
  271. }
  272.  
  273. if(strcmp(pUsers -> username, userchk)==0 && strcmp(pUsers -> password, passchk)==0)
  274. {
  275. if(pUsers->type=='A' || pUsers->type == 'a')
  276. {
  277. printf("going to admin menu\n");
  278. return 2;
  279. }
  280. else if(pUsers->type=='S' || pUsers->type == 's'){
  281. printf("welcome to the shopper menu\n");
  282. return 1;
  283. }
  284. }
  285. }
  286. }
  287.  
  288. void lockedaccs(userType *pUsers){
  289. printf("Here are all the locked accounts\n");
  290. do{
  291. if(pUsers->nlock==1)
  292. printf("%s\n",pUsers->username);
  293. pUsers = pUsers->pNext;
  294. }while(pUsers!=NULL);
  295. }
  296.  
  297. void manageaccs(userType *pUsers){
  298. int g;
  299. string15 userchk;
  300.  
  301. printf("Welcome to Manage Accounts Menu");
  302.  
  303. do{
  304. 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");
  305. scanf("%d",&g);
  306.  
  307. switch(g){
  308. case 1:lockedaccs(pUsers);break;
  309.  
  310. case 2:printf("Type the username of the account you wish to unlock:\n");scanf("%s", &userchk);
  311. lockedaccs(pUsers);
  312. do{
  313. pUsers = pUsers->pNext;
  314. if(strcmp(pUsers -> username, userchk)==0)
  315. pUsers->nlock = 0;
  316. printf("User unlocked");
  317. }while(pUsers!=NULL);break;
  318.  
  319. case 3:printf("Unlocking All Locked Accounts");
  320. do{
  321. pUsers = pUsers->pNext;
  322. pUsers->nlock = 0;
  323. }while(pUsers!=NULL && pUsers->nlock==1);break;
  324. case 4:printf("Displaying All Accounts with Outstanding Balance");
  325. do{
  326. pUsers = pUsers->pNext;
  327. if(pUsers->outstanding<0)
  328. printf("%s", pUsers->username);
  329. }while(pUsers!=NULL);break;
  330. case 5:printf("Returning to Admin Menu");break;
  331. }
  332. }while(g!=5);
  333. }
  334.  
  335. stockinfo *productCode ()
  336.  
  337. void addstock(stocktype *pNewStock, stocktype *pStock, stockcategtype *pStockcateg, stocktype *pLastStock, stockcategtype *pNewStcateg, stockcategtype *pLastStcateg){
  338. int n,nqa,nqs;
  339. float fpp,fusp,fdr;
  340. char cDump;
  341.  
  342. do{//add stockcategtypes later to account for unique categories and str pNewStock for others
  343. printf("Enter Category:");fgets(pStockcateg->category, 16, stdin);
  344.  
  345. printf("You entered %s Category\n",pStockcateg->category);
  346.  
  347. printf("Input the name of the supplier:");scanf("%s%c",pNewStock->supplier,&cDump);
  348.  
  349. printf("Input name of product:");scanf("%s%c",pNewStock->product,&cDump);
  350.  
  351. printf("Enter Available Product Quantity: "); scanf("%d%c", &nqa, &cDump);pNewStock->Qavail = nqa;
  352.  
  353. printf("Input Price of the Product: ");scanf("%f%c", &fpp, &cDump);pNewStock->purchasePrice = fpp;
  354.  
  355. printf("Input the Unit Selling Price of the product: ");scanf("%f%c", &fusp, &cDump);pNewStock->sellingPrice = fusp;
  356.  
  357. printf("Input the Discount Rate of the product: ");scanf("%f%c", &fdr, &cDump);pNewStock->discountRate = fdr;
  358.  
  359. pNewStock->Qsold = 0;
  360.  
  361. printf("type 1 to add new stock\n0 to return to Manage Stocks Menu:");scanf("%d",&n);
  362. if(n!=1 || n!=0 )
  363. printf("Invalid Input... Returning you to Manage Stocks Menu");
  364.  
  365. }while(n!=0);
  366. }
  367.  
  368. void managestock(stocktype *pStock, stocktype *pNewStock, stockcategtype *pStockcateg, stocktype *pLastStock, stockcategtype *pNewStcateg, stockcategtype *pLastStcateg){
  369. int g,a;
  370.  
  371. printf("Welcome to Manage Stocks Menu");
  372.  
  373. do{
  374. 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");
  375. scanf("%d",&g);
  376.  
  377. switch(g){
  378. case 1:printf("Adding New Stock");
  379. do{
  380. pNewStock = malloc(sizeof(stocktype));//Based almost everything from signup
  381.  
  382. addstock(pNewStock,pStock,pStockcateg,pLastStock,pNewStcateg,pLastStcateg);
  383. pNewStock->pNextstockinfo=NULL;
  384. pNewStcateg->pNextstockcateg=NULL;
  385. if(pStock==NULL && pStockcateg){
  386. pStock=pNewStock;
  387. pStockcateg=pNewStcateg;
  388. }
  389. else
  390. pLastStock->pNextstockinfo = pNewStock;
  391. pLastStcateg->pNextstockcateg = pNewStcateg;
  392. pLastStock = pNewStock;
  393. pLastStcateg = pNewStcateg;
  394. }while(a!=0);break;
  395. case 2:printf("Viewing All Stocks");break;
  396.  
  397. case 3:printf("Viewing Stocks by Category");break;
  398.  
  399. case 4:printf("View Stocks to reorder");break;
  400.  
  401. case 5:printf("Modifying Stock Info");break;
  402.  
  403. case 6:printf("Restocking");break;
  404.  
  405. case 7:printf("Saving Inventory");break;
  406.  
  407. case 8:printf("Updating Inventory from file");break;
  408.  
  409. case 9:printf("Returning to Admin menu");break;
  410. }
  411. }while(g!=9);
  412. }
  413.  
  414. 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){
  415. int i;
  416.  
  417. printf("Welcome to admin menu\n");
  418.  
  419. do{
  420. 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");
  421. scanf("%d",&i);
  422.  
  423. switch(i){
  424. case 1:printf("Going to Manage Accounts Menu please wait\n");
  425. manageaccs(pUsers);break;
  426. case 2:printf("Going to Manage Stocks Menu please wait\n");
  427. managestock(pStock, pNewStock, pStcateg, pLastStock,pNewStcateg,pLastStcateg);break;
  428. case 3:printf("Preparing Delivery Receipt Please Wait\n")/*phase 2*/;break;
  429. case 4:printf("Shutting Down\n");*end=1;break;//passes 1 to end so that it'll terminate the program
  430. case 5:printf("Logging Out Goodbye\n");*n=1;break;//assign 1 to n so that it'll return to sign up
  431. }
  432. }while(i!=4 && i!=5);
  433.  
  434. }
  435.  
  436. void ModifyInfo(userInfoType *pInfo){
  437. int n;
  438.  
  439. printf("Type 1 to Modify First Name\nType 2 to Modify Middle Name\nType 3 To Modify Last Name\nType 4 To Modify Address\n");
  440. scanf("%d",n);
  441. switch(n){
  442.  
  443. case 1:printf("First Name: "); fgets(pInfo -> name.first, 21, stdin);break;
  444. case 2:printf("Middle Name: "); fgets(pInfo -> name.middle, 21, stdin);break;
  445. case 3:printf("Last Name: "); fgets(pInfo -> name.last, 21, stdin);break;
  446. case 4:printf("Enter Address: "); fgets(pInfo -> address, 51, stdin);break;
  447. }
  448. printf("Your new name 'Last Name,First Name, Middle Name\n'%s,%s%s \n", pInfo -> name.last, pInfo -> name.first, pInfo -> name.middle);
  449. printf("Your new Address\n%s", pInfo->address);
  450.  
  451. pInfo -> address[strlen(pInfo -> address) - 1] = '\0';
  452. pInfo -> name.first[strlen(pInfo -> name.first) - 1] = '\0';
  453. pInfo -> name.middle[strlen(pInfo -> name.middle) - 1] = '\0';
  454. pInfo -> name.last[strlen(pInfo -> name.last) - 1] = '\0';
  455. }
  456.  
  457. void ShopperMenu(userType *pUsers, userInfoType *pInfo, int *n){
  458. int g;
  459.  
  460. printf("Welcome to shopper menu\n");
  461.  
  462. 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:");
  463. scanf("%d",&g);
  464.  
  465. switch(g){
  466. case 1:printf("Modify your info\n");
  467. ModifyInfo(&pUsers->info);break;
  468. case 2:printf("Browsing All Products");break;
  469. case 3:printf("Browse Products by Category");break;
  470. case 4:printf("Browse Products on Sale");break;
  471. case 5:printf("Adding to cart");break;
  472. case 6:printf("Viewing Cart");break;
  473. case 7:printf("Settling Outstanding Balance");break;
  474. case 8:printf("Logging Out Goodbye\n");*n=1 ;break;
  475. }
  476. }
  477.  
  478. int main(){
  479.  
  480. /*
  481. userType user;
  482. userType *pUsers = NULL,
  483. *pNew;
  484. */
  485. ptrUser pUsers = NULL,
  486. pNew, pLast;
  487.  
  488. ptrStock pStock = NULL,
  489. pNewStock,pLastStock;
  490.  
  491. ptrStockcateg pStcateg = NULL,
  492. pNewStcateg,pLastStcateg;
  493.  
  494. int end=1,n;
  495. char cDump;
  496. ptrUser pRun, pTrail;
  497.  
  498. do{
  499. printf("Welcome to the shopping kiosk\n\n");
  500. 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: ");
  501. scanf("%d",&n);
  502.  
  503. switch(n){
  504.  
  505. case 2:if(login(pUsers)==2)
  506. AdminMenu(pUsers, &pUsers->info, pNew, pLast, &end, pStock, pNewStock, pStcateg, &n, pLastStock, pNewStcateg, pLastStcateg);
  507. else if(login(pUsers)==1)
  508. ShopperMenu(pUsers, &pUsers->info, &n);
  509. else if(login(pUsers)==0)
  510. end=0;break;
  511.  
  512.  
  513. default:
  514. /*
  515. pNew = malloc(sizeof(userType));
  516.  
  517. signUp(pNew);
  518. pNew -> pNext = NULL;
  519.  
  520. if (pUsers == NULL)
  521. pUsers = pNew;
  522.  
  523. else
  524. pLast -> pNext = pNew;
  525.  
  526. pLast = pNew;
  527. */
  528.  
  529. pNew = malloc(sizeof(userType));
  530.  
  531. signUp(pNew,pUsers);
  532. pNew -> pNext = NULL;
  533.  
  534. if (pUsers == NULL) //if list is empty
  535. pUsers = pNew;
  536.  
  537. else if (strcmp(pUsers -> username, pNew -> username) > 0){ //conect as first node
  538.  
  539. pNew -> pNext = pUsers;
  540. pUsers = pNew;
  541.  
  542. }
  543.  
  544. else { //modifying middle of list
  545.  
  546. pRun = pUsers;
  547. while (pRun != NULL && strcmp(pRun -> username, pNew -> username) < 0){
  548.  
  549. pTrail = pRun;
  550. pRun = pRun -> pNext;
  551.  
  552. }
  553.  
  554. pTrail -> pNext = pNew;
  555. pNew -> pNext = pRun;
  556.  
  557. }
  558. printf("Type '1' to Return to Log in/Sign up menu or '0' To exit the program. "); scanf("%d%c", &end, &cDump);
  559.  
  560. break;
  561. //use for view locked accounts displayAllRecur(pUsers);
  562. //displayAll(pUsers);
  563.  
  564. }
  565. } while (end != 0);
  566. freeAll(&pUsers);
  567. freeAllstockver(&pStock);
  568.  
  569. return 0;
  570. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement