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)

Hackerrank > SQL > Basic Select

Select
01-Select All
Given a City table, whose fields are described as +-------------+----------+ | Field       | Type     | +-------------+----------+ | ID          | int(11)  | | Name        | char(35) | | CountryCode | char(3)  | | District    | char(20) | | Population  | int(11)  | +-------------+----------+
write a query that will fetch all columns for every row in the table.

My Solution
SELECT*FROM city;
---------------------------------------------------------------------------------
02-Select by ID
Given a City table, whose fields are described as

Zoho Puzzle Questions With Answers

Measuring Time Logic Puzzle You are given with two ropes with variable width. However if we start burning both the ropes, they will burn at exactly same time i.e. an hour. The ropes are non-homogeneous in nature. You are asked to measure 45 minutes by using these two ropes.

How can you do it?

Please note that you can’t break the rope in half as it is being clearly stated that the ropes are non-homogeneous in nature.
Answer & Explanation Solution: 45 minutes

Explanation :
All you have to do is burn the first rope from both the ends and the second rope from one end only simultaneously. The first rope will burn in 30 minutes (half of an hour since we burned from both sides) while the other rope would have burnt half. At this moment, light the second rope from the other end as well. Where, the second rope would have taken half an hour more to burn completely, it will take just 15 minutes as we have lit it from the other end too.

Thus you have successfully calculated 30+15 = 45 minutes …