Typedef Struct Item: #Include #Include #Include #Include #Include
Typedef Struct Item: #Include #Include #Include #Include #Include
h> typedef struct item { char name[100]; int quantity; float price; struct item *next; struct item *prev; } IT; int cnt=0; IT *head,*tail; int displayMenu(); IT *find_item(char *NAME); int pass(); void print(); void del_item(IT *p); void insert_item(); void change_item(); void sell_item(); void getType(int choice);
int main() { int trial=3,x; while(trial--) { x=pass(); if(x) break; } if(trial==-1) { printf("\nSorry you ended up giving wrong credentials many times.\nThe program is terminating...\n\n"); return 0; } int choice,i; while(1) { choice=displayMenu(); getType(choice); } return 0; } int pass() { FILE *fp_n,*fp_p;
int flag1=0,flag2=0,x,i,sp,dig; fp_n=fopen("name.txt","r+"); fp_p=fopen("pass.txt","r+"); char name[20],pass[20],t,name_f[20],pass_f[20],*p; fscanf(fp_n,"%s",name_f); fscanf(fp_p,"%s",pass_f); if(fp_n==NULL||fp_p==NULL) { fp_n=fopen("name.txt","w"); fp_p=fopen("pass.txt","w"); printf("Get ready for first time setup of your software.\n"); printf("Enter your desired username\t:\t"); gets(name); fflush(stdin); fputs(name,fp_n); printf("\nPlease use special character, digits, and atlest 8 character long password for your safety purpose.\n"); do{ i=0,sp=0,dig=0; printf("Enter your desired password\t:\t"); while(1) { t=getch(); if(t=='!'||t=='@'||t=='#'||t=='$'||t=='%'||t=='^'|| t=='&'||t=='*'||t=='`'||t==';'||t=='/') sp=1; if(t=='1'||t=='2'||t=='3'||t=='4'||t=='5'||t=='6'|| t=='7'||t=='8'||t=='9'||t=='0') dig=1; if(t==13) break; printf("*"); pass[i++]=t; fflush(stdin); } if(!dig||!sp||i<8) printf("\nNecessary requiremnets don't match!! Please try again..\n"); }while(i<8||!sp||!dig); pass[i]='\0'; fputs(pass,fp_p); printf("\nYour system is ready to use.\n"); x=1; system("pause"); } else { printf("Enter your username\t:\t"); gets(name); if(strcmp(name,name_f)==0) flag1=1; printf("\nEnter your password\t:\t"); int i=0; while(1) { t=getch(); if(t==13) break; printf("*"); pass[i++]=t; } pass[i]='\0'; if(strcmp(pass,pass_f)==0) flag2=1;
if(!flag1||!flag2) { printf("\n\nUser name or password doesn't match!!\n"); x=0; } else x=1; } fclose(fp_n); fclose(fp_p); return x; } int displayMenu() { int choice=1,MAX_OPTION=4; char input; do { system("cls"); printf("\n==========USE UP-DOWN ARROW KEY TO SELECT===========\n\n"); if(choice==1) printf(" ==> 1. S T O C K I T E M.\n"); else printf("\t 1. Stock Item.\n"); if(choice==2) printf(" ==> 2. C H A N G E I T E M.\n"); else printf("\t 2. Change Item.\n"); if(choice==3) printf(" ==> 3. S E L L I T E M.\n"); else printf("\t 3. Sell Item.\n"); if(choice==4) printf(" ==> 4. S H O W I N V E N T O R Y.\n"); else printf("\t 4. Show Inventory.\n"); if(choice==0) printf(" ==> 0. E X I T.\n"); else printf("\t 0. Exit.\n"); input=getch(); if(input==72) choice--; else if(input==80) choice++; if(choice>MAX_OPTION) choice=0; else if(choice<0) choice=MAX_OPTION; } while(input!=13); return choice; } void getType(int choice) { switch(choice) { case 1:
while(1){ insert_item(); level: printf("Do you want to add more(y/n) : "); char ch; scanf("%c",&ch); getchar(); if(ch=='y' || ch=='Y'); else if(ch=='n' || ch=='N')break; else{ printf("\n\twrong command\n\n"); goto level; } } break;
} case 2: change_item(); break; case 3: sell_item(); break; case 4: print(); break; default: exit(0); }
void print() { double total=0; if(!cnt) printf("\nYour Inventory Is Currently Empty.\n"); else { IT *temp=head; printf("Item Name\tItem Amount\tSale Price\n"); printf("=================================================\n"); while(temp!=NULL) { printf("%s\t\t%d\t\t%10.2f\n",temp->name,temp>quantity,temp->price); total+=temp->price*temp->quantity; temp=temp->next; } printf("=================================================\n"); printf("Total Inventory Worth\t\t%10.2lf BDT\n",total); } printf("\n(Press any key to continue)\n"); getch(); } void del_item(IT *p) {
if(cnt==1) { free(p); head=NULL; } else if(cnt==2) { if(p==head) { free(p); head=tail; head->prev=NULL; } else { free(p); head->next=NULL; tail=head; } } else if(cnt>=3) { if(p==head) { head=head->next; free(p); } else { p->prev->next=p->next; p->next->prev=p->prev; free(p); } } cnt--;
void insert_item() { int flag=1; IT *name; if(head==NULL) { head=(IT *)malloc(sizeof(IT)); printf("\n\nWrite down the name of the product :\t\t"); scanf("%s",head->name); printf("Write down the quantity of the product :\t"); scanf("%d",&head->quantity); printf("Write down the price of the product :\t\t"); scanf("%f",&head->price); head->next=NULL; head->prev=NULL; tail=head; } else {
IT *temp=(IT *)malloc(sizeof(IT)); printf("\nWrite down the name of the product :\t\t"); gets(temp->name); if((name=find_item(temp->name))==NULL) { printf("Write down the quantity of the product :\t"); scanf("%d",&temp->quantity); printf("Write down the price of the product :\t\t"); scanf("%f",&temp->price); temp->next = NULL; temp->prev = tail; tail->next = temp; tail = tail->next; } else { printf("\nThis Item Already Exists in Your Inventory!!\n\n"); free(temp); flag=0; } } cnt++; fflush(stdin); if(flag) printf("\nYour item has been successfully loaded up in your inventory!!\n\n"); } IT *find_item(char *NAME) { IT *temp=head; while(temp!=NULL) { if(strcmp(NAME,temp->name)==0)return temp; else temp=temp->next; } return NULL; } void change_item() { int flag=1; if(!cnt) { printf("\nYou have no item in your inventory to update its information!!\n"); flag=0; } else { printf("Showing Your Latest Inventory.\n"); print(); printf("\nEnter the Item Name to Update Its Information :\t"); char NAME[100]; scanf("%s",NAME); IT *p;
} if(flag) printf("\nYour Desired Item Information Has Been Successfully Updated.\n"); getch(); } void sell_item() { int flag=1; if(!cnt) { printf("You have nothing to sell.\n"); flag=0; } else { char NAME[100]; printf("Enter the item name that you want to sell :\t"); scanf("%s",NAME); fflush(stdin); IT *p; if( (p=find_item(NAME))!=NULL ) { int q; printf("How many items do you want to sell? :\t\t"); scanf("%d",&q); if(p->quantity-q>=0)
if( (p=find_item(NAME))!=NULL ) { char ch; printf("Do you want to change the name(y/n) : "); fflush(stdin); scanf("%c",&ch); fflush(stdin); if(ch>='A' && ch<='Z')ch=ch-'A'+'a'; if(ch=='y') { printf("Enter your Desired name : "); scanf("%s",p->name); fflush(stdin); } printf("Do you change the price(y/n) : "); scanf("%c",&ch); if(ch>='A' && ch<='Z')ch=ch-'A'+'a'; if(ch=='y') { printf("Enter your Desired price : "); scanf("%f",&p->price); } fflush(stdin); } else { printf("\nItem Doas not exists\n"); flag=0; }
{ } else { }
printf("Item Doas not exists\n"); flag=0; } } fflush(stdin); if(flag) printf("\nThe Product Has Been Sold Successfully.\n"); getch();