Advertisement
Guest User

INOMINE PARTI

a guest
Mar 7th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define MAXCHAR 16
  5. #define MAXADD 51
  6. #define MAXNAME 21
  7. #define MAXCODE 9
  8. #define MAXCART 100
  9. typedef char Char [MAXCHAR];
  10. typedef char Name [MAXNAME];
  11. typedef char Add [MAXADD];
  12. typedef char Code [MAXCODE];
  13. typedef char Adcd [MAXCODE];
  14. typedef struct {
  15. Name First;
  16. Name Second;
  17. Name Last;
  18. }sName;
  19. typedef struct {
  20. sName Userinfo;
  21. Add Address;
  22. }Userinfo;
  23. typedef struct {
  24. Code Prodcode;
  25. int qty;
  26. }ProductTag;
  27. typedef ProductTag arrBought[MAXCART];
  28. typedef struct Usertag {
  29. Char username;
  30. Char password;
  31. Userinfo info;
  32. char type;
  33. float credlimit;
  34. float outstanding;
  35. arrBought cart;
  36. int nItems;
  37. int count;
  38. struct Usertag *pNext;
  39. };
  40. typedef struct Usertag Usertype;
  41. typedef Usertype * ptrUser;
  42. typedef struct {
  43. Char supplier;
  44. Char product;
  45. int quantity;
  46. float puchase;
  47. float unit;
  48. float discount;
  49. int sold;
  50. Code code;
  51. }Productlist;
  52. typedef struct {
  53. Char category;
  54. Productlist list;
  55. }Addstock;
  56. void getUserinfo (Userinfo *uInfo){
  57. printf("Enter your First Name: ");
  58. fgets(uInfo->Userinfo.First ,MAXNAME,stdin);
  59. printf("Enter your Middle Name: ");
  60. fgets(uInfo->Userinfo.Second,MAXNAME,stdin);
  61. printf("Enter your Last Name: ");
  62. fgets(uInfo->Userinfo.Last,MAXNAME,stdin);
  63. printf("Enter your Address: ");
  64. fgets(uInfo->Address,MAXADD,stdin);
  65. uInfo->Userinfo.Last[strlen(uInfo->Userinfo.Last)-1]='\0';
  66. uInfo->Userinfo.First[strlen(uInfo->Userinfo.First)-1]='\0';
  67. uInfo->Userinfo.Second[strlen(uInfo->Userinfo.Second)-1]='\0';
  68. if (uInfo->Userinfo.First[0] >= 97 && uInfo->Userinfo.First[0] <=122)
  69. uInfo->Userinfo.First[0]-=32;
  70. if (uInfo->Userinfo.Second[0] >= 97 && uInfo->Userinfo.Second[0] <=122)
  71. uInfo->Userinfo.Second[0]-=32;
  72. if (uInfo->Userinfo.Last[0] >= 97 && uInfo->Userinfo.Last[0] <=122)
  73. uInfo->Userinfo.Last[0]-=32;
  74. printf("Your Name is: ");
  75. printf("%s, %s %s\n",uInfo->Userinfo.Last,uInfo->Userinfo.First,uInfo->Userinfo.Second);
  76. printf("Your Address is: ");
  77. printf("%s\n",uInfo->Address);
  78. }
  79. int passcheck (Usertype *user){
  80. int i,count=0;
  81. for (i=0;i<strlen(user->password);i++)
  82. if ((user->password[i] < 'A' || user->password[i] > 'Z'))
  83. if ((user->password[i] < 'a' || user->password[i] > 'z'))
  84. count++;
  85. if (count==0)
  86. printf("You must have at least 1 non-letter character\n");
  87. return count;
  88. }
  89. int length (Usertype *user){
  90. if (strlen (user->password) < 6){
  91.  
  92. printf("password is too short \n");
  93. return 0;
  94. }
  95.  
  96. else if (strlen (user->password) > 15){
  97. printf("password is too long \n");
  98. return 0;
  99. }
  100. else return 1;
  101. }
  102. void Signup (Usertype *user){
  103. char cDump;
  104. Adcd Admincode;
  105. do{
  106. printf("Enter Username: ");
  107. scanf("%s%c",user->username,&cDump);
  108. } while (strlen (user->username) < 3|| strlen(user->username) > 15);
  109. printf("%s\n",user->username);
  110.  
  111. do{
  112. printf("Enter Password: ");
  113. scanf("%s%c",user->password,&cDump);
  114. } while (length (user)==0 || passcheck(user)==0);
  115.  
  116. printf("%s\n",user->password);
  117.  
  118. getUserinfo (&user->info);
  119. printf("Enter account type [S]hopper or [A]dmin : ");
  120. scanf("%c",&user->type);
  121. if (user->type=='s'|| user->type=='S'){
  122. user->credlimit = 5000.00;
  123. user->outstanding = 0.00;
  124. user->nItems = 0;
  125. }
  126. else if (user->type == 'a' || user->type == 'A'){
  127. printf("Enter Admin Code: \n");
  128. do{
  129. scanf("%s",Admincode);
  130. if (strcmp(Admincode,"DLSU2017")==0)
  131. user->type = 'A';
  132. } while (strcmp(Admincode,"DLSU2017")!=0);
  133. }
  134. else printf("Invalid input");
  135. user->count=0;
  136. }
  137. int login (Usertype *user) {
  138. Char input;
  139. do {
  140. printf("Enter Username: ");
  141. scanf("%s",input);
  142. if (strcmp (user->username,input)==0 && user->count < 4){
  143. do{
  144. printf("Enter Password: ");
  145. scanf("%s",input);
  146. if (strcmp (user->password,input)!=0){
  147. printf("Try again, %d try/s left\n",3-user->count);
  148. user->count++;
  149. }
  150. } while (user->count <= 3 && strcmp (user->password,input) !=0);
  151. if (strcmp(user->password,input)==0)
  152. return 1;
  153. else if (user->count >= 3)
  154. return 0;
  155. }
  156.  
  157. } while (strcmp (user->username,input));
  158. return 0;
  159. }
  160. void Adminmenu (Usertype *user,int *esc){
  161. int exit=1,input;
  162. do {
  163. printf("ADMIN MENU \n");
  164. printf("Choose an option \n");
  165. printf("Manage Accounts: 1 \n");
  166. printf("Manage Stocks: 2 \n");
  167. printf("Prepare Delivery Report: 3 \n");
  168. printf("Shutdown Kioski: 4 \n");
  169. printf("Log out: 5 \n");
  170. scanf("%d",&input);
  171. switch (input) {
  172. // case 1: Manageaccounts (user);
  173. // case 2: Managestocks (user);
  174. // case 3: Deliveryreport (user);
  175. case 4: *esc = 0; break;
  176. case 5: exit = 0;
  177. }
  178. } while (exit);
  179. }
  180. void Changename (Userinfo *uInfo){
  181. printf("Enter your First Name: ");
  182. fgets(uInfo->Userinfo.First,MAXNAME,stdin);
  183. fgets(uInfo->Userinfo.First,MAXNAME,stdin);
  184. printf("Enter your Middle Name: ");
  185. fgets(uInfo->Userinfo.Second,MAXNAME,stdin);
  186. printf("Enter your Last Name: ");
  187. fgets(uInfo->Userinfo.Last,MAXNAME,stdin);
  188. uInfo->Userinfo.Last[strlen(uInfo->Userinfo.Last)-1]='\0';
  189. uInfo->Userinfo.First[strlen(uInfo->Userinfo.First)-1]='\0';
  190. uInfo->Userinfo.Second[strlen(uInfo->Userinfo.Second)-1]='\0';
  191. if (uInfo->Userinfo.First[0] >= 97 && uInfo->Userinfo.First[0] <=122)
  192. uInfo->Userinfo.First[0]-=32;
  193. if (uInfo->Userinfo.Second[0] >= 97 && uInfo->Userinfo.Second[0] <=122)
  194. uInfo->Userinfo.Second[0]-=32;
  195. if (uInfo->Userinfo.Last[0] >= 97 && uInfo->Userinfo.Last[0] <=122)
  196. uInfo->Userinfo.Last[0]-=32;
  197. printf("Your New Name is: ");
  198. printf("%s, %s %s\n",uInfo->Userinfo.Last,uInfo->Userinfo.First,uInfo->Userinfo.Second);
  199. }
  200. void Changeadd (Userinfo *uInfo) {
  201. printf("Enter your Address: ");
  202. fgets(uInfo->Address,MAXADD,stdin);
  203. fgets(uInfo->Address,MAXADD,stdin);
  204. printf("Your New Address is: ");
  205. printf("%s\n",uInfo->Address);
  206. }
  207. void Changepass (Usertype *user) {
  208. char cDump;
  209. do{
  210. printf("Enter Password: ");
  211. scanf("%s%c",user->password,&cDump);
  212. } while (length (user)==0 || passcheck(user)==0);
  213. printf("Your new password is: ");
  214. printf("%s\n", user->password);
  215. }
  216. void Modifyinfo (Usertype *user) {
  217. int exit = 1,input;
  218. do {
  219. printf("Modify User Menu \n");
  220. printf("Choose an option \n");
  221. printf("Change Name: 1 \n");
  222. printf("Change Address: 2 \n");
  223. printf("Change Password: 3 \n");
  224. printf("Return to Shopper Menu: 4 \n");
  225. scanf("%d",&input);
  226. switch (input) {
  227. case 1: Changename (&user->info); break;
  228. case 2: Changeadd (&user->info); break;
  229. case 3: Changepass (user); break;
  230. case 4: exit = 0;
  231. }
  232. } while (exit);
  233. }
  234. void Settlebalance (Usertype *user) {
  235. int card,security,paid=1;
  236. float payment;
  237. printf("Your outstanding balance is %.2f \n",user->outstanding);
  238. printf("Enter your credit card number (without spaces): ");
  239. scanf("%d",&card);
  240. printf("Enter your security code: ");
  241. scanf("%d",&security);
  242. do {
  243. printf("Enter amount: ");
  244. scanf("%f",&payment);
  245. if (payment <= user->outstanding){
  246. user->outstanding -= payment;
  247. paid=0;
  248. }
  249. else
  250. printf("You entered too much!");
  251. } while (paid);
  252. printf("Your new balance is %.2f\n",user->outstanding);
  253. }
  254. void Shoppermenu (Usertype *user){
  255. int exit = 1,input;
  256. do {
  257. printf("Shopper Menu \n");
  258. printf("Choose an option \n");
  259. printf("Modify User Info: 1 \n");
  260. printf("Browse All Products: 2 \n");
  261. printf("Browse Products by Category: 3 \n");
  262. printf("Browse Products on Sale: 4 \n");
  263. printf("Add to Cart: 5 \n");
  264. printf("View Cart: 6 \n");
  265. if (user->outstanding != 0.00)
  266. printf("Settle Outstanding Balance: 7 \n");
  267. printf("Log out: 8 \n");
  268. scanf ("%d",&input);
  269. switch (input) {
  270. case 1: Modifyinfo (user); break;
  271. case 2:
  272. case 3:
  273. case 4:
  274. case 5:
  275. case 6:
  276. case 7: Settlebalance (user); break;
  277. case 8: exit = 0;
  278. }
  279. } while (exit);
  280. }
  281. void displayAll (ptrUser pUser){
  282. while (pUser != NULL) {
  283. printf("%s\n",pUser->username);
  284. pUser=pUser->pNext;
  285. }
  286. }
  287. void freeAll (ptrUser *pUser) {
  288. ptrUser pTemp;
  289. while ((*pUser) != NULL){
  290. pTemp= *pUser;
  291. *pUser = (*pUser)->pNext;
  292. free(pTemp);
  293. }
  294. }
  295. void displayAllRecur (ptrUser pUsers) {
  296. if (pUsers!=NULL){
  297. printf("%s\n",pUsers->username);
  298. displayAllRecur (pUsers->pNext);
  299. }
  300. }
  301.  
  302. ptrUser search (ptrUser pFirst, Char username){ // ADD TO CODE
  303. ptrUser pRun;
  304. pRun = pFirst;
  305. while (pRun != NULL && strcmp(username,pRun->username) != 0)
  306. pRun = pRun->pNext;
  307. return pRun;
  308. }
  309.  
  310. void deleteNode (ptrUser *pFirst, Char username){ // ADD TO CODE
  311. ptrUser pFind,pRun;
  312. if (*pFirst == NULL)
  313. printf("List is empty");
  314. else {
  315. pFind = search (*pFirst,username);
  316. if (pFind == NULL)
  317. printf("%s is not in the list\n",username);
  318. else {
  319. if (pFind == *pFirst){
  320. *pFirst == (*pFirst)->pNext;
  321. }
  322. else {
  323. pRun = *pFirst;
  324. while (pRun->pNext != pFind)
  325. pRun = pRun->pNext;
  326. pRun->pNext = pFind->pNext;
  327. }
  328. free(pFind);
  329. pFind = NULL;
  330. }
  331. }
  332. int main () {
  333. int exit=1,input;
  334. Usertype user;
  335. ptrUser pUsers = NULL,
  336. pNew, pLast,
  337. pRun, pTrail;
  338. Char input;
  339. do {
  340. printf("Main Menu \n");
  341. printf("Choose an option: \n");
  342. printf("Log-in : 1\n");
  343. printf("Sign-up : 2\n");
  344. scanf("%d",&input);
  345. switch (input){
  346. case 1: if (login (&user)){
  347. if (user.type == 's' || user.type == 'S')
  348. Shoppermenu (&user);
  349. else if (user.type == 'A')
  350. Adminmenu (&user,&exit);
  351. } break;
  352. case 2: pNew = malloc(sizeof(Usertype)); // ADD TO CODE
  353. Signup (pNew);
  354. pNew->pNext = NULL ;
  355. if (pUsers == NULL)
  356. pUsers = pNew;
  357. else if (strcmp (pUsers->username, pNew->username)>0){
  358. pNew->pNext = pUsers;
  359. pUsers = pNew;
  360. }
  361. else {
  362. pRun = pUsers ;
  363. while (pRun != NULL && strcmp(pRun->username, pNew->username) < 0) {
  364. pTrail = pRun;
  365. pRun = pRun->pNext;
  366. }
  367. pTrail->pNext = pNew;
  368. pNew->pNext=pRun;
  369. }
  370. break;
  371. case 3: displayAllRecur (pUsers); break;
  372. case 4: freeAll(&pUsers);
  373. case 5: scanf("&s",&input);
  374. deleteNode (&pUsers,input);
  375. }
  376. } while (exit);
  377.  
  378. return 0;
  379. }
  380. /* while (pUsers != NULL) { // ADD TO CODE
  381. pTemp = pUsers;
  382. pUsers = pUsers->pNext;
  383. }
  384. */
  385. /* ptrUser pRun
  386. pNew = malloc(sizeof(Usertype));
  387. Signup (pNew);
  388. pNew->pNext = NULL ;
  389. if (pUsers == NULL)
  390. pUsers = pNew;
  391. else
  392. pLast->pNext = pNew;
  393. pLast=pNew;
  394. break;
  395. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement