Skip to main content

e-Shop

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

#define N 3

struct Product{
 char name[20];
 int price;
 int discount;
 int selected;
}product[N];

struct Customer{
 char name[20];
 struct Product list[N];
 char credit[20];
 double amount;
}cust;

void intialProductList();
void displayList();

int getDiscount(){
 srand(time(NULL));
 return (rand()%6+1);

}

double discountPrice(struct Product node){
 double temp;
 //printf("\n>>%f",k);
 temp = ((double)node.price/(double)100) * node.discount ;
 //printf("---%lf",temp);
 return temp;
}


void display(struct Product product){
 printf("\n-----------------------------------------");
 printf("\n\n\tName :: %s",product.name);
 printf("\n\tMRP Price  :: %d",product.price);
 printf("\n\tDiscount:: %d %%",product.discount);
 printf("\n\tDiscount Price :: %.2lf",discountPrice(product));
 printf("\n-----------------------------------------");
 getch();
}

void displayList(){
int i;
 for(i=0;i<N;i++)
     display(product[i]);
}
void displayCart(char *name){
int i,k;
k = 0;
strcpy(cust.name,name);
cust.amount = 0;
 for(i=0;i<N;i++)
  if( product[i].selected == 1){
    display(product[i]);
    printf("\n Do you want %s (press 1 or 0) :: ",product[i].name);
     scanf("%d",&product[i].selected);
   if(product[i].selected){
    cust.list[k++] = product[i];
    cust.amount +=   discountPrice(product[i]);
   }
  }
}

void initalProductList(){
 FILE *fptr;
 int i;
 clrscr();
 if( (fptr=fopen("tempfile.txt","r")) == NULL )
  printf("OOPSS ......File cannot be open ");
 else{
  for(i=0;i<N;i++){
   fscanf(fptr,"%s",product[i].name);
   fscanf(fptr,"%d",&product[i].price);
   product[i].discount = getDiscount();
   product[i].selected = 0;
  }
 }
 fclose(fptr);

}

int isValid(char *name, char *password){
 FILE *fptr;
 char n[20],p[20];
 clrscr();
 if( (fptr=fopen("log.txt","r")) == NULL )
  printf("\n\n\tOOPSS ......Login File Missing");
 else{
      while(!feof(fptr)){
  fscanf(fptr,"%s",n);
  fscanf(fptr,"%s",p);
  if( (strcmp(n,name)==0) && (strcmp(p,password)==0) ){
   fclose(fptr);
   return 1;
  }
      }
 }
fclose(fptr);
return 0;
}

int isValidProduct(char *temp){
int i;
 for(i=0;i<N;i++){
  if( (strcmpi(product[i].name, temp)) == 0 )
   return i;
 }
return -1;
}

int sumDigits(int n){
 //printf("< %d > ",n%9);
  if((n%9)==0)
  return 9;
return (n%9);
}

int chartonum(char ch){

 return (ch-'0');
}


int isValidCredit(char *temp){
int i,sum;
 strrev(temp);
 //puts(temp);
 for(sum = i =0; i<strlen(temp); i++){
  if(i%2==1)
       sum += sumDigits(chartonum(temp[i]) * 2);
  else
       sum += chartonum(temp[i]);
 //printf("%3d",sum);
 }

 if( (sum%10) == 0 )
  return 1;

return 0;
}

void userInterface(char *name){
int ch,temp[20];
while(1){
 printf("\n****************************");
  printf("\n 1. Search the Product");
  printf("\n 2. Display Product List");
  printf("\n 3. Display Add to Cart List");
  printf("\n 4. Payment");
  printf("\n 5. Logout");
 printf("\n****************************");

 printf("\n\n\tEnter your Choice :: ");
  scanf("%d",&ch);

 switch(ch){
  case 1:
   printf("Enter the Product Name to be Search :: ");
    scanf("%s",temp);
    ch = isValidProduct(temp);
    if(ch > -1){
    display(product[ch]);
    printf("\n Do you want %s (press 1 or 0) :: ",product[ch].name);
     scanf("%d",&product[ch].selected);
    }
    else
    printf("\n\n Sry your Searching Items is not Found");
    break;
  case 2:
   displayList();
   break;
  case 3:
   displayCart(name);
   break;
  case 4:
   printf("\n\nEnter the Credit Card Number (16 Digits) :: ");
    scanf("%s",temp);
   if(isValidCredit(temp))
    printf("\n\n\t\tVaild Credit Card ");
   else
    printf("\n\n\t\tNOT Vaild Credit Card ");
   break;
  case 5:
   clrscr();
   printf("\n\n\n\n\n\t\t\tLog Out Successfully....");
   getch();
   exit(0);
 }
}
}

void getLogin(){
char name[20],pwd[20],ch,i=0;
 printf("\n\t-------------------------------------------");
 printf("\n\n\n\t\tEnter the Username :: ");
  scanf("%[^'\n']s",name);
 printf("\n\n\n\t\tEnter the Password :: ");
  while( (ch=getch()) != 13 ){
   printf("*");
   pwd[i++] = ch;
  }
  pwd[i] = '\0';
 printf("\n\t-------------------------------------------\n");
       // puts(name);puts(pwd);
 getch();

 if( isValid(name,pwd) ){
  printf("\n\n\tSuccessfully Login ....");
  //displayList();
  userInterface(name);

 }
 else{
  clrscr();
  printf("\n\n\tInvailed Username or Password");
  getch();
  getLogin();
 }
}


int main(){
clrscr();
     initalProductList();
     getLogin();
getch();
return 0;
}









Comments

Popular posts from this blog

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)

C Questions

C Questions
C Questions

Note : All the programs are tested under Turbo C/C++ compilers.
It is assumed that,
Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).
Predict the output or error(s) for the following:

void main()
{
int const * p=5; printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
main()
{
char s[ ]="man"; int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer: mmmm
aaaa nnnn
Explanation

Zoho Interview | Set 1 (Advanced Programming Round)

Third Round: (Advanced Programming Round) Here they asked us to create a “Railway reservation system” and gave us 4 modules. The modules were:
    1. Booking
    2. Availability checking
    3. Cancellation
    4. Prepare chart
We were asked to create the modules for representing each data first and to continue with the implementation phase.

 My Solution :

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<iostream.h>#include<time.h>#include<iomanip.h>#include<fstream.h>char f[10]="f";char s[10]="s";int addr,ad,flag,f1,d,m,i,amt;float tamt; class login {public:char id[100];char pass[100];char*password;void getid(){ cout<<"Enter your id:";gets(id); password=getpass("Enter the password:");strcpy(pass,password);}void displayid(){ cout<<"Id:";puts(id); cout<<"Password:";puts(pass);}}; class detail {public:in…