Advertisement
Guest User

WHAT DID I DO?

a guest
Mar 5th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 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 {
  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. }Usertype;
  39. typedef struct {
  40. Char supplier;
  41. Char product;
  42. int quantity;
  43. float puchase;
  44. float unit;
  45. float discount;
  46. int sold;
  47. Code code;
  48. }Productlist;
  49. typedef struct {
  50. Char category;
  51. Productlist list;
  52. }Addstock;
  53. void getUserinfo (Userinfo *uInfo){
  54. printf("Enter your First Name: ");
  55. fgets(uInfo->Userinfo.First ,MAXNAME,stdin);
  56. printf("Enter your Middle Name: ");
  57. fgets(uInfo->Userinfo.Second,MAXNAME,stdin);
  58. printf("Enter your Last Name: ");
  59. fgets(uInfo->Userinfo.Last,MAXNAME,stdin);
  60. printf("Enter your Address: ");
  61. fgets(uInfo->Address,MAXADD,stdin);
  62. uInfo->Userinfo.Last[strlen(uInfo->Userinfo.Last)-1]='\0';
  63. uInfo->Userinfo.First[strlen(uInfo->Userinfo.First)-1]='\0';
  64. uInfo->Userinfo.Second[strlen(uInfo->Userinfo.Second)-1]='\0';
  65. if (uInfo->Userinfo.First[0] >= 97 && uInfo->Userinfo.First[0] <=122)
  66. uInfo->Userinfo.First[0]-=32;
  67. if (uInfo->Userinfo.Second[0] >= 97 && uInfo->Userinfo.Second[0] <=122)
  68. uInfo->Userinfo.Second[0]-=32;
  69. if (uInfo->Userinfo.Last[0] >= 97 && uInfo->Userinfo.Last[0] <=122)
  70. uInfo->Userinfo.Last[0]-=32;
  71. printf("Your Name is: ");
  72. printf("%s, %s %s\n",uInfo->Userinfo.Last,uInfo->Userinfo.First,uInfo->Userinfo.Second);
  73. printf("Your Address is: ");
  74. printf("%s\n",uInfo->Address);
  75. }
  76. int passcheck (Usertype *user){
  77. int i,count=0;
  78. for (i=0;i<strlen(user->password);i++)
  79. if ((user->password[i] < 'A' || user->password[i] > 'Z'))
  80. if ((user->password[i] < 'a' || user->password[i] > 'z'))
  81. count++;
  82. if (count==0)
  83. printf("You must have at least 1 non-letter character\n");
  84. return count;
  85. }
  86. int length (Usertype *user){
  87. if (strlen (user->password) < 6){
  88.  
  89. printf("password is too short \n");
  90. return 0;
  91. }
  92.  
  93. else if (strlen (user->password) > 15){
  94. printf("password is too long \n");
  95. return 0;
  96. }
  97. else return 1;
  98. }
  99. void Signup (Usertype *user){
  100. char cDump;
  101. Adcd Admincode;
  102. do{
  103. printf("Enter Username: ");
  104. scanf("%s%c",user->username,&cDump);
  105. } while (strlen (user->username) < 3|| strlen(user->username) > 15);
  106. printf("%s\n",user->username);
  107.  
  108. do{
  109. printf("Enter Password: ");
  110. scanf("%s%c",user->password,&cDump);
  111. } while (length (user)==0 || passcheck(user)==0);
  112.  
  113. printf("%s\n",user->password);
  114.  
  115. getUserinfo (&user->info);
  116. printf("Enter account type [S]hopper or [A]dmin : ");
  117. scanf("%c",&user->type);
  118. if (user->type=='s'|| user->type=='S'){
  119. user->credlimit = 5000.00;
  120. user->outstanding = 0.00;
  121. user->nItems = 0;
  122. }
  123. else if (user->type == 'a' || user->type == 'A'){
  124. printf("Enter Admin Code: \n");
  125. do{
  126. scanf("%s",&Admincode);
  127. if (strcmp(Admincode,"DLSU2017")==0)
  128. user->type = 'A';
  129. } while (strcmp(Admincode,"DLSU2017")!=0);
  130. }
  131. user->count=0;
  132. }
  133. int login (Usertype *user) {
  134. Char input;
  135. do {
  136. printf("Enter Username: ");
  137. scanf("%s",input);
  138. if (strcmp (user->username,input)==0 && user->count < 4){
  139. do{
  140. printf("Enter Password: ");
  141. scanf("%s",input);
  142. if (strcmp (user->password,input)!=0){
  143. printf("Try again, %d try/s left\n",3-user->count);
  144. user->count++;
  145. }
  146. } while (user->count <= 3 && strcmp (user->password,input) !=0);
  147. if (strcmp(user->password,input)==0)
  148. return 1;
  149. else if (user->count >= 3)
  150. return 0;
  151. }
  152.  
  153. } while (strcmp (user->username,input));
  154. }
  155. void Adminmenu (Usertype *user,int *esc){
  156. int exit=1,input;
  157. do {
  158. printf("ADMIN MENU \n");
  159. printf("Choose an option \n");
  160. printf("Manage Accounts: 1 \n");
  161. printf("Manage Stocks: 2 \n");
  162. printf("Prepare Delivery Report: 3 \n");
  163. printf("Shutdown Kioski: 4 \n");
  164. printf("Log out: 5 \n");
  165. scanf("%d",&input);
  166. switch (input) {
  167. // case 1: Manageaccounts (user);
  168. // case 2: Managestocks (user);
  169. // case 3: Deliveryreport (user);
  170. case 4: *esc = 0; break;
  171. case 5: exit = 0;
  172. }
  173. } while (exit);
  174. }
  175. void Changename (Userinfo *uInfo){
  176. printf("Enter your First Name: ");
  177. fgets(uInfo->Userinfo.First,MAXNAME,stdin);
  178. fgets(uInfo->Userinfo.First,MAXNAME,stdin);
  179. printf("Enter your Middle Name: ");
  180. fgets(uInfo->Userinfo.Second,MAXNAME,stdin);
  181. printf("Enter your Last Name: ");
  182. fgets(uInfo->Userinfo.Last,MAXNAME,stdin);
  183. uInfo->Userinfo.Last[strlen(uInfo->Userinfo.Last)-1]='\0';
  184. uInfo->Userinfo.First[strlen(uInfo->Userinfo.First)-1]='\0';
  185. uInfo->Userinfo.Second[strlen(uInfo->Userinfo.Second)-1]='\0';
  186. if (uInfo->Userinfo.First[0] >= 97 && uInfo->Userinfo.First[0] <=122)
  187. uInfo->Userinfo.First[0]-=32;
  188. if (uInfo->Userinfo.Second[0] >= 97 && uInfo->Userinfo.Second[0] <=122)
  189. uInfo->Userinfo.Second[0]-=32;
  190. if (uInfo->Userinfo.Last[0] >= 97 && uInfo->Userinfo.Last[0] <=122)
  191. uInfo->Userinfo.Last[0]-=32;
  192. printf("Your New Name is: ");
  193. printf("%s, %s %s\n",uInfo->Userinfo.Last,uInfo->Userinfo.First,uInfo->Userinfo.Second);
  194. }
  195. void Changeadd (Userinfo *uInfo) {
  196. printf("Enter your Address: ");
  197. fgets(uInfo->Address,MAXADD,stdin);
  198. fgets(uInfo->Address,MAXADD,stdin);
  199. printf("Your New Address is: ");
  200. printf("%s\n",uInfo->Address);
  201. }
  202. void Changepass (Usertype *user) {
  203. char cDump;
  204. do{
  205. printf("Enter Password: ");
  206. scanf("%s%c",user->password,&cDump);
  207. } while (length (user)==0 || passcheck(user)==0);
  208. printf("Your new password is: ");
  209. printf("%s\n", user->password);
  210. }
  211. void Modifyinfo (Usertype *user) {
  212. int exit = 1,input;
  213. do {
  214. printf("Modify User Menu \n");
  215. printf("Choose an option \n");
  216. printf("Change Name: 1 \n");
  217. printf("Change Address: 2 \n");
  218. printf("Change Password: 3 \n");
  219. printf("Return to Shopper Menu: 4 \n");
  220. scanf("%d",&input);
  221. switch (input) {
  222. case 1: Changename (&user->info); break;
  223. case 2: Changeadd (&user->info); break;
  224. case 3: Changepass (user); break;
  225. case 4: exit = 0;
  226. }
  227. } while (exit);
  228. }
  229. void Settlebalance (Usertype *user) {
  230. int card,security,paid=1;
  231. float payment;
  232. printf("Your outstanding balance is %.2f \n",user->outstanding);
  233. printf("Enter your credit card number (without spaces): ");
  234. scanf("%d",&card);
  235. printf("Enter your security code: ");
  236. scanf("%d",&security);
  237. do {
  238. printf("Enter amount: ");
  239. scanf("%f",&payment);
  240. if (payment <= user->outstanding){
  241. user->outstanding -= payment;
  242. paid=0;
  243. }
  244. else
  245. printf("You entered too much!");
  246. } while (paid);
  247. printf("Your new balance is %.2f\n",user->outstanding);
  248. }
  249. void Shoppermenu (Usertype *user){
  250. int exit = 1,input;
  251. do {
  252. printf("Shopper Menu \n");
  253. printf("Choose an option \n");
  254. printf("Modify User Info: 1 \n");
  255. printf("Browse All Products: 2 \n");
  256. printf("Browse Products by Category: 3 \n");
  257. printf("Browse Products on Sale: 4 \n");
  258. printf("Add to Cart: 5 \n");
  259. printf("View Cart: 6 \n");
  260. if (user->outstanding != 0.00)
  261. printf("Settle Outstanding Balance: 7 \n");
  262. printf("Log out: 8 \n");
  263. scanf ("%d",&input);
  264. switch (input) {
  265. case 1: Modifyinfo (user); break;
  266. case 2:
  267. case 3:
  268. case 4:
  269. case 5:
  270. case 6:
  271. case 7: Settlebalance (user); break;
  272. case 8: exit = 0;
  273. }
  274. } while (exit);
  275. }
  276. int main () {
  277. int exit=1,input;
  278. Usertype user;
  279. do {
  280. printf("Main Menu \n");
  281. printf("Choose an option: \n");
  282. printf("Log-in : 1\n");
  283. printf("Sign-up : 2\n");
  284. printf("Exit : 3\n");
  285. scanf("%d",&input);
  286. switch (input){
  287. case 1: if (login (&user)){
  288. if (user.type == 's' || user.type == 'S')
  289. Shoppermenu (&user);
  290. else if (user.type == 'A')
  291. Adminmenu (&user,&exit);
  292. } break;
  293. case 2: Signup (&user); break;
  294. case 3: printf("Exiting program\n"); exit=0; break;
  295. }
  296. } while (exit);
  297. return 0;
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement