You are on page 1of 4

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

h> #define NAME_LEN 30 #define MAX 100 //Project for 4206, student Id: 0122479 name: Ahmed Tholaal struct Item { char name [NAME_LEN]; int quantity; float cost; }; int menu_func(), authenticate(); struct Item* add(struct Item*, struct Item*); struct Item* get_item(char*,struct Item*); modify(struct Item*), init(struct Item*), report(struct Item*), sell(struct Item*); main() { struct Item items[MAX], *next; int choice, last=0; init(items); next = items; if(authenticate()){ while(1){ choice = menu_func(); switch(choice){ case 1: next = add(next, items); break; case 2: modify(items); break; case 3: sell(items); break; case 4: report(items); break; case 5: return; } } } } int menu_func() { int num; printf("\nPlease select an item from the menu\n");

printf("\n1. Add products\n"); printf("2. Modify\n"); printf("3. Sell\n"); printf("4. Report\n"); printf("5. Exit\n"); printf("Enter your choice: scanf("%d", &num); getchar(); return num; } init(struct Item *database){ int i; for(i=0; i< MAX; i++) (database+i)->name[0] = '\0'; } struct Item* add(struct Item *next, struct Item *beggining){ char name[NAME_LEN]; if(next-beggining != MAX){ printf("Name: "); gets(name); if(!get_item(name, beggining)) { strcpy(next->name, name); printf("\nQuantity: "); scanf("%d", &next->quantity); printf("\nCost: "); scanf("%f", &next->cost); getchar(); return next+1; } else { printf("\nItem already in database"); getch(); return next; } } else printf("\nDatabase full\n"); getch(); return next; ");

struct Item* get_item(char *name,struct Item *database) { int i; for(i=0; i<MAX; i++){ if(strcmp(name, (database+i)->name) == 0){ return database+i; } } return NULL; }

modify(struct Item *database) { char name[NAME_LEN]; struct Item *item; int quantity; if(authenticate()) { printf("\nEnter the name: "); gets(name); item = get_item(name, database); if(item){ printf("\nQuantity: "); scanf("%d", &item->quantity); } else { printf("\nItem not found\n"); getch(); } } } sell(struct Item *database) { char name[NAME_LEN]; int quantity; struct Item *item; printf("\nEnter the name: "); gets(name); item = get_item(name, database); if(item){ printf("\nQuantity: "); scanf("%d", &quantity); if(item->quantity >= quantity){ item->quantity = item->quantity - quantity; } else { printf("\nNot enough items available\n"); getch(); } } else { printf("\nItem not found\n"); getch(); } } report(struct Item *database) { int i; float sum = 0; if(authenticate()) { printf("\nName\t\tCost\tQuantity\n\n"); for(i=0; i<MAX; i++){ if((database+i)->name[0]){ printf("%s\t\t", (database+i)->name); printf("%.2f\t", (database+i)->cost);

printf("%d\t\n", (database+i)->quantity); sum = sum + (database+i)>quantity*(database+i)->cost; } } printf("\nTotal worth of inventory is %.2f.\n\n", sum); getch(); } } int authenticate() { char password[]="D3f@ult.", temp[20]; printf("Enter the password: "); gets(temp); if(strcmp(temp, password) == 0) return 1; else { printf("Wrong password!"); getch(); } return 0; }

You might also like