Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- /*
- defining compile time constants to set global arrays with a constant size
- for const keyword variables are run time constants which are set after compilation
- */
- #define RECORDS 100
- #define STR_LIM 100
- /*
- tableRow header printing constants
- */
- #define STOCKS 0
- #define SALES 1
- #define PURCHASES 2
- //global arrays for item code
- int itemCodes[RECORDS];
- char itemNames[RECORDS][STR_LIM];
- float unitPrices[RECORDS];
- float discLevels[RECORDS];
- float discRates[RECORDS]; //entered as 15,10,7.5 and not in the form of 0.15,0.10,0.075
- int currStocks[RECORDS];
- //the most recent item code,purchase code and sales code
- int recentItemCode = 0;
- int recentPurchaseCode = 0;
- int recentSalesCode = 0;
- int purchaseCodes[RECORDS];
- int purchaseItemCodes[RECORDS];
- int purchaseAmounts[RECORDS];
- float purchaseNettotals[RECORDS]; // use the net total array for the sub total as well
- int salesCodes[RECORDS];
- int salesItemCodes[RECORDS];
- int salesAmounts[RECORDS];
- float salesSubtotals[RECORDS];
- float salesNettotals[RECORDS];
- void genTestData();
- void funcAddPurchase();
- void addSalesFunc();
- void tableHeaderRowPrint(int section);
- void funcCurStockDet();
- void funcViewSales();
- void viewPurchasesFunc();
- void funcDetailsEntry();
- void clearWindow();
- int main(){
- genTestData();
- bool exitNumberPressed = false;
- char choice = ' ';
- while(!exitNumberPressed){
- clearWindow();
- int selectnum = 0;
- printf("Welcome...!!! \n");
- printf("\tWhat do you want to do here? \n");
- printf("\tAdd Stock Details - 1\n");
- printf("\tAdd Purchases - 2\n");
- printf("\tAdd Sales - 3\n");
- printf("\tView Purchases - 4\n");
- printf("\tView Sales - 5\n");
- printf("\tView Current Stock details - 6\n");
- printf("\tClose Application - 7\n");
- printf("\nPlease Enter the Number Here :");
- scanf("%d",&selectnum);
- clearWindow();
- switch (selectnum){
- case 1:
- funcDetailsEntry();
- break;
- case 2:
- funcAddPurchase();
- break;
- case 3:
- addSalesFunc();
- break;
- case 5:
- funcViewSales();
- break;
- case 4:
- viewPurchasesFunc();
- break;
- case 6:
- funcCurStockDet();
- break;
- case 7:
- printf("Exiting Software\n");
- exit(0);
- break;
- default:
- printf("\n Invalid choice \n");
- return 0;
- break;
- }
- printf("\nWould you like to continue operations(y/n): ");
- scanf(" %c",&choice);
- exitNumberPressed = choice == 'y' || choice == 'Y' ? false : true;
- }
- }
- void genTestData(){
- char gItemNames[3][STR_LIM] = {"Apples","Papol","Oranges"};
- float gUnitPrices[3] = {10,20,30};
- float gDiscLevels[3] = {10,20,30};
- float gDiscRates[3] = {10,20,30};
- int gCurrStocks[3] = {10,20,30};
- for(int i = 0;i<3;i++){
- itemCodes[i] = i+1;
- strcpy(itemNames[i],gItemNames[i]);
- unitPrices[i] = gUnitPrices[i];
- discLevels[i] = gDiscLevels[i];
- discRates[i] = gDiscRates[i];
- currStocks[i] = gCurrStocks[i];
- recentItemCode = i+1;
- }
- int gPurchItemCodes[3] = {1,2,3};
- int gPurchAmounts[3] = {5,10,15};
- for(int i = 0;i<3;i++){
- purchaseCodes[i]=i+1;
- purchaseAmounts[i] = gPurchAmounts[i];
- purchaseItemCodes[i] = gPurchItemCodes[i];
- purchaseNettotals[i] = gPurchAmounts[i] * unitPrices[gPurchItemCodes[i]-1];
- recentPurchaseCode = i+1;
- }
- int gSalesItemCodes[3] = {1,2,3};
- int gSalesAmounts[3] = {5,10,15};
- for(int i =0;i<3;i++){
- salesCodes[i] =i+1;
- salesItemCodes[i] = gSalesItemCodes[i];
- salesAmounts[i] = gSalesAmounts[i];
- salesNettotals[i] = gSalesAmounts[i] * unitPrices[gSalesItemCodes[i]-1];
- salesSubtotals[i] = ( salesNettotals[i] >= discLevels[gSalesItemCodes[i]-1] ) ?
- salesNettotals[i] * (1 - discRates[gSalesItemCodes[i]-1]/100.0) :
- salesNettotals[i];
- recentSalesCode = i+1;
- }
- }
- void funcAddPurchase(){
- int inputItemCode,inputAmount;
- printf("Enter the item code to purchase: ");
- scanf("%d",&inputItemCode);
- if(inputItemCode<=recentItemCode){
- //item exists
- printf("Enter the amount to purchase: ");
- scanf("%d",&inputAmount);
- purchaseCodes[recentPurchaseCode] = recentPurchaseCode+1;
- purchaseItemCodes[recentPurchaseCode] = inputItemCode;
- purchaseAmounts[recentPurchaseCode] = inputAmount;
- purchaseNettotals[recentPurchaseCode] = unitPrices[inputItemCode-1] * inputAmount;
- currStocks[inputItemCode-1] += inputAmount;
- recentPurchaseCode += 1;
- }
- }
- void addSalesFunc(){
- int inputItemCode,inputAmount;
- printf("Enter the item code to sell: ");
- scanf("%d",&inputItemCode);
- if(inputItemCode<=recentItemCode){
- //item exists
- printf("Enter the amount to sell: ");
- scanf("%d",&inputAmount);
- if(currStocks[inputItemCode-1] >= inputAmount){
- //amount is enough
- salesCodes[recentSalesCode] = recentSalesCode+1;
- salesItemCodes[recentSalesCode] = inputItemCode;
- salesAmounts[recentSalesCode] = inputAmount;
- salesNettotals[recentSalesCode] = unitPrices[inputItemCode-1] * inputAmount;
- salesSubtotals[recentSalesCode] = salesNettotals[recentSalesCode] <= discLevels[inputItemCode-1] ?
- salesNettotals[recentSalesCode] * (1 - discRates[inputItemCode-1]/100.0) :
- salesNettotals[recentSalesCode];
- currStocks[inputItemCode-1] -= inputAmount;
- recentSalesCode += 1;
- }
- else{
- printf("Not enough stocks\n");
- }
- }else{
- printf("Item code does not exist\n");
- }
- }
- void tableHeaderRowPrint(int section){
- switch (section){
- case STOCKS:
- for(int i =0;i<100;i++){
- printf("-");
- }
- printf("\n");
- printf("%12s %12s %12s %12s %12s %12s\n",
- "item code","item name","unit price","discount level","discount rate","current stock");
- break;
- case SALES:
- for(int i =0;i<100;i++){
- printf("-");
- }
- printf("\n");
- printf("%12s %12s %12s %12s %12s %12s %12s\n",
- "sales code","item code","item name","unit price","amount","net total","sub total");
- break;
- case PURCHASES:
- for(int i =0;i<100;i++){
- printf("-");
- }
- printf("\n");
- printf("%12s %12s %12s %12s %12s %12s \n",
- "purchase code","item code","item name","unit price","amount","net total");
- break;
- default:
- break;
- }
- }
- void funcCurStockDet(){
- tableHeaderRowPrint(STOCKS);
- for(int i = 0;i<recentItemCode;i++){
- printf("%12d %12s %12.2f %12.2f %12.2f %12d\n",
- itemCodes[i],itemNames[i],unitPrices[i],discLevels[i],discRates[i],currStocks[i]);
- }
- }
- void funcViewSales(){
- tableHeaderRowPrint(SALES);
- for(int a=0;a<recentItemCode;a++){
- for(int i=0;i<recentSalesCode;i++){
- if(itemCodes[a] == salesItemCodes[i]){
- printf("%12d ",salesCodes[i]);
- printf("%12d ",salesItemCodes[i]);
- printf("%12s ",itemNames[salesItemCodes[i]-1]);
- printf("%12d ",salesAmounts[i]);
- printf("%12.2f ",unitPrices[salesItemCodes[i]-1]);
- printf("%12.2f ",salesNettotals[i]);
- printf("%12.2f ",salesSubtotals[i]);
- printf("\n");
- }
- }
- }
- }
- void viewPurchasesFunc(){
- tableHeaderRowPrint(PURCHASES);
- for(int a=0;a<recentItemCode;a++){
- for(int i=0;i<recentSalesCode;i++){
- if(itemCodes[a] == salesItemCodes[i]){
- printf("%12d ",purchaseCodes[i]);
- printf("%12d ",purchaseItemCodes[i]);
- printf("%12s ",itemNames[purchaseItemCodes[i]-1]);
- printf("%12.2f ",unitPrices[purchaseItemCodes[i]-1]);
- printf("%12d ",purchaseAmounts[i]);
- printf("%12.2f ",purchaseNettotals[i]);
- printf("\n");
- }
- }
- }
- }
- void funcDetailsEntry(){
- printf("Adding stock info for item code %d\n",recentItemCode+1);
- int curr_stock;
- float unit_price,disc_level,disc_rate;
- char item_name[STR_LIM];
- printf("Enter item name: ");
- scanf("%s",item_name);
- printf("Enter item unit price: ");
- scanf("%f",&unit_price);
- printf("Enter item discount level: ");
- scanf("%f",&disc_level);
- printf("Enter item discount rate: ");
- scanf("%f",&disc_rate);
- printf("Enter item current stock: ");
- scanf("%d",&curr_stock);
- strcpy(itemNames[recentItemCode-1],item_name);
- unitPrices[recentItemCode-1] = unit_price;
- discLevels[recentItemCode-1] = disc_level;
- discRates[recentItemCode-1] = disc_rate;
- currStocks[recentItemCode-1] = curr_stock;
- recentItemCode = recentItemCode+1;
- }
- void clearWindow(){
- system("cls||clear");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement