Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define MAXCHAR 16
- #define MAXADD 51
- #define MAXNAME 21
- #define MAXCODE 9
- #define MAXCART 100
- typedef char Char [MAXCHAR];
- typedef char Name [MAXNAME];
- typedef char Add [MAXADD];
- typedef char Code [MAXCODE];
- typedef char Adcd [MAXCODE];
- typedef struct {
- Name First;
- Name Second;
- Name Last;
- }sName;
- typedef struct {
- sName Userinfo;
- Add Address;
- }Userinfo;
- typedef struct {
- Code Prodcode;
- int qty;
- }ProductTag;
- typedef ProductTag arrBought[MAXCART];
- typedef struct Usertag {
- Char username;
- Char password;
- Userinfo info;
- char type;
- float credlimit;
- float outstanding;
- arrBought cart;
- int nItems;
- int count;
- struct Usertag *pNext;
- };
- typedef struct Usertag Usertype;
- typedef Usertype * ptrUser;
- typedef struct {
- Char supplier;
- Char product;
- int quantity;
- float puchase;
- float unit;
- float discount;
- int sold;
- Code code;
- }Productlist;
- typedef struct {
- Char category;
- Productlist list;
- }Addstock;
- void getUserinfo (Userinfo *uInfo){
- printf("Enter your First Name: ");
- fgets(uInfo->Userinfo.First ,MAXNAME,stdin);
- printf("Enter your Middle Name: ");
- fgets(uInfo->Userinfo.Second,MAXNAME,stdin);
- printf("Enter your Last Name: ");
- fgets(uInfo->Userinfo.Last,MAXNAME,stdin);
- printf("Enter your Address: ");
- fgets(uInfo->Address,MAXADD,stdin);
- uInfo->Userinfo.Last[strlen(uInfo->Userinfo.Last)-1]='\0';
- uInfo->Userinfo.First[strlen(uInfo->Userinfo.First)-1]='\0';
- uInfo->Userinfo.Second[strlen(uInfo->Userinfo.Second)-1]='\0';
- if (uInfo->Userinfo.First[0] >= 97 && uInfo->Userinfo.First[0] <=122)
- uInfo->Userinfo.First[0]-=32;
- if (uInfo->Userinfo.Second[0] >= 97 && uInfo->Userinfo.Second[0] <=122)
- uInfo->Userinfo.Second[0]-=32;
- if (uInfo->Userinfo.Last[0] >= 97 && uInfo->Userinfo.Last[0] <=122)
- uInfo->Userinfo.Last[0]-=32;
- printf("Your Name is: ");
- printf("%s, %s %s\n",uInfo->Userinfo.Last,uInfo->Userinfo.First,uInfo->Userinfo.Second);
- printf("Your Address is: ");
- printf("%s\n",uInfo->Address);
- }
- int passcheck (Usertype *user){
- int i,count=0;
- for (i=0;i<strlen(user->password);i++)
- if ((user->password[i] < 'A' || user->password[i] > 'Z'))
- if ((user->password[i] < 'a' || user->password[i] > 'z'))
- count++;
- if (count==0)
- printf("You must have at least 1 non-letter character\n");
- return count;
- }
- int length (Usertype *user){
- if (strlen (user->password) < 6){
- printf("password is too short \n");
- return 0;
- }
- else if (strlen (user->password) > 15){
- printf("password is too long \n");
- return 0;
- }
- else return 1;
- }
- void Signup (Usertype *user){
- char cDump;
- Adcd Admincode;
- do{
- printf("Enter Username: ");
- scanf("%s%c",user->username,&cDump);
- } while (strlen (user->username) < 3|| strlen(user->username) > 15);
- printf("%s\n",user->username);
- do{
- printf("Enter Password: ");
- scanf("%s%c",user->password,&cDump);
- } while (length (user)==0 || passcheck(user)==0);
- printf("%s\n",user->password);
- getUserinfo (&user->info);
- printf("Enter account type [S]hopper or [A]dmin : ");
- scanf("%c",&user->type);
- if (user->type=='s'|| user->type=='S'){
- user->credlimit = 5000.00;
- user->outstanding = 0.00;
- user->nItems = 0;
- }
- else if (user->type == 'a' || user->type == 'A'){
- printf("Enter Admin Code: \n");
- do{
- scanf("%s",Admincode);
- if (strcmp(Admincode,"DLSU2017")==0)
- user->type = 'A';
- } while (strcmp(Admincode,"DLSU2017")!=0);
- }
- else printf("Invalid input");
- user->count=0;
- }
- int login (Usertype *user) {
- Char input;
- do {
- printf("Enter Username: ");
- scanf("%s",input);
- if (strcmp (user->username,input)==0 && user->count < 4){
- do{
- printf("Enter Password: ");
- scanf("%s",input);
- if (strcmp (user->password,input)!=0){
- printf("Try again, %d try/s left\n",3-user->count);
- user->count++;
- }
- } while (user->count <= 3 && strcmp (user->password,input) !=0);
- if (strcmp(user->password,input)==0)
- return 1;
- else if (user->count >= 3)
- return 0;
- }
- } while (strcmp (user->username,input));
- return 0;
- }
- void Adminmenu (Usertype *user,int *esc){
- int exit=1,input;
- do {
- printf("ADMIN MENU \n");
- printf("Choose an option \n");
- printf("Manage Accounts: 1 \n");
- printf("Manage Stocks: 2 \n");
- printf("Prepare Delivery Report: 3 \n");
- printf("Shutdown Kioski: 4 \n");
- printf("Log out: 5 \n");
- scanf("%d",&input);
- switch (input) {
- // case 1: Manageaccounts (user);
- // case 2: Managestocks (user);
- // case 3: Deliveryreport (user);
- case 4: *esc = 0; break;
- case 5: exit = 0;
- }
- } while (exit);
- }
- void Changename (Userinfo *uInfo){
- printf("Enter your First Name: ");
- fgets(uInfo->Userinfo.First,MAXNAME,stdin);
- fgets(uInfo->Userinfo.First,MAXNAME,stdin);
- printf("Enter your Middle Name: ");
- fgets(uInfo->Userinfo.Second,MAXNAME,stdin);
- printf("Enter your Last Name: ");
- fgets(uInfo->Userinfo.Last,MAXNAME,stdin);
- uInfo->Userinfo.Last[strlen(uInfo->Userinfo.Last)-1]='\0';
- uInfo->Userinfo.First[strlen(uInfo->Userinfo.First)-1]='\0';
- uInfo->Userinfo.Second[strlen(uInfo->Userinfo.Second)-1]='\0';
- if (uInfo->Userinfo.First[0] >= 97 && uInfo->Userinfo.First[0] <=122)
- uInfo->Userinfo.First[0]-=32;
- if (uInfo->Userinfo.Second[0] >= 97 && uInfo->Userinfo.Second[0] <=122)
- uInfo->Userinfo.Second[0]-=32;
- if (uInfo->Userinfo.Last[0] >= 97 && uInfo->Userinfo.Last[0] <=122)
- uInfo->Userinfo.Last[0]-=32;
- printf("Your New Name is: ");
- printf("%s, %s %s\n",uInfo->Userinfo.Last,uInfo->Userinfo.First,uInfo->Userinfo.Second);
- }
- void Changeadd (Userinfo *uInfo) {
- printf("Enter your Address: ");
- fgets(uInfo->Address,MAXADD,stdin);
- fgets(uInfo->Address,MAXADD,stdin);
- printf("Your New Address is: ");
- printf("%s\n",uInfo->Address);
- }
- void Changepass (Usertype *user) {
- char cDump;
- do{
- printf("Enter Password: ");
- scanf("%s%c",user->password,&cDump);
- } while (length (user)==0 || passcheck(user)==0);
- printf("Your new password is: ");
- printf("%s\n", user->password);
- }
- void Modifyinfo (Usertype *user) {
- int exit = 1,input;
- do {
- printf("Modify User Menu \n");
- printf("Choose an option \n");
- printf("Change Name: 1 \n");
- printf("Change Address: 2 \n");
- printf("Change Password: 3 \n");
- printf("Return to Shopper Menu: 4 \n");
- scanf("%d",&input);
- switch (input) {
- case 1: Changename (&user->info); break;
- case 2: Changeadd (&user->info); break;
- case 3: Changepass (user); break;
- case 4: exit = 0;
- }
- } while (exit);
- }
- void Settlebalance (Usertype *user) {
- int card,security,paid=1;
- float payment;
- printf("Your outstanding balance is %.2f \n",user->outstanding);
- printf("Enter your credit card number (without spaces): ");
- scanf("%d",&card);
- printf("Enter your security code: ");
- scanf("%d",&security);
- do {
- printf("Enter amount: ");
- scanf("%f",&payment);
- if (payment <= user->outstanding){
- user->outstanding -= payment;
- paid=0;
- }
- else
- printf("You entered too much!");
- } while (paid);
- printf("Your new balance is %.2f\n",user->outstanding);
- }
- void Shoppermenu (Usertype *user){
- int exit = 1,input;
- do {
- printf("Shopper Menu \n");
- printf("Choose an option \n");
- printf("Modify User Info: 1 \n");
- printf("Browse All Products: 2 \n");
- printf("Browse Products by Category: 3 \n");
- printf("Browse Products on Sale: 4 \n");
- printf("Add to Cart: 5 \n");
- printf("View Cart: 6 \n");
- if (user->outstanding != 0.00)
- printf("Settle Outstanding Balance: 7 \n");
- printf("Log out: 8 \n");
- scanf ("%d",&input);
- switch (input) {
- case 1: Modifyinfo (user); break;
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7: Settlebalance (user); break;
- case 8: exit = 0;
- }
- } while (exit);
- }
- void displayAll (ptrUser pUser){
- while (pUser != NULL) {
- printf("%s\n",pUser->username);
- pUser=pUser->pNext;
- }
- }
- void freeAll (ptrUser *pUser) {
- ptrUser pTemp;
- while ((*pUser) != NULL){
- pTemp= *pUser;
- *pUser = (*pUser)->pNext;
- free(pTemp);
- }
- }
- void displayAllRecur (ptrUser pUsers) {
- if (pUsers!=NULL){
- printf("%s\n",pUsers->username);
- displayAllRecur (pUsers->pNext);
- }
- }
- ptrUser search (ptrUser pFirst, Char username){ // ADD TO CODE
- ptrUser pRun;
- pRun = pFirst;
- while (pRun != NULL && strcmp(username,pRun->username) != 0)
- pRun = pRun->pNext;
- return pRun;
- }
- void deleteNode (ptrUser *pFirst, Char username){ // ADD TO CODE
- ptrUser pFind,pRun;
- if (*pFirst == NULL)
- printf("List is empty");
- else {
- pFind = search (*pFirst,username);
- if (pFind == NULL)
- printf("%s is not in the list\n",username);
- else {
- if (pFind == *pFirst){
- *pFirst == (*pFirst)->pNext;
- }
- else {
- pRun = *pFirst;
- while (pRun->pNext != pFind)
- pRun = pRun->pNext;
- pRun->pNext = pFind->pNext;
- }
- free(pFind);
- pFind = NULL;
- }
- }
- int main () {
- int exit=1,input;
- Usertype user;
- ptrUser pUsers = NULL,
- pNew, pLast,
- pRun, pTrail;
- Char input;
- do {
- printf("Main Menu \n");
- printf("Choose an option: \n");
- printf("Log-in : 1\n");
- printf("Sign-up : 2\n");
- scanf("%d",&input);
- switch (input){
- case 1: if (login (&user)){
- if (user.type == 's' || user.type == 'S')
- Shoppermenu (&user);
- else if (user.type == 'A')
- Adminmenu (&user,&exit);
- } break;
- case 2: pNew = malloc(sizeof(Usertype)); // ADD TO CODE
- Signup (pNew);
- pNew->pNext = NULL ;
- if (pUsers == NULL)
- pUsers = pNew;
- else if (strcmp (pUsers->username, pNew->username)>0){
- pNew->pNext = pUsers;
- pUsers = pNew;
- }
- else {
- pRun = pUsers ;
- while (pRun != NULL && strcmp(pRun->username, pNew->username) < 0) {
- pTrail = pRun;
- pRun = pRun->pNext;
- }
- pTrail->pNext = pNew;
- pNew->pNext=pRun;
- }
- break;
- case 3: displayAllRecur (pUsers); break;
- case 4: freeAll(&pUsers);
- case 5: scanf("&s",&input);
- deleteNode (&pUsers,input);
- }
- } while (exit);
- return 0;
- }
- /* while (pUsers != NULL) { // ADD TO CODE
- pTemp = pUsers;
- pUsers = pUsers->pNext;
- }
- */
- /* ptrUser pRun
- pNew = malloc(sizeof(Usertype));
- Signup (pNew);
- pNew->pNext = NULL ;
- if (pUsers == NULL)
- pUsers = pNew;
- else
- pLast->pNext = pNew;
- pLast=pNew;
- break;
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement