Facebook
From erwer, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 258
  1. MAin
  2.   _____________________________
  3.   package example.szeregowanie;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Random;
  8. import java.util.Scanner;
  9.  
  10. public class Main
  11. {
  12.        
  13.         public static Language lang;
  14.          
  15.     public static void fillTransmissions(List<Transmission> trans, double deadline, double transSize , double startTime, double count)
  16.         {
  17.                 for (int i = 0; i < count; ++i)
  18.                 {
  19.                         trans.add( new Transmission(transSize, deadline, startTime) );
  20.                 }
  21.         }
  22.        
  23.         public static Procesor createProcessor(int power, Resource resources )
  24.         {      
  25.                 return new Procesor((double)power, resources);
  26.         }
  27.  
  28.    
  29.         public static void main(String[] args)
  30.         {
  31.                 System.out.println("Wybierz język : \n1) Polski\n2) Angielski");
  32.                 Scanner scanner = new Scanner(System.in);
  33.                 int value = scanner.nextInt();
  34.                 Polish pol = new Polish();
  35.                
  36.                 switch(value)
  37.                 {
  38.                         case 1:
  39.                                 lang = new Polish();
  40.                                 break;
  41.                         case 2:
  42.                                 lang = new English();
  43.                                 break;
  44.                         default:
  45.                         {
  46.                                 System.out.println("Brak takiej wartosci. Domsylnie wybrany polski");
  47.                                 lang = new Polish();
  48.                         }
  49.                        
  50.                 }
  51.                
  52.                 Random generator = new Random();
  53.                
  54.                 Resource res = new Resource(generator.nextInt(200) + 51 );
  55.                 List<Procesor> processors = new ArrayList<Procesor>();
  56.                 List<Transmission> transmissions = new ArrayList<Transmission>();
  57.                
  58.                 processors.add(createProcessor(generator.nextInt(200) + 20, res));
  59.                 fillTransmissions(transmissions,generator.nextInt(500) + 100,generator.nextInt(50) + 1, generator.nextInt(200), generator.nextInt(10) + 2 );
  60.                
  61.                 boolean isFinished = false;
  62.                
  63.                 while(!isFinished)
  64.                 {
  65.                         int i = 0;
  66.                     for(Transmission trans : transmissions)
  67.                     {
  68.                         if(i < processors.size())
  69.                         {
  70.                                 processors.get(i).addTransmission(trans);
  71.                                 ++i;
  72.                         }
  73.                         else
  74.                         {
  75.                                 i = 0;
  76.                         }
  77.                     }
  78.                         try
  79.                         {
  80.                                 for(Procesor proc : processors)
  81.                                 {
  82.                                         proc.runProcessor();
  83.                                 }
  84.                                
  85.                                 isFinished = true;
  86.                         }
  87.                         catch(OverDeadlineException e)
  88.                         {
  89.                                 System.out.println( e.getMessage() );
  90.                                 processors.add(createProcessor(generator.nextInt(250) + 50, res));
  91.                                
  92.                         }
  93.                         catch(InterruptedException ei)
  94.                         {
  95.                                 System.out.println(ei.getMessage());
  96.                                 isFinished = true;
  97.                         }
  98.                         catch(ResourcesException er)
  99.                         {
  100.                                 System.out.println(er.getMessage());
  101.                                 isFinished = true;
  102.                         }
  103.                         finally
  104.                         {
  105.                                 for(Procesor pro : processors)
  106.                                 {
  107.                                         pro.clearProcesor();
  108.                                 }
  109.                                 if(processors.size() > 10)
  110.                                 {
  111.                                         System.out.println(lang.getMesg(Language.Message.mesg2));
  112.                                         isFinished = true;
  113.                                 }
  114.                         }
  115.                 }
  116.                
  117.  
  118.         }
  119.  
  120. }
  121.  
  122. Processor
  123.   -----------------------------------
  124.   package example.szeregowanie;
  125.  
  126. import java.util.PriorityQueue;
  127.  
  128. public class Procesor
  129. {
  130.         private int id = 0;
  131.         static private int nextId = 0;
  132.         private double power;
  133.         Resource resources;
  134.         private double time = 0.0;
  135.         private PriorityQueue<Transmission>transmissions = new PriorityQueue<Transmission>();
  136.        
  137.         public Procesor(double power, Resource resources)
  138.         {
  139.                 id += nextId;
  140.                 this.power = power;
  141.                 this.resources = resources;
  142.         }
  143.        
  144.         void addTransmission(Transmission transmission)
  145.         {
  146.                 transmissions.add(transmission);
  147.                 System.out.println("zadanie dodano");
  148.         }
  149.        
  150.         void runProcessor() throws ResourcesException, OverDeadlineException, InterruptedException
  151.         {
  152.                 Transmission tempTrans;
  153.                 while(!transmissions.isEmpty())
  154.                 {
  155.                         tempTrans = transmissions.element();
  156.                        
  157.                         if(resources.getSize() < tempTrans.getSize())
  158.                         {
  159.                                 throw new ResourcesException();
  160.                         }
  161.                         else
  162.                         {
  163.                                 if(tempTrans.getSize()/power > tempTrans.getTransmissionDeadline())
  164.                                 {
  165.                                         throw new OverDeadlineException(tempTrans);
  166.                                 }
  167.                                 else
  168.                                 {
  169.                                         if(time <= tempTrans.getStartTime())
  170.                                         {
  171.                                                 double tempSize = resources.getSize() - tempTrans.getSize();
  172.                                                 resources.setSize(tempSize);
  173.                                                 Thread.sleep( (long)(tempTrans.getSize()/power) * 100);
  174.                                                
  175.                                                 System.out.println("Procesor: " + id + ", zadanie: " + tempTrans.getId());
  176.                                         }
  177.                                         else
  178.                                         {
  179.                                                 Thread.sleep(100);
  180.                                                 time += 0.1;
  181.                                         }
  182.                                 }
  183.                         }
  184.                        
  185.                         double tempSize = resources.getSize() + tempTrans.getSize();
  186.                         resources.setSize(tempSize);
  187.                         transmissions.remove();
  188.                        
  189.                 }
  190.         }
  191.        
  192.         void clearProcesor()
  193.         {
  194.                 this.transmissions.clear();
  195.                 this.time = 0.0;
  196.         }
  197.        
  198.         int countTransmissions()
  199.         {
  200.                 return transmissions.size();
  201.         }
  202.        
  203.         boolean isEmpty()
  204.         {
  205.                 return transmissions.isEmpty();
  206.         }
  207.        
  208.        
  209. }
  210.  
  211. Resource
  212.   ------------------------------
  213.   package example.szeregowanie;
  214.  
  215. public class Resource
  216. {
  217.         private double size;
  218.        
  219.         Resource(int size)
  220.         {
  221.                 this.size = size;
  222.         }
  223.        
  224.         void takeResource(double size) throws ResourcesException
  225.         {
  226.                 double tempResource = this.size - size;
  227.                
  228.                 if(tempResource < 0)
  229.                 {
  230.                         throw new ResourcesException();
  231.                 }
  232.                
  233.                 this.size = tempResource;
  234.         }
  235.        
  236.         void releaseResource(double size)
  237.         {
  238.                 this.size += size;
  239.         }
  240.         void setSize(double size)
  241.         {
  242.                 this.size = size;
  243.         }
  244.        
  245.         double getSize()
  246.         {
  247.                 return size;
  248.         }
  249.        
  250. }
  251.  
  252. Transmission
  253.   -----------------------------------------------------------------
  254.   package example.szeregowanie;
  255.  
  256.  
  257. public class Transmission implements Comparable<Transmission>
  258. {
  259.         static int nextId = 0;
  260.         private int id;
  261.         private double size;
  262.         private double transmissionDeadline;
  263.         private double startTime;
  264.        
  265.         Transmission(double size, double transmissionDeadline, double startTime)
  266.         {
  267.                 this.size = size;
  268.                 this.transmissionDeadline = transmissionDeadline;
  269.                 this.startTime = startTime;
  270.                 id = ++nextId;
  271.         }
  272.  
  273.         @Override
  274.         public int compareTo(Transmission transmission)
  275.         {
  276.                 if(this.startTime < transmission.startTime)
  277.                 {
  278.                         return 1;
  279.                 }
  280.             if(this.startTime > transmission.startTime)
  281.                 {
  282.                         return 0;
  283.                 }
  284.             if(this.startTime == transmission.startTime)
  285.                 {
  286.                         if(this.size > transmission.size)
  287.                         {
  288.                                 return 1;
  289.                         }
  290.                         else return 0;
  291.  
  292.                 }
  293.                 return 0;
  294.         }
  295.        
  296.         double getTransmissionDeadline()
  297.         {
  298.                 return transmissionDeadline;
  299.         }
  300.        
  301.         double getSize()
  302.         {
  303.                 return size;
  304.         }
  305.        
  306.         double getId()
  307.         {
  308.                 return id;
  309.         }
  310.        
  311.         double getStartTime()
  312.         {
  313.                 return startTime;
  314.         }
  315. }
  316.  
  317. Polish
  318.   __________________________________
  319.   package example.szeregowanie;
  320.  
  321. import java.util.HashMap;
  322.  
  323. public class Polish extends Language
  324. {
  325.         private HashMap<Message, String> map;
  326.        
  327.         Polish()
  328.         {
  329.                 super("Polish");
  330.                 map = new HashMap<>();
  331.         }
  332.         void createMap()
  333.         {
  334.                 map.put(Message.mesg1, "Przydotowywanie");
  335.                 map.put(Message.mesg2, "Przydotowywanie");
  336.                 map.put(Message.mesg3, "Przydotowywanie");
  337.                 map.put(Message.mesg4, "Przydotowywanie");
  338.                 map.put(Message.mesg5, "Przydotowywanie");
  339.         }
  340.        
  341.         @Override
  342.         String getMesg(Message mes)
  343.         {
  344.                 String message = map.get(mes);
  345.                 return message;
  346.         }
  347. }
  348.  
  349. OverDeadLineExcepion
  350.   ___________________________________________
  351.   package example.szeregowanie;
  352.  
  353. public class OverDeadlineException extends Exception
  354. {
  355.         OverDeadlineException(Transmission transmission)
  356.         {
  357.                 super("Przekroczony deadline transmisji numer : " + transmission.getId());
  358.         }
  359.         OverDeadlineException()
  360.         {
  361.                 super("Przekroczony deadline transmisji, nie znana transmisja ");
  362.         }
  363. }
  364.  
  365. class ResourcesException extends Exception
  366. {
  367.         ResourcesException()
  368.         {
  369.                 super("Brak wystarczajacych zasobów by wykonac zadanie");
  370.         }
  371.        
  372.         ResourcesException(String mesg)
  373.         {
  374.                 super(mesg);
  375.         }
  376. }
  377.  
  378. English
  379.   _________________________________________
  380.   package example.szeregowanie;
  381.  
  382. import java.util.HashMap;
  383.  
  384. public class English extends Language
  385. {
  386.         private HashMap<Message, String> map;
  387.        
  388.         English()
  389.         {
  390.                 super("English");
  391.                 map = new HashMap<>();
  392.         }
  393.         void createMap()
  394.         {
  395.                 map.put(Message.mesg1, "Preparing");
  396.                 map.put(Message.mesg2, "System Error. Too much processors");
  397.                 map.put(Message.mesg3, "Preparing");
  398.                 map.put(Message.mesg4, "Preparing");
  399.                 map.put(Message.mesg5, "Preparing");
  400.         }
  401.        
  402.         @Override
  403.         String getMesg(Message mes)
  404.         {
  405.                 String message = map.get(mes);
  406.                 return message;
  407.         }
  408. }
  409.  
  410. Language
  411. _______________________________________________________
  412.   package example.szeregowanie;
  413.  
  414. import java.util.HashMap;
  415.  
  416. abstract public class Language
  417. {
  418.          
  419.         String language;
  420.         public static enum Message
  421.         {
  422.                 mesg1, mesg2, mesg3, mesg4, mesg5;
  423.         }
  424.                
  425.         Language(String language)
  426.         {
  427.                 this.language = language;
  428.         }
  429.  
  430.         abstract void createMap();
  431.         abstract String getMesg(Message mes);
  432. }
  433.