Guest User

Untitled

a guest
May 26th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. // Requirements
  2. // Simulate day to day running of the factory
  3. // Continously add machines/equipments as capacity grows
  4. // Show financial breakdown
  5.  
  6. // global variables
  7. const machineArray = [];
  8. const PKO_PRICE = 340000;
  9. const PKC_PRICE = 40000;
  10. const PKSL_PRICE = 50000;
  11.  
  12. const pknToPkc1 = 0.58;
  13. const pknToPko = 0.35
  14. const pknToPksl = 0.07
  15. const pkc1ToPko = 0.09;
  16. const pkc1ToPkc2 = 0.86;
  17. const pkc1ToPksl = 0.05
  18.  
  19. function Expeller(capacity, isFirstCrush) {
  20. this.cap = capacity;
  21. this.config = {};
  22. this.config.firstCrush = isFirstCrush;
  23. }
  24.  
  25. Expeller.prototype.crush = function() {
  26. if(this.config.firstCrush) {
  27. return this.firstCrush();
  28. } else {
  29. return this.secondCrush();
  30. }
  31. }
  32.  
  33. Expeller.prototype.firstCrush = function() {
  34. this.capHr = this.cap / 24;
  35. const output = {};
  36.  
  37. output.pko = this.capHr * pknToPko;
  38. output.pkc1 = this.capHr * pknToPkc1;
  39. output.pksl = this.capHr * pknToPksl;
  40.  
  41. return output;
  42. }
  43.  
  44. Expeller.prototype.secondCrush = function() {
  45. this.capHr = (this.cap - 3) / 24;
  46. const output = {};
  47.  
  48. output.pko = this.capHr * pkc1ToPko
  49. output.pkc2 = this.capHr * pkc1ToPkc2
  50. output.pksl = this.capHr * pkc1ToPksl
  51.  
  52. return output;
  53. }
  54.  
  55. Expeller.prototype.setConfig = function() {
  56. this.config.firstCrush = !this.config.firstCrush;
  57. }
  58.  
  59. function Factory() {
  60. this.output = {};
  61. }
  62.  
  63. Factory.prototype.configure = function(pknAmt, pkcAmt, shiftHr) {
  64. // take configuration object
  65. // apply it to the machine array
  66.  
  67. this.input = {};
  68. this.input.pkn = pknAmt;
  69. this.input.pkc1 = pkcAmt;
  70. }
  71.  
  72. Factory.prototype.runMachines = function() {
  73. machineArray.forEach(machine => {
  74. let machineOutput = machine.crush();
  75. this.output.pko += machineOutput.pko;
  76. this.output.pksl += machineOutput.pksl;
  77. this.output.pkc1 = machineOutput.pkc1 ? this.output.pkc1 + machineOutput.pkc1 : this.output.pkc1;
  78. this.output.pkc2 = machineOutput.pkc2 ? this.output.pkc2 + machineOutput.pkc2 : this.output.pkc2;
  79. });
  80. }
  81.  
  82. Factory.prototype.runShift = function(shiftHr) {
  83. for(let i = 0; i < shiftHr; i++) {
  84. this.runMachines();
  85. }
  86.  
  87. // end of day function
  88. // check to see if more pkn needs to be bought
  89. // check to see if sale should be made
  90. if(this.output.pko >= 10 || this.output.pkc2 >= 5 || this.output.pksl >= 5) this.makeSale;
  91. // check if machine can be bought
  92. if(this.profit > 5000000) this.makePurchase({'expeller': 3000000})
  93. }
  94.  
  95. Factory.prototype.sellProduct = function(qty, product) {
  96. let sale = 0;
  97. switch (product) { // reformat string
  98. case 'PKO':
  99. case 'pko':
  100. sale = qty * PKO_PRICE;
  101. break;
  102. case 'PKC2':
  103. case 'pkc2':
  104. sale = qty * PKC_PRICE;
  105. break;
  106. case 'PKSL':
  107. case 'pksl':
  108. sale = qty * PKSL_PRICE;
  109. break;
  110. default:
  111. console.log('please enter a valid product');
  112. }
  113. return sale;
  114. }
  115.  
  116. Factory.prototype.calcRevenue = function(saleConfig) {
  117. for (key in this.output) {
  118. if(saleConfig.hasOwnProperty(key) && this.output[key] >= saleConfig[key] ) {
  119. revenue += this.sellProduct(saleConfig[key], key);
  120. }
  121. }
  122. return revenue;
  123. }
  124.  
  125. Factory.prototype.makeSale = function() {
  126. let config = {};
  127. config['pko'] = 10;
  128. config['pkc2'] = 5;
  129. config['pksl'] = 5;
  130. this.revenue = this.calcRevenue(config);
  131. this.profit = this.revenue - this.overallCost;
  132. }
  133.  
  134. Factory.prototype.makePurchase = function(purchaseConfig) {
  135. if(this.profit > 1.5 * purchaseConfig['expeller']) {
  136.  
  137. this.profit -= purchaseConfig['expeller'];
  138. let isFirstCrush = false;
  139.  
  140. if(machineArray.length % 2 == 0) isFirstCrush = true;
  141.  
  142. machineArray.push(new Expeller(15, isFirstCrush));
  143. }
  144. }
Add Comment
Please, Sign In to add comment