Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Requirements
- // Simulate day to day running of the factory
- // Continously add machines/equipments as capacity grows
- // Show financial breakdown
- // global variables
- const machineArray = [];
- const PKO_PRICE = 340000;
- const PKC_PRICE = 40000;
- const PKSL_PRICE = 50000;
- const pknToPkc1 = 0.58;
- const pknToPko = 0.35
- const pknToPksl = 0.07
- const pkc1ToPko = 0.09;
- const pkc1ToPkc2 = 0.86;
- const pkc1ToPksl = 0.05
- function Expeller(capacity, isFirstCrush) {
- this.cap = capacity;
- this.config = {};
- this.config.firstCrush = isFirstCrush;
- }
- Expeller.prototype.crush = function() {
- if(this.config.firstCrush) {
- return this.firstCrush();
- } else {
- return this.secondCrush();
- }
- }
- Expeller.prototype.firstCrush = function() {
- this.capHr = this.cap / 24;
- const output = {};
- output.pko = this.capHr * pknToPko;
- output.pkc1 = this.capHr * pknToPkc1;
- output.pksl = this.capHr * pknToPksl;
- return output;
- }
- Expeller.prototype.secondCrush = function() {
- this.capHr = (this.cap - 3) / 24;
- const output = {};
- output.pko = this.capHr * pkc1ToPko
- output.pkc2 = this.capHr * pkc1ToPkc2
- output.pksl = this.capHr * pkc1ToPksl
- return output;
- }
- Expeller.prototype.setConfig = function() {
- this.config.firstCrush = !this.config.firstCrush;
- }
- function Factory() {
- this.output = {};
- }
- Factory.prototype.configure = function(pknAmt, pkcAmt, shiftHr) {
- // take configuration object
- // apply it to the machine array
- this.input = {};
- this.input.pkn = pknAmt;
- this.input.pkc1 = pkcAmt;
- }
- Factory.prototype.runMachines = function() {
- machineArray.forEach(machine => {
- let machineOutput = machine.crush();
- this.output.pko += machineOutput.pko;
- this.output.pksl += machineOutput.pksl;
- this.output.pkc1 = machineOutput.pkc1 ? this.output.pkc1 + machineOutput.pkc1 : this.output.pkc1;
- this.output.pkc2 = machineOutput.pkc2 ? this.output.pkc2 + machineOutput.pkc2 : this.output.pkc2;
- });
- }
- Factory.prototype.runShift = function(shiftHr) {
- for(let i = 0; i < shiftHr; i++) {
- this.runMachines();
- }
- // end of day function
- // check to see if more pkn needs to be bought
- // check to see if sale should be made
- if(this.output.pko >= 10 || this.output.pkc2 >= 5 || this.output.pksl >= 5) this.makeSale;
- // check if machine can be bought
- if(this.profit > 5000000) this.makePurchase({'expeller': 3000000})
- }
- Factory.prototype.sellProduct = function(qty, product) {
- let sale = 0;
- switch (product) { // reformat string
- case 'PKO':
- case 'pko':
- sale = qty * PKO_PRICE;
- break;
- case 'PKC2':
- case 'pkc2':
- sale = qty * PKC_PRICE;
- break;
- case 'PKSL':
- case 'pksl':
- sale = qty * PKSL_PRICE;
- break;
- default:
- console.log('please enter a valid product');
- }
- return sale;
- }
- Factory.prototype.calcRevenue = function(saleConfig) {
- for (key in this.output) {
- if(saleConfig.hasOwnProperty(key) && this.output[key] >= saleConfig[key] ) {
- revenue += this.sellProduct(saleConfig[key], key);
- }
- }
- return revenue;
- }
- Factory.prototype.makeSale = function() {
- let config = {};
- config['pko'] = 10;
- config['pkc2'] = 5;
- config['pksl'] = 5;
- this.revenue = this.calcRevenue(config);
- this.profit = this.revenue - this.overallCost;
- }
- Factory.prototype.makePurchase = function(purchaseConfig) {
- if(this.profit > 1.5 * purchaseConfig['expeller']) {
- this.profit -= purchaseConfig['expeller'];
- let isFirstCrush = false;
- if(machineArray.length % 2 == 0) isFirstCrush = true;
- machineArray.push(new Expeller(15, isFirstCrush));
- }
- }
Add Comment
Please, Sign In to add comment