Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This source code is written in Linux
- some syntax will not be recognized
- by Windows compiler. If you faced a
- problem, please review and fix the code
- yourself.
- SESSIONS VARIABLES
- - temporary and will be cleared after the session is over
- TRANSACTIONS VARIABLES
- - record sessions data
- */
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- #ifdef linux
- #define CLS_NAME "clear"
- #include <ncurses.h>
- #else
- #define CLS_NAME "cls"
- #endif
- // GLOBAL VARIABLES DECLARATIONS
- /*
- itemQuantity[][0] = Item Qty in inventory
- itemQuantity[][1] = Item Qty in a session
- itemQuantity[][2] = Item Qty in all transactions
- item_Price_Profit[][0] = Item Price
- item_Price_Profit[][1] = Item Profit
- */
- int totalItem=13,itemQuantity[100][3]={8,0,0,9,0,0,11,0,0,0,0,0,20,0,0,22,0,0,16,0,0,18,0,0,9,0,0,7,0,0,5,0,0,12,0,0,0,0,0},sessionQuantity,sessionNumber=1,hiddenItemQuantity[]={},itemId;
- float item_Price_Profit[100][2]={1,0.5,2,0.2,3,0.2,4,0.2,5,0.5,6,0.8,7,0.5,8,0.2,9,0.2,10,0.2,11,0.5,12,0.8,13,0.9},sesionTotalPrice,transactionTotalProfit=0,transactionTotalPrice=0,sessionPayment;
- char itemName[100][30]={"STABILO PENCIL 2B","STABILO PEN 0.5","STABILO ERASER","STABILO RULER","STABILO TEST PAD","STABILO BOOK","STABILO SCISSORS","STABILO SHARPENER","STABILO GLUE","STABILO CHALK","STABILO MARKER PEN","OXFORD DICTIONARY","STABILO HIGHLIGHTER"},dummy,str[3],customer[15];
- // FUNCTIONS PROTOTYPES
- void showTime();void menuSelection(int);void newTransaction();void allTransactions();void addItem();void reviewCart();void removeItem();void sessionReceipt();void gotoMenu();void calcQuantity(int);void allStocks();void addStock();void addStockItem();void editStockItem();void hideStockItem();void getItemId(int);void getItemQty();void getNewItemProfit(int);void getAuth();int validateNum();void validateItemName(int,int);
- // DISPLAY MENU
- main(){
- int choice;
- system(CLS_NAME);
- showTime();
- printf("Assalamualaikum\n\nMAIN MENU :\n\n 1 - New Transaction\n 2 - All Transactions\n 3 - Stocks (Manager ONLY)\n\nPlease select an option : ");
- scanf(" %s",str);
- if(validateNum(str)==1) choice = atoi(str);
- else main();
- if(choice>0&&choice<4) menuSelection(choice);
- else main();
- }
- // SHOW CURRENT DATE AND TIME
- void showTime(){
- time_t mytime;
- mytime = time(NULL);
- printf("---------------------%24s\n",ctime(&mytime));
- }
- // MENU SELECTION
- void menuSelection(int choice){
- if(choice!=6) system(CLS_NAME);
- switch(choice){
- case 1 : sesionTotalPrice = 0;
- system(CLS_NAME);
- printf("CUSTOMER NAME : ");
- scanf(" %15[^\n]",customer);
- for(choice=0;choice<15;choice++) customer[choice] = toupper(customer[choice]);
- newTransaction();break;
- case 2 : allTransactions();break;
- case 3 : getAuth(); break;
- //allStocks(); break;
- case 4 : addStock();break;
- case 5 : addStockItem();break;
- case 6 : getItemId(3);
- editStockItem();break;
- case 7 : getItemId(4);
- hideStockItem();break;
- case 8 : main();break;
- default : main();break;
- }
- }
- // SHOWS AVAILABLE ITEMS LIST INCLUDING OUT OF STOCK ITEMS
- void newTransaction(){
- int i;
- system(CLS_NAME); //system("cls");
- printf("INVENTORY SESSION NO : %3d\n",sessionNumber);
- printf("CUSTOMER: %-15s TOTAL : RM %6.2f\n\n",customer,sesionTotalPrice);
- printf("ID Item Qty Price\n");
- printf("-------------------------------------------\n");
- for (i=0;i<totalItem;i++){
- if (itemQuantity[i][0]==-1) ;
- else if (itemQuantity[i][0]==0) printf("%-2d %-27sOUT OF STOCK\n",i,itemName[i]);
- else printf("%-2d %-28s%3d %6.2f\n",i,itemName[i],itemQuantity[i][0],item_Price_Profit[i][0]);
- }
- printf("-------------------------------------------\n");
- printf("-1 VIEW CART\n99 CHECK OUT\n");
- addItem();
- }
- // ADD ITEM TO CART
- void addItem(){
- getItemId(1);
- if (itemId==-1) reviewCart();
- if (itemId==99) sessionReceipt();
- if (itemId>=totalItem) newTransaction();
- if (itemQuantity[itemId][0]==0||itemQuantity[itemId][0]==-1) {
- printf("--UNAVAILABLE--\n");
- sleep(1);
- newTransaction();
- }
- if (itemQuantity[itemId][0]==1) {
- itemQuantity[itemId][0] --;
- itemQuantity[itemId][1] ++;
- itemQuantity[itemId][2] ++;
- sesionTotalPrice += item_Price_Profit[itemId][0];
- newTransaction();
- }
- getItemQty();
- if (sessionQuantity<0){
- printf("--INVALID--\n");
- sleep(1);
- newTransaction();
- }
- if (sessionQuantity>itemQuantity[itemId][0]){
- printf("--INSUFFICIENT--\n");
- sleep(1);
- newTransaction();
- }
- calcQuantity(1);
- newTransaction();
- }
- // DISPLAY ALL ITEMS ADDED TO CART
- void reviewCart(){
- int i;
- system(CLS_NAME); //system("cls");
- printf("INVENTORY SESSION NO : %3d\n",sessionNumber);
- printf("CUSTOMER: %-15s TOTAL : RM %6.2f\n\n",customer,sesionTotalPrice);
- printf("ID Item Price Amount\n");
- printf("-------------------------------------------\n");
- for (i=0;i<totalItem;i++){
- if (itemQuantity[i][1]!=0)
- printf("%-2d %2dx%-24s %5.2f %6.2f\n",i,itemQuantity[i][1],itemName[i],item_Price_Profit[i][0],item_Price_Profit[i][0]*itemQuantity[i][1]);
- }
- printf(" ------\n");
- printf("SUB TOTAL %6.2f\n",sesionTotalPrice);
- printf("-------------------------------------------\n");
- printf("-1 BROWSE INVENTORY\n99 CHECK OUT\n");
- removeItem();
- }
- // REMOVE ITEM FROM CART
- void removeItem(){
- getItemId(2);
- if (itemId==-1) newTransaction();
- if (itemId==99) sessionReceipt();
- if (itemId>=totalItem) removeItem();
- if (itemQuantity[itemId][1]==1) {
- itemQuantity[itemId][0] ++;
- itemQuantity[itemId][1] --;
- itemQuantity[itemId][2] --;
- sesionTotalPrice -= item_Price_Profit[itemId][0];
- reviewCart();
- }
- if (itemQuantity[itemId][1]==0||itemQuantity[itemId][0]==-1) {
- printf("--ITEM INEXISTENT--\n");
- sleep(1);
- reviewCart();
- }
- getItemQty();
- if (sessionQuantity<0){
- printf("--INVALID--\n");
- sleep(1);
- reviewCart();
- }
- if (sessionQuantity>itemQuantity[itemId][1]){
- printf("--INSUFFICIENT--\n");
- sleep(1);
- reviewCart();
- }
- calcQuantity(-1);
- reviewCart();
- }
- /*GET TARGET ITEM ID :
- 1=TO BE ADDED TO CART,
- 2=TO BE REMOVED FROM CART,
- 3=TO BE EDITED,
- 4=TO BE REMOVED,*/
- void getItemId(int i){
- switch(i){
- case 1 : printf("\nPlease enter Item ID to add to cart : ");break;
- case 2 : printf("\nPlease enter Item ID to hide from cart : ");break;
- case 3 : printf("ID of item to be edited : ");break;
- case 4 : printf("ID of item to be hide/unhide : ");break;
- case 5 : printf("ID of item to be restocked : ");break;
- }
- scanf(" %s",str);
- if(validateNum(str)==1) itemId = atoi(str);
- else getItemId(i);
- if(i==3 && itemId>=totalItem) {
- printf("Item does not exist. Create? Y to confirm : ");
- scanf(" %c",&dummy);
- if(toupper(dummy)!='Y') allStocks();
- itemId = totalItem++;
- }
- }
- // GET TARGET ITEM QTY
- void getItemQty(){
- printf("Please enter Item Quantity : ");
- scanf(" %s",str);
- if(validateNum(str)==1) sessionQuantity = atoi(str);
- else getItemQty();
- }
- // DISPLAY SESSION RECEIPT - ONE TIME ONLY
- void sessionReceipt(){
- int i;
- system(CLS_NAME); //system("cls");
- showTime();
- printf(" Receipt number : %3d\n",sessionNumber);
- printf("---------------------------------------------\n");
- printf("ID Item Price Amount\n");
- for (i=0;i<totalItem;i++){
- if (itemQuantity[i][1]!=0)
- printf("%-2d %2dx%-24s %6.2f %6.2f\n",i,itemQuantity[i][1],itemName[i],item_Price_Profit[i][0],item_Price_Profit[i][0]*itemQuantity[i][1]);
- if(sessionPayment!=0) itemQuantity[i][1]=0;
- }
- printf(" -------\n");
- printf("SUB TOTAL %7.2f\n",sesionTotalPrice);
- printf(" -------\n");
- if(sessionPayment==0){
- printf("CASH : ");
- scanf(" %s",str);
- if(validateNum(str)==1) sessionPayment = atoi(str);
- else sessionReceipt();
- sessionReceipt();
- }else printf("CASH : %.2f\n",sessionPayment);
- printf("CHANGE : %.2f\n",sessionPayment-sesionTotalPrice);
- printf("\n---------------------------------------------\n");
- printf("-------------THANK YOU FOR COMING------------\n");
- printf("---------------------------------------------\n");
- transactionTotalPrice += sesionTotalPrice;
- sessionNumber++;
- sessionPayment=0;
- gotoMenu();
- }
- // DISPLAY TOTAL TRANSACTIONS
- void allTransactions(){
- int i;
- system(CLS_NAME); //system("cls");
- showTime();
- printf("ALL TRANCTIONS\n\n");
- printf("ID Item Price Profit\n");
- printf("---------------------------------------------\n");
- for (i=0;i<totalItem;i++){
- if (itemQuantity[i][2]!=0)
- printf("%-2d %2dx%-24s%7.2f %7.2f\n",i,itemQuantity[i][2],itemName[i],item_Price_Profit[i][0]*itemQuantity[i][2],item_Price_Profit[i][1]*itemQuantity[i][2]);
- transactionTotalProfit += item_Price_Profit[i][1]*itemQuantity[i][2];
- }
- printf(" ------- -------\n");
- printf("TOTAL %7.2f %7.2f\n",transactionTotalPrice,transactionTotalProfit);
- printf(" ------- -------\n");
- printf("TOTAL TRANSACTIONS %7d\n",sessionNumber-1);
- printf("---------------------------------------------\n");
- transactionTotalProfit = 0;
- gotoMenu();
- }
- // PAUSE PROGRAM AND CONTINUE WITH ENTER
- void gotoMenu(){
- printf("\n\nPress Enter to continue...");
- dummy = getchar();
- while (getchar() != '\n');
- main();
- }
- // CALCULATE QUANTITY IN SESSIONS AND TRANSACTIONS
- void calcQuantity(int i){
- itemQuantity[itemId][1] += (i*sessionQuantity);
- itemQuantity[itemId][2] += (i*sessionQuantity);
- itemQuantity[itemId][0] -= (i*sessionQuantity);
- sesionTotalPrice += (i*sessionQuantity*item_Price_Profit[itemId][0]);
- }
- // DISPLAY ALL STOCKS TABLE
- void allStocks(){
- int i,choice;
- system(CLS_NAME);
- printf("INVENTORY\n");
- printf("ID Item Qty Price Profit\n");
- printf("+----------------------------------------------------+\n");
- for (i=0;i<totalItem;i++){
- if(itemQuantity[i][0]==-1) printf("|%-2d %-31sITEM HAS BEEN HID|\n",i,itemName[i]);
- else printf("|%-2d %-28s%3d %5.2f %6.2f|\n",i,itemName[i],itemQuantity[i][0],item_Price_Profit[i][0],item_Price_Profit[i][1]);
- }
- printf("+----------------------------------------------------+\n");
- printf("MENU :\n\n 1 - Add Stock\n 2 - New Item\n 3 - Edit Item\n 4 - Hide/Unhide Item\n 5 - Main Menu\n\nPlease select an option : ");
- scanf(" %s",str);
- if(validateNum(str)==1) choice = atoi(str);
- else allStocks();
- choice += 3;
- if(choice>3&&choice<9) menuSelection(choice);
- else allStocks();
- }
- // ADD STOCK
- void addStock(){
- getItemId(5);
- if(itemQuantity[itemId][0]==-1){
- printf("UNHIDE ITEM BEFORE ADD STOCK\n");
- sleep(1);
- allStocks();
- }
- getItemQty();
- itemQuantity[itemId][0] += sessionQuantity;
- allStocks();
- }
- // ADD NEW ITEM
- void addStockItem(){
- int i;
- system(CLS_NAME);
- printf("New Item\n\n");
- printf("New Item Name : ");
- scanf(" %30[^\n]", itemName[totalItem]);
- for (i=0;i<30;i++) itemName[totalItem][i] = toupper(itemName[totalItem][i]);
- printf("New Item Price : ");
- scanf(" %f",&item_Price_Profit[totalItem][0]);
- getNewItemProfit(totalItem);
- printf("New Item Qty : ");
- scanf(" %d",&itemQuantity[totalItem][0]);
- itemQuantity[totalItem][1]=0;
- itemQuantity[totalItem][2]=0;
- validateItemName(1,totalItem);
- ++totalItem;
- allStocks();
- }
- // EDIT ITEM
- void editStockItem(){
- int i;
- system(CLS_NAME);
- printf("Name : %s\n",itemName[itemId]);
- printf("Price : %.2f\n",item_Price_Profit[itemId][0]);
- printf("Profit : %.2f\n",item_Price_Profit[itemId][1]);
- printf("Qty : %d\n",itemQuantity[itemId][0]);
- printf("\nEdit \n\n 1 - Name\n 2 - Price\n 3 - Profit\n 4 - Quantity\n 5 - Back to Stocks\n\nPlease select what to edit: ");
- scanf(" %s",str);
- if(validateNum(str)==1) i = atoi(str);
- else editStockItem();
- switch(i){
- case 1 : printf("New Item Name : ");
- strcpy(itemName[99],itemName[itemId]);
- scanf(" %30[^\n]", itemName[itemId]);
- for (i=0;i<30;i++) itemName[itemId][i] = toupper(itemName[itemId][i]);
- break;
- case 2 : printf("New Item Price : ");
- scanf(" %f",&item_Price_Profit[itemId][0]);break;
- case 3 : getNewItemProfit(itemId);break;
- case 4 : printf("New Item Qty : ");
- scanf(" %d",&itemQuantity[itemId][0]);break;
- case 5 : allStocks();break;
- default : break;
- }
- validateItemName(0,itemId);
- editStockItem();
- }
- // MAKE SURE PROFIT NOT MORE THAN HALF PRICE
- void getNewItemProfit(int i){
- printf("New Item Profit : ");
- scanf(" %f",&item_Price_Profit[i][1]);
- while(item_Price_Profit[i][1]>item_Price_Profit[i][0]/2){
- printf("Profit more than half of item's price. Please reduce!\n New Item Profit : ");
- scanf(" %f",&item_Price_Profit[i][1]);
- }
- }
- // HIDE ITEM FROM MENU
- void hideStockItem(){
- if(itemQuantity[itemId][0]!=-1) {
- hiddenItemQuantity[itemId]=itemQuantity[itemId][0];
- itemQuantity[itemId][0]=-1;
- }
- else itemQuantity[itemId][0]=hiddenItemQuantity[itemId];
- allStocks();
- }
- // ONLY AUTHORIZED USER CAN VIEW ALL STOCKS
- void getAuth(){
- int i;
- char user[][10]={"manager","134562"},authdummy[10]={};
- system(CLS_NAME);
- printf("USER : ");
- scanf(" %s",&*authdummy);
- for(i=0;i<10;i++){
- if(authdummy[i]!=user[0][i]){
- printf("ONLY A MANAGER CAN VIEW THIS PAGE\n");
- sleep(1);
- main();
- }
- }
- printf("PASS : ");
- fflush(stdin); // experimental
- for(i=0;i<6;i++){
- authdummy[i] = getch();
- if(authdummy[i]=='\n') break;
- printf("*");
- }
- for(i=0;i<6;i++){
- if(authdummy[i]!=user[1][i]){
- printf("\nWRONG PASSWORD\n");
- sleep(1);
- main();
- }
- }
- allStocks();
- }
- // MAKE SURE ITEM NAMES' ARE UNIQUE
- void validateItemName(int add,int id){
- int i;
- for (i = 0; i < totalItem; ++i)
- {
- if (!strcmp(itemName[id],itemName[i]) && id!=i){
- if(add==1){
- item_Price_Profit[i][0] = item_Price_Profit[id][0];
- item_Price_Profit[i][1] = item_Price_Profit[id][1];
- itemQuantity[i][0] += itemQuantity[id][0];
- for (i = 0; i < 30; ++i)itemName[id][i] = '\0';
- item_Price_Profit[id][0] = 0;
- item_Price_Profit[id][1] = 0;
- itemQuantity[id][0] = 0;
- --totalItem;
- }else{
- printf("Item Exists\n");
- sleep(1);
- strcpy(itemName[id],itemName[99]);
- editStockItem();
- }
- }
- }
- }
- // MAKE SURE INPUTS ARE FOOLPROOF
- int validateNum(char *p){
- if (*p) {
- char c;
- while ((c=*p++)) if (!isdigit(c)&&c!='-') return 0;
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement