Advertisement
Guest User

NUWA 2

a guest
May 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define ROWS 100
  5. #define SIZE_OF_STIRNG 100
  6.  
  7. int main();
  8. void getUserChoice(int*);
  9. void addStockEntry();
  10. void purchaseStocks();
  11. void sellStocks();
  12. void displayStocks();
  13. void displaySales();
  14. void displayPurchases();
  15. void clearWindow();
  16. void evaluateChoice(int);
  17. void mainMenu();
  18. void generateTestData();
  19. void columnTables();
  20.  
  21.  
  22. int itemCodeArray[ROWS];
  23. char itemNameArray[ROWS][SIZE_OF_STIRNG];
  24. float unitPriceArray[ROWS];
  25. int discountLevelsArray[ROWS];          //discount level is the number of items of he product the customer has to buy to be eligible to receive the discount
  26. float discountRateArray[ROWS];
  27. int currentStockArray[ROWS];
  28.  
  29. int purchaseCodeArray[ROWS];
  30. int purchaseItemCodeArray[ROWS];
  31. int purchaseAmounts[ROWS];
  32. float purchaseNetTotalArray[ROWS];
  33.  
  34. int salesCodeArray[ROWS];
  35. int salesItemCodeArray[ROWS];
  36. int salesAmounts[ROWS];
  37. float salesNetTotalArray[ROWS];
  38. float salesSubTotalArray[ROWS];
  39.  
  40. int lastItemCode = 0;
  41. int lastPurchaseCode = 0;
  42. int lastSalesCode = 0;
  43.  
  44. void generateTestData(){
  45.     char gItemNames[3][SIZE_OF_STIRNG] = {"Apples","Pineapples","Oranges"};
  46.     float gUnitPrices[3] = {50,100,40};
  47.     float gDiscLevels[3] = {150,200,300};
  48.     float gDiscRates[3] = {20,25,10};
  49.     int gCurrStocks[3] = {10,20,50};
  50.     for(int i = 0;i<3;i++){
  51.         itemCodeArray[i] = i+1;
  52.         strcpy(itemNameArray[i],gItemNames[i]);
  53.         unitPriceArray[i] = gUnitPrices[i];
  54.         discountLevelsArray[i] = gDiscLevels[i];
  55.         discountRateArray[i] = gDiscRates[i];
  56.         currentStockArray[i] = gCurrStocks[i];
  57.         lastItemCode = i+1;
  58.     }
  59.  
  60.  
  61.     int gPurchItemCodes[3] = {1,2,3};
  62.     int gPurchAmounts[3] = {5,10,15};
  63.     for(int i = 0;i<3;i++){
  64.         purchaseCodeArray[i]=i+1;
  65.         purchaseAmounts[i] = gPurchAmounts[i];
  66.         purchaseItemCodeArray[i] = gPurchItemCodes[i];
  67.         purchaseNetTotalArray[i] = gPurchAmounts[i] * unitPriceArray[gPurchItemCodes[i]-1];
  68.  
  69.         lastPurchaseCode = i+1;
  70.     }
  71.  
  72.     int gSalesItemCodes[3] = {1,2,3};
  73.     int gSalesAmounts[3] = {5,10,15};
  74.     for(int i =0;i<3;i++){
  75.         salesCodeArray[i] =i+1;
  76.         salesItemCodeArray[i] = gSalesItemCodes[i];
  77.         salesAmounts[i] = gSalesAmounts[i];
  78.         salesNetTotalArray[i] = gSalesAmounts[i] * unitPriceArray[gSalesItemCodes[i]-1];
  79.         salesSubTotalArray[i] = ( salesNetTotalArray[i] >= discountLevelsArray[gSalesItemCodes[i]-1] ) ?
  80.                                 salesNetTotalArray[i] * (1 - discountRateArray[gSalesItemCodes[i]-1]/100.0) :
  81.                                 salesNetTotalArray[i];
  82.  
  83.         lastSalesCode = i+1;
  84.  
  85.  
  86.  
  87.  
  88.  
  89.     }
  90.  
  91. }
  92. void mainMenu(){
  93.     printf("Welcome to the inventory Management System\n");
  94.     printf("\t\t\tMAIN MENU\n");
  95.     printf("\t1.  Add new stock entry\n");
  96.     printf("\t2.  Record Purchases \n");
  97.     printf("\t3.  Record Sales\n");
  98.     printf("\t4.  View Sales details\n");
  99.     printf("\t5.  View Purchase details\n");
  100.     printf("\t6.  View Stock Details\n");
  101.     printf("\t9.  Exit Program\n");
  102. }
  103. void getUserChoice(int* userChoice){
  104.     printf("Enter choice to continue: ");
  105.     scanf("%d",userChoice);
  106. }
  107. void addStockEntry(){
  108.     printf("Entering item data for item code %d\n",lastItemCode+1);
  109.     itemCodeArray[lastItemCode] = lastItemCode + 1;
  110.     printf("Enter item name: ");
  111.     scanf("%s",itemNameArray[lastItemCode]);
  112.     int isUnitPriceZero = 1;
  113.     while(isUnitPriceZero){
  114.         printf("Enter item unit price: ");
  115.         scanf("%f",&unitPriceArray[lastItemCode]);
  116.         if(unitPriceArray[lastItemCode] == 0 && unitPriceArray[lastItemCode] > 0){
  117.             printf("Unit price can not be zero\n");
  118.         }else{
  119.             isUnitPriceZero = 0;
  120.         }
  121.     }
  122.     printf("Enter item discount levels: ");
  123.     scanf("%d",&discountLevelsArray[lastItemCode]);
  124.     printf("Enter item discount rates(Enter the percentage e.g. to represent 15%% input 15): ");
  125.     scanf("%f",&discountRateArray[lastItemCode]);
  126.     printf("Enter current stock: ");
  127.     scanf("%d",&currentStockArray[lastItemCode]);
  128.     lastItemCode += 1;
  129. }
  130. void purchaseStocks(){
  131.     int inputItemCode = 0;
  132.     int inputAmount = 0;
  133.     printf("Entering purchase code: %d\n",lastPurchaseCode+1);
  134.     printf("Enter item code: ");
  135.     scanf("%d",&inputItemCode);
  136.     if(inputItemCode <= lastItemCode){
  137.         int isAmountZero = 1;
  138.         while(isAmountZero){
  139.             printf("Enter amount: ");
  140.             scanf("%d",&inputAmount);
  141.             if(inputAmount == 0){
  142.                 isAmountZero = 1;
  143.             }else{
  144.                 isAmountZero = 0;
  145.             }
  146.         }
  147.         purchaseCodeArray[lastPurchaseCode] = lastPurchaseCode+1;
  148.         purchaseItemCodeArray[lastPurchaseCode] = inputItemCode;
  149.         purchaseAmounts[inputItemCode-1] = inputAmount;
  150.         purchaseNetTotalArray[inputItemCode-1] = purchaseAmounts[inputItemCode-1] * unitPriceArray[inputItemCode-1];
  151.         currentStockArray[inputItemCode-1]=currentStockArray[inputItemCode-1]+inputAmount;
  152.         lastPurchaseCode += 1;
  153.     }
  154.     else
  155.         printf("invalid item code.\n");
  156.  
  157. }
  158. void sellStocks(){
  159.     int inputItemCode = 0;
  160.     int sellingAmount = 0;
  161.     printf("Entering sales code: %d\n",lastSalesCode+1);
  162.     printf("Enter item code: ");
  163.     scanf("%d",&inputItemCode);
  164.     if(inputItemCode <= lastItemCode){
  165.         int isAmountZero = 1;
  166.         while(isAmountZero){
  167.             printf("Enter amount: ");
  168.             scanf("%d",&sellingAmount);
  169.             if(sellingAmount == 0){
  170.                 isAmountZero = 1;
  171.             }else{
  172.                 isAmountZero = 0;
  173.             }
  174.         }
  175.         if(sellingAmount>currentStockArray[inputItemCode-1])
  176.         {
  177.             printf("not enough stock available to complete the order\n");
  178.         }
  179.         else
  180.         {
  181.         salesCodeArray[lastSalesCode] = lastSalesCode+1;
  182.         salesItemCodeArray[lastSalesCode] = inputItemCode;
  183.         salesAmounts[lastSalesCode] = sellingAmount;
  184.         salesNetTotalArray[inputItemCode-1]= sellingAmount * unitPriceArray[inputItemCode-1];
  185.         //discountArray[lastItemCode]=(sellingAmount>=discountLevelsArray[lastItemCode])?salesNetTotalArray[lastItemCode]*discountRateArray[lastItemCode]/100:0;
  186.         //salesSubTotalArray[lastItemCode]=salesNetTotalArray[lastItemCode]-discountArray[lastItemCode];
  187.         salesSubTotalArray[inputItemCode-1] = ( salesNetTotalArray[inputItemCode-1] >= discountLevelsArray[inputItemCode-1] ) ?
  188.                             salesNetTotalArray[inputItemCode-1] * (1 - discountRateArray[inputItemCode-1]/100.0) :
  189.                             salesNetTotalArray[inputItemCode-1];
  190.  
  191.         currentStockArray[inputItemCode-1]=currentStockArray[inputItemCode-1]-sellingAmount;
  192.         lastSalesCode+=1;
  193.         }
  194.    
  195.         }
  196.  
  197. }
  198.  
  199. void displayStocks(){
  200.  
  201. //labels
  202.         for(int j =0;j<80;j++){
  203.             printf("_");}
  204.         printf("\n");
  205.  
  206.         printf("item name  |");
  207.         printf(" item code  |");
  208.         printf("unit price  |");
  209.         printf("discount level|");
  210.         printf("discount rate|");
  211.         printf("current stock|");
  212.         printf("\n");
  213.  
  214.          for(int k =0;k<80;k++)
  215.             printf("_");
  216.           printf("\n");
  217.  
  218.  
  219.  
  220.  
  221.     for(int i=0;i<lastItemCode;i++){
  222.         printf("%10s | ",itemNameArray[i]);
  223.         printf("%10d | ",itemCodeArray[i]);
  224.         printf("%10.2f | ",unitPriceArray[i]);
  225.         printf("%12.2f | ",discountLevelsArray[i]);
  226.         printf("%11.2f%%| ",discountRateArray[i]);
  227.         printf("%11d | ",currentStockArray[i]);
  228.         printf("\n");
  229.  
  230.     }
  231. }
  232. void displaySales(){
  233.  
  234.     for(int j=0;j<lastItemCode;j++){
  235.         for(int i =0;i<lastSalesCode;i++){
  236.            if(itemCodeArray[j] == salesItemCodeArray[i]){
  237.                 printf("%10d  | ",salesCodeArray[i]);
  238.                 printf("%10d  |",salesItemCodeArray[i]);
  239.                 printf("%11s  |",itemNameArray[salesItemCodeArray[i]-1]);
  240.                 printf("%10.2f | ",unitPriceArray[salesItemCodeArray[i]-1]);
  241.                 printf("%10d  |",salesAmounts[i]);
  242.                 printf("%10.2f | ",salesNetTotalArray[i]);
  243.                 printf("%10.2f   |",salesSubTotalArray[i]);
  244.                 printf("\n");
  245.             }
  246.         }
  247.     }
  248. }
  249. void displayPurchases(){
  250.  
  251.     //labels
  252.         for(int j =0;j<80;j++)
  253.             printf("_");
  254.         printf("\n");
  255.  
  256.         printf("purchase code|");
  257.         printf(" item code  |");
  258.         printf(" item name   |");
  259.         printf(" unit price |");
  260.         printf(" no. of items|");
  261.         printf(" net total|");
  262.         printf("\n");
  263.  
  264.          for(int k =0;k<80;k++)
  265.             printf("_");
  266.           printf("\n");
  267.  
  268.  
  269.     for(int j=0;j<lastItemCode;j++){
  270.         for(int i =0;i<lastPurchaseCode;i++){
  271.            if(itemCodeArray[j] == purchaseItemCodeArray[i]){
  272.                 printf("%10d  | ",purchaseCodeArray[i]);
  273.                 printf("%10d  |",purchaseItemCodeArray[i]);
  274.                 printf("%11s  |",itemNameArray[purchaseItemCodeArray[i]-1]);
  275.                 printf("%10.2f | ",unitPriceArray[purchaseItemCodeArray[i]-1]);
  276.                 printf("%10d  |",purchaseAmounts[purchaseItemCodeArray[i]-1]);
  277.                 printf("%10.2f | ",purchaseNetTotalArray[purchaseItemCodeArray[i]-1]);
  278.                 printf("\n");
  279.             }
  280.         }
  281.     }
  282. }
  283. void clearWindow(){
  284.     system("cls||clear");
  285. }
  286. void evaluateChoice(int userChoice){
  287.  
  288.     clearWindow();
  289.     switch (userChoice)
  290.     {
  291.         case 1:
  292.             addStockEntry();
  293.             break;
  294.         case 2:
  295.             purchaseStocks();
  296.             break;
  297.         case 3:
  298.             sellStocks();
  299.             break;
  300.         case 4:
  301.             displaySales();
  302.             break;
  303.         case 5:
  304.             displayPurchases();
  305.             break;
  306.         case 6:
  307.             displayStocks();
  308.             break;
  309.         case 9:
  310.             printf("Exiting the program");
  311.             exit(0);
  312.             break;
  313.         default:
  314.             printf("INVALID CHOICE\n");
  315.             break;
  316.     }
  317. }
  318.  
  319. int main(){
  320.     generateTestData();
  321.     int userWantstoContinue = 1;
  322.     int userChoice = 0;
  323.     while(userWantstoContinue){
  324.         char yesOrno = ' ';
  325.         clearWindow();
  326.         mainMenu();
  327.         getUserChoice(&userChoice);
  328.         evaluateChoice(userChoice);
  329.        printf("Do you want to continue using the software (y/n): ");
  330.         scanf(" %c",&yesOrno);
  331.         if(yesOrno == 'y'|yesOrno == 'Y'){
  332.             userWantstoContinue = 1;
  333.         }else{
  334.             userWantstoContinue  = 0;
  335.         }
  336.     }
  337.  
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement