Advertisement
zaidhuda

Programming Project

Sep 15th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.61 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. #include <ncurses.h>
  22. #else
  23. #define CLS_NAME "cls"
  24. #endif
  25.  
  26. // GLOBAL VARIABLES DECLARATIONS
  27. /*
  28.     itemQuantity[][0] = Item Qty in inventory
  29.     itemQuantity[][1] = Item Qty in a session
  30.     itemQuantity[][2] = Item Qty in all transactions
  31.     item_Price_Profit[][0] = Item Price
  32.     item_Price_Profit[][1] = Item Profit
  33. */
  34. 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;
  35. 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;
  36. 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];
  37.  
  38. // FUNCTIONS PROTOTYPES
  39. 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);
  40.  
  41. // DISPLAY MENU
  42. main(){
  43.     int choice;
  44.     system(CLS_NAME);
  45.     showTime();
  46.     printf("Assalamualaikum\n\nMAIN MENU :\n\n    1 - New Transaction\n    2 - All Transactions\n    3 - Stocks (Manager ONLY)\n\nPlease select an option : ");
  47.     scanf(" %s",str);
  48.     if(validateNum(str)==1) choice = atoi(str);
  49.     else main();
  50.     if(choice>0&&choice<4) menuSelection(choice);
  51.     else main();
  52. }
  53.  
  54. // SHOW CURRENT DATE AND TIME
  55. void showTime(){
  56.     time_t mytime;
  57.     mytime = time(NULL);
  58.     printf("---------------------%24s\n",ctime(&mytime));
  59. }
  60.  
  61. // MENU SELECTION
  62. void menuSelection(int choice){
  63.     if(choice!=6) system(CLS_NAME);
  64.     switch(choice){
  65.         case 1  :   sesionTotalPrice = 0;
  66.                     system(CLS_NAME);
  67.                     printf("CUSTOMER NAME : ");
  68.                     scanf(" %15[^\n]",customer);
  69.                     for(choice=0;choice<15;choice++) customer[choice] = toupper(customer[choice]);
  70.                     newTransaction();break;
  71.         case 2  :   allTransactions();break;
  72.         case 3  :   getAuth(); break;
  73.                     //allStocks(); break;
  74.         case 4  :   addStock();break;
  75.         case 5  :   addStockItem();break;
  76.         case 6  :   getItemId(3);
  77.                     editStockItem();break;
  78.         case 7  :   getItemId(4);
  79.                     hideStockItem();break;
  80.         case 8  :   main();break;
  81.         default :   main();break;
  82.     }
  83. }
  84.  
  85. // SHOWS AVAILABLE ITEMS LIST INCLUDING OUT OF STOCK ITEMS
  86. void newTransaction(){
  87.     int i;
  88.     system(CLS_NAME); //system("cls");
  89.     printf("INVENTORY           SESSION NO  :       %3d\n",sessionNumber);
  90.     printf("CUSTOMER: %-15s TOTAL : RM %6.2f\n\n",customer,sesionTotalPrice);
  91.     printf("ID  Item                        Qty   Price\n");
  92.     printf("-------------------------------------------\n");
  93.     for (i=0;i<totalItem;i++){
  94.         if (itemQuantity[i][0]==-1) ;
  95.         else if (itemQuantity[i][0]==0) printf("%-2d  %-27sOUT OF STOCK\n",i,itemName[i]);
  96.         else printf("%-2d  %-28s%3d  %6.2f\n",i,itemName[i],itemQuantity[i][0],item_Price_Profit[i][0]);
  97.     }
  98.     printf("-------------------------------------------\n");
  99.     printf("-1 VIEW CART\n99 CHECK OUT\n");
  100.     addItem();
  101. }
  102.  
  103. // ADD ITEM TO CART
  104. void addItem(){
  105.     getItemId(1);
  106.     if (itemId==-1) reviewCart();
  107.     if (itemId==99) sessionReceipt();
  108.     if (itemId>=totalItem) newTransaction();
  109.     if (itemQuantity[itemId][0]==0||itemQuantity[itemId][0]==-1) {
  110.         printf("--UNAVAILABLE--\n");
  111.         sleep(1);
  112.         newTransaction();
  113.     }
  114.     if (itemQuantity[itemId][0]==1) {
  115.         itemQuantity[itemId][0] --;
  116.         itemQuantity[itemId][1] ++;
  117.         itemQuantity[itemId][2] ++;
  118.         sesionTotalPrice += item_Price_Profit[itemId][0];
  119.         newTransaction();
  120.     }
  121.     getItemQty();
  122.     if (sessionQuantity<0){
  123.         printf("--INVALID--\n");
  124.         sleep(1);
  125.         newTransaction();
  126.     }
  127.     if (sessionQuantity>itemQuantity[itemId][0]){
  128.         printf("--INSUFFICIENT--\n");
  129.         sleep(1);
  130.         newTransaction();
  131.     }
  132.     calcQuantity(1);
  133.     newTransaction();
  134. }
  135.  
  136. // DISPLAY ALL ITEMS ADDED TO CART
  137. void reviewCart(){
  138.     int i;
  139.     system(CLS_NAME); //system("cls");
  140.     printf("INVENTORY           SESSION NO  :       %3d\n",sessionNumber);
  141.     printf("CUSTOMER: %-15s TOTAL : RM %6.2f\n\n",customer,sesionTotalPrice);
  142.     printf("ID  Item                       Price Amount\n");
  143.     printf("-------------------------------------------\n");
  144.     for (i=0;i<totalItem;i++){
  145.         if (itemQuantity[i][1]!=0)
  146.             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]);
  147.     }
  148.     printf("                                     ------\n");
  149.     printf("SUB TOTAL                            %6.2f\n",sesionTotalPrice);
  150.     printf("-------------------------------------------\n");
  151.     printf("-1 BROWSE INVENTORY\n99 CHECK OUT\n");
  152.     removeItem();
  153. }
  154.  
  155. // REMOVE ITEM FROM CART
  156. void removeItem(){
  157.     getItemId(2);
  158.     if (itemId==-1) newTransaction();
  159.     if (itemId==99) sessionReceipt();
  160.     if (itemId>=totalItem) removeItem();
  161.     if (itemQuantity[itemId][1]==1) {
  162.         itemQuantity[itemId][0] ++;
  163.         itemQuantity[itemId][1] --;
  164.         itemQuantity[itemId][2] --;
  165.         sesionTotalPrice -= item_Price_Profit[itemId][0];
  166.         reviewCart();
  167.     }
  168.     if (itemQuantity[itemId][1]==0||itemQuantity[itemId][0]==-1) {
  169.         printf("--ITEM INEXISTENT--\n");
  170.         sleep(1);
  171.         reviewCart();
  172.     }
  173.     getItemQty();
  174.     if (sessionQuantity<0){
  175.         printf("--INVALID--\n");
  176.         sleep(1);
  177.         reviewCart();
  178.     }
  179.     if (sessionQuantity>itemQuantity[itemId][1]){
  180.         printf("--INSUFFICIENT--\n");
  181.         sleep(1);
  182.         reviewCart();
  183.     }
  184.     calcQuantity(-1);
  185.     reviewCart();
  186. }
  187.  
  188. /*GET TARGET ITEM ID :
  189.     1=TO BE ADDED TO CART,
  190.     2=TO BE REMOVED FROM CART,
  191.     3=TO BE EDITED,
  192.     4=TO BE REMOVED,*/
  193. void getItemId(int i){
  194.     switch(i){
  195.         case 1 :    printf("\nPlease enter Item ID to add to cart : ");break;
  196.         case 2 :    printf("\nPlease enter Item ID to hide from cart : ");break;
  197.         case 3 :    printf("ID of item to be edited : ");break;
  198.         case 4 :    printf("ID of item to be hide/unhide : ");break;
  199.         case 5 :    printf("ID of item to be restocked : ");break;
  200.     }
  201.     scanf(" %s",str);
  202.     if(validateNum(str)==1) itemId = atoi(str);
  203.     else getItemId(i);
  204.     if(i==3 && itemId>=totalItem) {
  205.         printf("Item does not exist. Create? Y to confirm : ");
  206.         scanf(" %c",&dummy);
  207.         if(toupper(dummy)!='Y') allStocks();
  208.         itemId = totalItem++;
  209.     }
  210. }
  211.  
  212. // GET TARGET ITEM QTY
  213. void getItemQty(){
  214.     printf("Please enter Item Quantity : ");
  215.     scanf(" %s",str);
  216.     if(validateNum(str)==1) sessionQuantity = atoi(str);
  217.     else getItemQty();
  218. }
  219.  
  220. // DISPLAY SESSION RECEIPT - ONE TIME ONLY
  221. void sessionReceipt(){
  222.     int i;
  223.     system(CLS_NAME); //system("cls");
  224.     showTime();
  225.     printf("                       Receipt number : %3d\n",sessionNumber);
  226.     printf("---------------------------------------------\n");
  227.     printf("ID  Item                        Price  Amount\n");
  228.     for (i=0;i<totalItem;i++){
  229.         if (itemQuantity[i][1]!=0)
  230.             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]);
  231.         if(sessionPayment!=0) itemQuantity[i][1]=0;
  232.     }
  233.     printf("                                      -------\n");
  234.     printf("SUB TOTAL                             %7.2f\n",sesionTotalPrice);
  235.     printf("                                      -------\n");
  236.     if(sessionPayment==0){
  237.         printf("CASH     : ");
  238.         scanf(" %s",str);
  239.         if(validateNum(str)==1) sessionPayment = atoi(str);
  240.         else sessionReceipt();
  241.         sessionReceipt();
  242.     }else printf("CASH     : %.2f\n",sessionPayment);
  243.     printf("CHANGE   : %.2f\n",sessionPayment-sesionTotalPrice);
  244.     printf("\n---------------------------------------------\n");
  245.     printf("-------------THANK YOU FOR COMING------------\n");
  246.     printf("---------------------------------------------\n");
  247.     transactionTotalPrice += sesionTotalPrice;
  248.     sessionNumber++;
  249.     sessionPayment=0;
  250.     gotoMenu();
  251. }
  252.  
  253. // DISPLAY TOTAL TRANSACTIONS
  254. void allTransactions(){
  255.     int i;
  256.     system(CLS_NAME); //system("cls");
  257.     showTime();
  258.     printf("ALL TRANCTIONS\n\n");
  259.     printf("ID  Item                        Price  Profit\n");
  260.     printf("---------------------------------------------\n");
  261.     for (i=0;i<totalItem;i++){
  262.         if (itemQuantity[i][2]!=0)
  263.             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]);
  264.         transactionTotalProfit += item_Price_Profit[i][1]*itemQuantity[i][2];
  265.     }
  266.     printf("                              ------- -------\n");
  267.     printf("TOTAL                         %7.2f %7.2f\n",transactionTotalPrice,transactionTotalProfit);
  268.     printf("                              ------- -------\n");
  269.     printf("TOTAL TRANSACTIONS                    %7d\n",sessionNumber-1);
  270.     printf("---------------------------------------------\n");
  271.     transactionTotalProfit = 0;
  272.     gotoMenu();
  273. }
  274.  
  275. // PAUSE PROGRAM AND CONTINUE WITH ENTER
  276. void gotoMenu(){
  277.     printf("\n\nPress Enter to continue...");
  278.     dummy = getchar();
  279.     while (getchar() != '\n');
  280.     main();
  281. }
  282.  
  283. // CALCULATE QUANTITY IN SESSIONS AND TRANSACTIONS
  284. void calcQuantity(int i){
  285.     itemQuantity[itemId][1] += (i*sessionQuantity);
  286.     itemQuantity[itemId][2] += (i*sessionQuantity);
  287.     itemQuantity[itemId][0] -= (i*sessionQuantity);
  288.     sesionTotalPrice += (i*sessionQuantity*item_Price_Profit[itemId][0]);
  289. }
  290.  
  291. // DISPLAY ALL STOCKS TABLE
  292. void allStocks(){
  293.     int i,choice;
  294.     system(CLS_NAME);
  295.     printf("INVENTORY\n");
  296.     printf("ID  Item                        Qty   Price   Profit\n");
  297.     printf("+----------------------------------------------------+\n");
  298.     for (i=0;i<totalItem;i++){
  299.         if(itemQuantity[i][0]==-1) printf("|%-2d  %-31sITEM HAS BEEN HID|\n",i,itemName[i]);
  300.         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]);
  301.     }
  302.     printf("+----------------------------------------------------+\n");
  303.     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 : ");
  304.     scanf(" %s",str);
  305.     if(validateNum(str)==1) choice = atoi(str);
  306.     else allStocks();
  307.     choice += 3;
  308.     if(choice>3&&choice<9) menuSelection(choice);
  309.     else allStocks();
  310. }
  311.  
  312. // ADD STOCK
  313. void addStock(){
  314.     getItemId(5);
  315.     if(itemQuantity[itemId][0]==-1){
  316.         printf("UNHIDE ITEM BEFORE ADD STOCK\n");
  317.         sleep(1);
  318.         allStocks();
  319.     }
  320.     getItemQty();
  321.     itemQuantity[itemId][0] += sessionQuantity;
  322.     allStocks();
  323. }
  324.  
  325. // ADD NEW ITEM
  326. void addStockItem(){
  327.     int i;
  328.     system(CLS_NAME);
  329.     printf("New Item\n\n");
  330.     printf("New Item Name   : ");
  331.     scanf(" %30[^\n]", itemName[totalItem]);
  332.     for (i=0;i<30;i++) itemName[totalItem][i] = toupper(itemName[totalItem][i]);
  333.     printf("New Item Price  : ");
  334.     scanf(" %f",&item_Price_Profit[totalItem][0]);
  335.     getNewItemProfit(totalItem);
  336.     printf("New Item Qty    : ");  
  337.     scanf(" %d",&itemQuantity[totalItem][0]);
  338.     itemQuantity[totalItem][1]=0;
  339.     itemQuantity[totalItem][2]=0;
  340.     validateItemName(1,totalItem);
  341.     ++totalItem;
  342.     allStocks();
  343. }
  344.  
  345. // EDIT ITEM
  346. void editStockItem(){  
  347.     int i;
  348.     system(CLS_NAME);
  349.     printf("Name   : %s\n",itemName[itemId]);
  350.     printf("Price  : %.2f\n",item_Price_Profit[itemId][0]);
  351.     printf("Profit : %.2f\n",item_Price_Profit[itemId][1]);
  352.     printf("Qty    : %d\n",itemQuantity[itemId][0]);
  353.     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: ");
  354.     scanf(" %s",str);
  355.     if(validateNum(str)==1) i = atoi(str);
  356.     else editStockItem();
  357.     switch(i){
  358.         case 1  :   printf("New Item Name   : ");
  359.                     strcpy(itemName[99],itemName[itemId]);
  360.                     scanf(" %30[^\n]", itemName[itemId]);
  361.                     for (i=0;i<30;i++) itemName[itemId][i] = toupper(itemName[itemId][i]);
  362.                     break;
  363.         case 2  :   printf("New Item Price  : ");
  364.                     scanf(" %f",&item_Price_Profit[itemId][0]);break;
  365.         case 3  :   getNewItemProfit(itemId);break;
  366.         case 4  :   printf("New Item Qty    : ");  
  367.                     scanf(" %d",&itemQuantity[itemId][0]);break;
  368.         case 5  :   allStocks();break;
  369.         default :   break;
  370.     }
  371.     validateItemName(0,itemId);
  372.     editStockItem();
  373. }
  374.  
  375. // MAKE SURE PROFIT NOT MORE THAN HALF PRICE
  376. void getNewItemProfit(int i){
  377.     printf("New Item Profit : ");  
  378.     scanf(" %f",&item_Price_Profit[i][1]);
  379.     while(item_Price_Profit[i][1]>item_Price_Profit[i][0]/2){
  380.         printf("Profit more than half of item's price. Please reduce!\n    New Item Profit : ");   
  381.         scanf(" %f",&item_Price_Profit[i][1]);
  382.     }
  383. }
  384.  
  385. // HIDE ITEM FROM MENU
  386. void hideStockItem(){
  387.     if(itemQuantity[itemId][0]!=-1) {
  388.         hiddenItemQuantity[itemId]=itemQuantity[itemId][0];
  389.         itemQuantity[itemId][0]=-1;
  390.     }
  391.     else itemQuantity[itemId][0]=hiddenItemQuantity[itemId];
  392.     allStocks();   
  393. }
  394.  
  395. // ONLY AUTHORIZED USER CAN VIEW ALL STOCKS
  396. void getAuth(){
  397.     int i;
  398.     char user[][10]={"manager","134562"},authdummy[10]={};
  399.     system(CLS_NAME);
  400.     printf("USER : ");
  401.     scanf(" %s",&*authdummy);
  402.     for(i=0;i<10;i++){
  403.         if(authdummy[i]!=user[0][i]){
  404.             printf("ONLY A MANAGER CAN VIEW THIS PAGE\n");
  405.             sleep(1);
  406.             main();
  407.         }
  408.     }
  409.     printf("PASS : ");
  410.     fflush(stdin); // experimental
  411.     for(i=0;i<6;i++){
  412.         authdummy[i] = getch();
  413.         if(authdummy[i]=='\n') break;
  414.         printf("*");
  415.     }
  416.     for(i=0;i<6;i++){
  417.         if(authdummy[i]!=user[1][i]){
  418.             printf("\nWRONG PASSWORD\n");
  419.             sleep(1);
  420.             main();
  421.         }
  422.     }
  423.     allStocks();
  424. }
  425.  
  426. // MAKE SURE ITEM NAMES' ARE UNIQUE
  427. void validateItemName(int add,int id){
  428.     int i;
  429.     for (i = 0; i < totalItem; ++i)
  430.     {
  431.         if (!strcmp(itemName[id],itemName[i]) && id!=i){
  432.             if(add==1){
  433.                 item_Price_Profit[i][0] = item_Price_Profit[id][0];
  434.                 item_Price_Profit[i][1] = item_Price_Profit[id][1];
  435.                 itemQuantity[i][0] += itemQuantity[id][0];
  436.                 for (i = 0; i < 30; ++i)itemName[id][i] = '\0';
  437.                 item_Price_Profit[id][0] = 0;
  438.                 item_Price_Profit[id][1] = 0;
  439.                 itemQuantity[id][0] = 0;
  440.                 --totalItem;
  441.             }else{
  442.                 printf("Item Exists\n");
  443.                 sleep(1);
  444.                 strcpy(itemName[id],itemName[99]);
  445.                 editStockItem();
  446.             }
  447.         }
  448.     }
  449. }
  450.  
  451. // MAKE SURE INPUTS ARE FOOLPROOF
  452. int validateNum(char *p){
  453.     if (*p) {
  454.         char c;
  455.         while ((c=*p++)) if (!isdigit(c)&&c!='-') return 0;
  456.         return 1;
  457.     }
  458.     return 0;
  459. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement