production produceGood(int SystemID, int goodID, int availableWorkers){ // This function produces the good in the system specified // Initializing production theProduct; int workers; float costPerUnit; int Produced; float cost; // How many workers are producing this good workers = (availableWorkers * ((float)((rand() % 50) + 1)/100)); // Calculating cost per unit costPerUnit = calculateProductionCost(SystemID, goodID); // Calculating how much product was created Produced = workers * productionRateForGood(goodID) * ((importsGood(SystemID, goodID)) ? 0.75 : 1) * ((exportsGood(SystemID, goodID)) ? 1.25 : 1); // Calculating Production Cost cost = costPerUnit * Produced; // Preping data for return theProduct.workers = workers; theProduct.cost = cost; // Saving production updateStock(SystemID, goodID, Produced); return theProduct; }
void Normal::sell(Machine& machine, int quantity) { int currStock = machine.getCurrentStock(); if (currStock < quantity) { throw std::runtime_error("Not enough stock"); } updateStock(machine, currStock - quantity); if (machine.getCurrentStock() == 0) { setState(machine, new SoldOut()); } }
int main(void){ /************************** Initializing variables ***************************/ int i, j; //Misc counter int workforcePercentage; // Total amount of the population that is // workforce. int growthPercentage; // Total amount of population growth float taxCollection; // Money Collected from Taxes. float productionCost; // Cost to produce resources int consumed; // Amount of the Good Consumed. float goodReserve; // Amount of a Good to Reserve float fluxFactor; // Fluctuation Factor float fluxPrice; // Good price based on the fluctuation systemDemographics SystemData; // System Demographics production Products; // Produced Product data /******************************* Main Program ********************************/ // Seeding Randomizer srand(time(NULL)); // Loading Settings printf("Loading Config.ini...."); if(loadConfig()){ exit(0); } printf("Done.\n"); // Connecting to DB printf("Connecting to DB..."); if(connectToDB()){ exit(0); } printf("Done.\n"); // Loading Goods printf("Loading Goods..."); loadGoods(); printf("Done.\n"); // Setting Good Rates productionRate = malloc(numberOfGoods * sizeof(goodRate)); consumptionRate = malloc(numberOfGoods * sizeof(goodRate)); productionFactor = malloc(numberOfGoods * sizeof(goodRate)); // Loading Rates printf("Loading Consumption and Production Rates..."); if(loadRates()){ exit(0); } printf("Done.\n"); // Loading Markets printf("Loading Markets..."); loadDestinations(); if(loadMarkets()){ exit(0); } printf("Done.\n"); // Cycling through each market for(i = 0; i < numberOfDestinations; i++){ // Loading Demographic Data SystemData = loadSystemDemographics(destinations[i].destinationID); // Setting Growth growthPercentage = ((rand() % 10) + 1) * ((rand() % 2) ? 1:-1); SystemData.pop += round(SystemData.pop * ((float)growthPercentage/100)); // Setting Workforce workforcePercentage = ((rand() % 25) + 1); SystemData.workPop = round(SystemData.pop * ((float)workforcePercentage / 100)); // Collecting Taxes taxCollection = (SystemData.workPop * SystemData.taxes); SystemData.funds += (SystemData.workPop * taxCollection); updateDemographics(SystemData); //Producing/Consuming Goods productionCost = 0; for(j = 0; j < numberOfGoods; j++){ Products = produceGood(SystemData.systemID, goodsID[j], SystemData.workPop); // Removing workers used to produce this good from work popultion (SystemData.workPop -= Products.workers); if(SystemData.workPop < 0){ SystemData.workPop = 0; } // Updating Total Production Cost for the System productionCost += Products.cost; // Calculating Consumption Amount of the Good consumed = ((SystemData.pop * consumptionRateForGood(goodsID[j])) * (0 -1)); // Updating System's Stock updateStock(SystemData.systemID, goodsID[j], consumed); // Determining Amount to reserve goodReserve = ((float)consumed * (0 - 1)) * TICKS_TO_RESERVE_FOR; // Calculating Factor fluxFactor = 1 - ((stockLevel(SystemData.systemID, goodsID[j]) - goodReserve)/goodReserve); // Calculating New Sell Price fluxPrice = fluxFactor * goodPrice(goodsID[j], SystemData.systemID); // Did it cost more to produce then the current Sell Price if(goodPrice(goodsID[j], SystemData.systemID) - calculateProductionCost(SystemData.systemID, goodsID[j]) < 0){ // Add the difference to the new Sell Price. fluxPrice += (calculateProductionCost(SystemData.systemID, goodsID[j]) - goodPrice(goodsID[j], SystemData.systemID)); } // Update New Sell Price updatePrice(SystemData.systemID, goodsID[j], fluxPrice); } // Good Cycle for() // Update Taxes if(taxCollection - productionCost < 0){ updateTaxes(SystemData.systemID, 1); } else { updateTaxes(SystemData.systemID, -1); } }// Markets Cycle for() // Clearing memory printf("Clearing Memory..."); mysql_free_result(result); iniparser_freedict(settings); free(destinations); free(markets); printf("Done.\n"); return 0; }
void SoldOut::refill(Machine& machine, int quantity) { updateStock(machine, quantity); setState(machine, new Normal()); }
void Normal::refill(Machine& machine, int quantity) { int currStock = machine.getCurrentStock(); updateStock(machine, currStock + quantity); }
void main () { startDay(sFile2); populateTextfile(sFile1); int i; do { printf("----------------------------------------------------\n"); printf("Push 1 to sell item.\n"); printf("Push 2 to return item.\n"); printf("Push 3 to search stock on system.\n"); printf("Push 4 to update stock.\n"); printf("Push 5 to print all stock to screen.\n"); printf("Push 6 to see cash in till for today only.\n"); printf("Push 7 or higher to quit the program.\n"); printf("?: "); scanf("%d", &i); switch (i) { case 1: printf("\n----------------------------------------------------\n"); sellItem(sFile1, sFile2); printf("----------------------------------------------------\n\n"); break; case 2: printf("\n----------------------------------------------------\n"); returnBook (sFile1, sFile2); printf("----------------------------------------------------\n\n"); break; case 3: printf("\n----------------------------------------------------\n"); stockSwitch(); printf("----------------------------------------------------\n\n"); break; case 4: printf("\n----------------------------------------------------\n"); updateStock(); printf("----------------------------------------------------\n"); break; case 5: printf("\n----------------------------------------------------\n"); printItem(sFile1); printf("----------------------------------------------------\n\n"); break; case 6: printf("\n----------------------------------------------------\n"); cash(sFile2); printf("----------------------------------------------------\n\n"); break; case 9: rofl(); break; } } while (i<7 && i!=9); populateTextfile(sFile1); endStatement(sFile2); }