Advertisement
zaidhuda

Source Code

Sep 28th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.76 KB | None | 0 0
  1. /*
  2.   This source code is written in Linux
  3.   some syntax will not be recognized
  4.   by Windows compiler. If you faced a
  5.   problem, please review and fix the code
  6.   yourself.
  7.  
  8.   SESSIONS VARIABLES
  9.     - temporary and will be cleared after the session is over
  10.   TRANSACTIONS VARIABLES
  11.     - record sessions data
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <time.h>
  17. #include <ctype.h>
  18.  
  19. #ifdef linux
  20. #define CLS_NAME "clear"
  21. #else
  22. #define CLS_NAME "cls"
  23. #endif
  24.  
  25. // GLOBAL VARIABLES DECLARATIONS
  26. /*
  27.     itemQuantity[][0] = Item Qty in inventory
  28.     itemQuantity[][1] = Item Qty in a session
  29.     itemQuantity[][2] = Item Qty in all transactions
  30.     item_Price_Profit[][0] = Item Price
  31.     item_Price_Profit[][1] = Item Profit
  32. */
  33. int totalItem=13,itemQuantity[][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;
  34. float item_Price_Profit[][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;
  35. char itemName[][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;
  36.  
  37. // FUNCTIONS PROTOTYPES
  38. 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();
  39.  
  40. // DISPLAY MENU
  41. main(){
  42.     int choice;
  43.     system(CLS_NAME);
  44.     showTime();
  45.     printf("Assalamualaikum\n\nMAIN MENU :\n\n    1 - New Transaction\n    2 - All Transactions\n    3 - Stocks\n\nPlease select an option : ");
  46.     scanf("%d",&choice);
  47.     if(choice>0&&choice<4) menuSelection(choice);
  48.     else main();
  49. }
  50.  
  51. void showTime(){
  52.     time_t mytime;
  53.     mytime = time(NULL);
  54.     printf("%38s",ctime(&mytime));
  55. }
  56.  
  57. // MENU SELECTION
  58. void menuSelection(int choice){
  59.     if(choice!=6) system(CLS_NAME);
  60.     switch(choice){
  61.         case 1  :   sesionTotalPrice = 0;
  62.                     newTransaction();break;
  63.         case 2  :   allTransactions();break;
  64.         case 3  :   //getAuth(); break;
  65.                     allStocks(); break;
  66.         case 4  :   addStock();break;
  67.         case 5  :   addStockItem();break;
  68.         case 6  :   getItemId(3);
  69.                     editStockItem();break;
  70.         case 7  :   getItemId(4);
  71.                     hideStockItem();break;
  72.         case 8  :   main();break;
  73.         default :   main();break;
  74.     }
  75. }
  76.  
  77. // SHOWS ITEMS LIST
  78. void newTransaction(){
  79.     int i;
  80.     system(CLS_NAME); //system("cls");
  81.     printf("INVENTORY           SESSION NO  :       %3d\n",sessionNumber);
  82.     printf("                    TOTAL PRICE : RM %6.2f\n\n",sesionTotalPrice);
  83.     printf("ID  Item                        Qty   Price\n");
  84.     printf("-------------------------------------------\n");
  85.  
  86.     for (i=0;i<totalItem;i++){
  87.         if (itemQuantity[i][0]==-1) ;
  88.         else if (itemQuantity[i][0]==0) printf("%-2d  %-27sOUT OF STOCK\n",i,itemName[i]);
  89.         else printf("%-2d  %-28s%3d  %6.2f\n",i,itemName[i],itemQuantity[i][0],item_Price_Profit[i][0]);
  90.     }
  91.     printf("-------------------------------------------\n");
  92.     printf("-1 VIEW CART\n99 CHECK OUT\n");
  93.     addItem();
  94. }
  95.  
  96. /*GET ITEM ID :
  97.     1=TO BE ADDED TO CART,
  98.     2=TO BE REMOVED FROM CART,
  99.     3=TO BE EDITED,
  100.     4=TO BE REMOVED,*/
  101. void getItemId(int i){
  102.     switch(i){
  103.         case 1 :    printf("\nPlease enter Item ID to add to cart : ");break;
  104.         case 2 :    printf("\nPlease enter Item ID to hide from cart : ");break;
  105.         case 3 :    printf("ID of item to be edited : ");break;
  106.         case 4 :    printf("ID of item to be hide/unhide : ");break;
  107.         case 5 :    printf("ID of item to be restocked : ");break;
  108.     }
  109.     scanf("%d",&itemId);
  110.     if(i==3 && itemId>=totalItem) {
  111.         printf("Item does not exist. Create? Y to confirm : ");
  112.         fflush(stdin);
  113.         scanf("%s",&dummy);
  114.         if(toupper(dummy)!='Y') allStocks();
  115.         itemId = totalItem++;
  116.     }
  117. }
  118.  
  119. void getItemQty(){
  120.     printf("Please enter Item Quantity : ");
  121.     scanf("%d",&sessionQuantity);
  122. }
  123.  
  124. // GET ITEM ID & QTY FROM USER TO BE ADDED TO SESSIONS VARIABLES
  125. void addItem(){
  126.     getItemId(1);
  127.     if (itemId==-1) reviewCart();
  128.     if (itemId==99) sessionReceipt();
  129.     if (itemId>=totalItem) newTransaction();
  130.     if (itemQuantity[itemId][0]==0||itemQuantity[itemId][0]==-1) {
  131.         printf("--UNAVAILABLE--\n");
  132.         sleep(1);
  133.         newTransaction();
  134.     }
  135.     if (itemQuantity[itemId][0]==1) {
  136.         itemQuantity[itemId][0] --;
  137.         itemQuantity[itemId][1] ++;
  138.         itemQuantity[itemId][2] ++;
  139.         sesionTotalPrice += item_Price_Profit[itemId][0];
  140.         newTransaction();
  141.     }
  142.     getItemQty();
  143.     if (sessionQuantity<0){
  144.         printf("--INVALID--\n");
  145.         sleep(1);
  146.         newTransaction();
  147.     }
  148.     if (sessionQuantity>itemQuantity[itemId][0]){
  149.         printf("--INSUFFICIENT--\n");
  150.         sleep(1);
  151.         newTransaction();
  152.     }
  153.     calcQuantity(1);
  154.     newTransaction();
  155. }
  156.  
  157. // DISPLAY DATA STORED IN SESSIONS VARIABLES
  158. void reviewCart(){
  159.     int i;
  160.     system(CLS_NAME); //system("cls");
  161.     printf("INVENTORY           SESSION NO  :       %3d\n",sessionNumber);
  162.     printf("                    TOTAL PRICE : RM %6.2f\n\n",sesionTotalPrice);
  163.     printf("ID  Item                       Price Amount\n");
  164.     printf("-------------------------------------------\n");
  165.     for (i=0;i<totalItem;i++){
  166.         if (itemQuantity[i][1]!=0)
  167.             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]);
  168.     }
  169.     printf("                                     ------\n");
  170.     printf("SUB TOTAL                            %6.2f\n",sesionTotalPrice);
  171.     printf("-------------------------------------------\n");
  172.     printf("-1 BROWSE INVENTORY\n99 CHECK OUT\n");
  173.     removeItem();
  174. }
  175.  
  176. // GET ITEM ID & QTY FROM USER TO BE hide FROM SESSIONS VEARIABLES
  177. void removeItem(){
  178.     getItemId(2);
  179.     if (itemId==-1) newTransaction();
  180.     if (itemId==99) sessionReceipt();
  181.     if (itemId>=totalItem) removeItem();
  182.     if (itemQuantity[itemId][1]==1) {
  183.         itemQuantity[itemId][0] ++;
  184.         itemQuantity[itemId][1] --;
  185.         itemQuantity[itemId][2] --;
  186.         sesionTotalPrice -= item_Price_Profit[itemId][0];
  187.         reviewCart();
  188.     }
  189.     if (itemQuantity[itemId][1]==0||itemQuantity[itemId][0]==-1) {
  190.         printf("--ITEM INEXISTENT--\n");
  191.         sleep(1);
  192.         reviewCart();
  193.     }
  194.     getItemQty();
  195.     if (sessionQuantity<0){
  196.         printf("--INVALID--\n");
  197.         sleep(1);
  198.         reviewCart();
  199.     }
  200.     if (sessionQuantity>itemQuantity[itemId][1]){
  201.         printf("--INSUFFICIENT--\n");
  202.         sleep(1);
  203.         reviewCart();
  204.     }
  205.     calcQuantity(-1);
  206.     reviewCart();
  207. }
  208.  
  209. // DISPLAY SESSION RECEIPT - ONE TIME ONLY
  210. void sessionReceipt(){
  211.     int i;
  212.     system(CLS_NAME); //system("cls");
  213.     showTime();
  214.     printf("Receipt number : %d\n",sessionNumber);
  215.     printf("-------------------------------------\n");
  216.     printf("Item\t\tQty   Price    Amount\n");
  217.     for (i=0;i<totalItem;i++){
  218.         if (itemQuantity[i][1]!=0)
  219.             printf("%d\t\t%3d   %5.2f    %6.2f\n%s\n",i,itemQuantity[i][1],item_Price_Profit[i][0],item_Price_Profit[i][0]*itemQuantity[i][1],itemName[i]);
  220.         if(sessionPayment!=0) itemQuantity[i][1]=0;
  221.     }
  222.     printf("\n                            ---------\n");
  223.     printf("SUB TOTAL                      %6.2f",sesionTotalPrice);
  224.     printf("\n                            ---------\n\n");
  225.     if(sessionPayment==0){
  226.         printf("CASH     : ");
  227.         scanf("%f",&sessionPayment);
  228.         sessionReceipt();
  229.     }else printf("CASH     : %.2f\n",sessionPayment);
  230.     printf("CHANGE   : %.2f\n",sessionPayment-sesionTotalPrice);
  231.     printf("\n\n-------------------------------------\n");
  232.     printf("---------THANK YOU FOR COMING--------\n");
  233.     printf("-------------------------------------\n");
  234.     transactionTotalPrice += sesionTotalPrice;
  235.     sessionNumber++;
  236.     sessionPayment=0;
  237.     gotoMenu();
  238. }
  239.  
  240. // DISPLAY TOTAL TRANSACTIONS
  241. void allTransactions(){
  242.     int i;
  243.     system(CLS_NAME); //system("cls");
  244.     printf("ALL TRANCTIONS\n\n");
  245.     printf("ID  Item                        Price  Profit\n");
  246.     printf("---------------------------------------------\n");
  247.     for (i=0;i<totalItem;i++){
  248.         if (itemQuantity[i][2]!=0)
  249.             printf("%-2d %2dx%-24s %6.2f  %6.2f\n",i,itemQuantity[i][2],itemName[i],item_Price_Profit[i][0]*itemQuantity[i][2],item_Price_Profit[i][1]*itemQuantity[i][2]);
  250.         transactionTotalProfit += item_Price_Profit[i][1]*itemQuantity[i][2];
  251.     }
  252.     printf("                               ------  ------\n");
  253.     printf("TOTAL                          %6.2f  %6.2f\n",transactionTotalPrice,transactionTotalProfit);
  254.     printf("                               ------  ------\n");
  255.     printf("TOTAL TRANSACTIONS                     %6d\n",sessionNumber-1);
  256.     printf("---------------------------------------------\n");
  257.     transactionTotalProfit = 0;
  258.     gotoMenu();
  259. }
  260.  
  261. // PAUSE PROGRAM AND CONTINUE WITH dummy KEY ENTRY
  262. void gotoMenu(){
  263.     printf("\n\nPress Enter to continue...");
  264.     fflush(stdin);
  265.     dummy = getchar();
  266.     while (getchar() != '\n');
  267.     main();
  268. }
  269.  
  270. // CALCULATE QUANTITY IN SESSIONS AND TRANSACTIONS
  271. void calcQuantity(int i){
  272.     itemQuantity[itemId][1] += (i*sessionQuantity);
  273.     itemQuantity[itemId][2] += (i*sessionQuantity);
  274.     itemQuantity[itemId][0] -= (i*sessionQuantity);
  275.     sesionTotalPrice += (i*sessionQuantity*item_Price_Profit[itemId][0]);
  276. }
  277.  
  278. void allStocks(){
  279.     int i,choice;
  280.     system(CLS_NAME);
  281.     printf("INVENTORY\n");
  282.     printf("ID  Item                        Qty   Price   Profit\n");
  283.     printf("+----------------------------------------------------+\n");
  284.     for (i=0;i<totalItem;i++){
  285.         if(itemQuantity[i][0]==-1) printf("|%-2d  %-31sITEM HAS BEEN HID|\n",i,itemName[i]);
  286.         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]);
  287.     }
  288.     printf("+----------------------------------------------------+\n");
  289.     printf("MENU :\n\n    1 - Add Stock\n    2 - Add Item\n    3 - Edit Item\n    4 - hide/Unhide Item\n    5 - Main Menu\n\nPlease select an option : ");
  290.     scanf("%d",&choice);
  291.     choice += 3;
  292.     if(choice>3&&choice<9) menuSelection(choice);
  293.     else allStocks();
  294. }
  295.  
  296. void addStock(){
  297.     getItemId(5);
  298.     if(itemQuantity[itemId][0]==-1){
  299.         printf("UNHIDE ITEM BEFORE ADD STOCK\n");
  300.         sleep(1);
  301.         allStocks();
  302.     }
  303.     getItemQty();
  304.     itemQuantity[itemId][0] += sessionQuantity;
  305.     allStocks();
  306. }
  307.  
  308. // Currently problematic
  309. void addStockItem(){
  310.     system(CLS_NAME);
  311.     printf("New Item\n\n");
  312.     printf("New Item Name   : ");
  313.     fflush(stdin);
  314.     scanf(" %[^\n]s",&*itemName[totalItem]);
  315.     //itemName[totalItem][30] = strupr(itemName[totalItem]);
  316.     fflush(stdin);
  317.     printf("New Item Price  : ");
  318.     scanf("%f",&item_Price_Profit[totalItem][0]);
  319.     getNewItemProfit(totalItem);
  320.     printf("New Item Qty    : ");  
  321.     scanf("%d",&itemQuantity[totalItem][0]);
  322.     itemQuantity[totalItem][1]=0;
  323.     itemQuantity[totalItem][2]=0;
  324.     ++totalItem;
  325.     allStocks();
  326. }
  327.  
  328. void getNewItemProfit(int i){
  329.     printf("New Item Profit : ");  
  330.     scanf("%f",&item_Price_Profit[i][1]);
  331.     while(item_Price_Profit[i][1]>item_Price_Profit[i][0]/2){
  332.         printf("Profit more than half of item's price. Please reduce!\n    New Item Profit : ");   
  333.         scanf("%f",&item_Price_Profit[i][1]);
  334.     }
  335. }
  336.  
  337. // Currently problematic
  338. void editStockItem(){  
  339.     int i;
  340.     system(CLS_NAME);
  341.     printf("Name   : %s\n",itemName[itemId]);
  342.     printf("Price  : %.2f\n",item_Price_Profit[itemId][0]);
  343.     printf("Profit : %.2f\n",item_Price_Profit[itemId][1]);
  344.     printf("Qty    : %d\n",itemQuantity[itemId][0]);
  345.     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: ");
  346.     scanf("%d",&i);
  347.     switch(i){
  348.         case 1  :   printf("New Item Name   : ");
  349.                     fflush(stdin);
  350.                     scanf(" %[^\n]s",&*itemName[itemId]);break;
  351.         case 2  :   printf("New Item Price  : ");
  352.                     scanf("%f",&item_Price_Profit[itemId][0]);break;
  353.         case 3  :   getNewItemProfit(itemId);break;
  354.         case 4  :   printf("New Item Qty    : ");  
  355.                     scanf("%d",&itemQuantity[itemId][0]);break;
  356.         case 5  :   allStocks();break;
  357.         default :   break;
  358.     }
  359.     editStockItem();
  360. }
  361.  
  362. void hideStockItem(){
  363.     if(itemQuantity[itemId][0]!=-1) {
  364.         hiddenItemQuantity[itemId]=itemQuantity[itemId][0];
  365.         itemQuantity[itemId][0]=-1;
  366.     }
  367.     else itemQuantity[itemId][0]=hiddenItemQuantity[itemId];
  368.     allStocks();   
  369. }
  370.  
  371. void getAuth(){
  372.     int i;
  373.     char user[][10]={"manager","134562"},authdummy[10]={};
  374.     system(CLS_NAME);
  375.     printf("USER : ");
  376.     scanf("%s",&*authdummy);
  377.     for(i=0;i<10;i++){
  378.         if(authdummy[i]!=user[0][i]){
  379.             printf("ONLY A MANAGER CAN VIEW THIS PAGE\n");
  380.             sleep(1);
  381.             main();
  382.         }
  383.     }
  384.     printf("PASS : ");
  385.     for(i=0;i<6;i++){
  386.          fflush(stdin);
  387.          // authdummy[i] = getch();
  388.          fflush(stdin);
  389.          if(authdummy[i]=='\n') break;
  390.          printf("*");
  391.     }
  392.     for(i=0;i<6;i++){
  393.         if(authdummy[i]!=user[1][i]){
  394.             printf("\nWRONG PASSWORD\n");
  395.             sleep(1);
  396.             main();
  397.         }
  398.     }
  399.     allStocks();
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement