Guest User

Untitled

a guest
Nov 15th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. //************* Evaporate.java
  2.  
  3. package mainpkg;
  4.  
  5. public class Evaporate {
  6.  
  7.     public static void main(String[] args) {
  8.         new StatusGenerator(2565, 3535, 20).generateAndPrint();
  9.     }
  10. }
  11.  
  12. // ***************** StatusGenerator.java
  13.  
  14.  
  15. package mainpkg;
  16.  
  17. import java.io.FileNotFoundException;
  18. import java.io.PrintWriter;
  19. import java.io.UnsupportedEncodingException;
  20. import java.util.Random;
  21.  
  22. public class StatusGenerator {
  23.    
  24.     private int minWeight;
  25.     private int maxWeight;
  26.     private int alertPercentage;
  27.     private int weightRange;
  28.     private int itemsToGenerate = 2160; // 90 days x 24 hours
  29.     private int weekdayDelta = 20;
  30.     private int weekendDelta = 5;
  31.     private PrintWriter writer;
  32.    
  33.     public StatusGenerator(int minWeight, int maxWeight, int alertPercentage) {
  34.         this.minWeight = minWeight;
  35.         this.maxWeight = maxWeight;
  36.         this.weightRange = maxWeight - minWeight;
  37.         this.alertPercentage = alertPercentage;
  38.     }
  39.    
  40.     public void generateAndPrint() {
  41.         try {
  42.             writer = new PrintWriter("C:\\UTN\\gas.txt", "UTF-8");  //FIXME ojo que esto está hardcodeado
  43.         } catch (FileNotFoundException | UnsupportedEncodingException e) {
  44.             e.printStackTrace();
  45.         }
  46.         int currentValue = this.generateInitialValue();
  47.         for (int rows = 0; rows < itemsToGenerate; rows++) {
  48.             doPrint(currentValue);
  49.             boolean isWeekend = rows % 168 < 120;
  50.             currentValue = currentValue - new Random().nextInt(isWeekend ? weekendDelta : weekdayDelta) - 1;
  51.             if (this.shouldRecharge(currentValue)) {
  52.                 currentValue = maxWeight;
  53.             }
  54.         }
  55.        
  56.         writer.close();
  57.     }
  58.    
  59.     private boolean shouldRecharge(int currentValue) {
  60.         if (currentValue <= minWeight + weekdayDelta) return true;
  61.         if (isLow(currentValue) && new Random().nextInt(50) == 1) return true;
  62.         return false;
  63.     }
  64.    
  65.     private boolean isLow(int currentValue) {
  66.         return currentValue - minWeight < weightRange * alertPercentage / 100;
  67.     }
  68.    
  69.     private int generateInitialValue() {
  70.         return new Random().nextInt(weightRange) + minWeight;
  71.     }
  72.    
  73.     private void doPrint(int value) {
  74.         writer.print(value + "\n");
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment