MAin _____________________________ package example.szeregowanie; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; public class Main { public static Language lang; public static void fillTransmissions(List trans, double deadline, double transSize , double startTime, double count) { for (int i = 0; i < count; ++i) { trans.add( new Transmission(transSize, deadline, startTime) ); } } public static Procesor createProcessor(int power, Resource resources ) { return new Procesor((double)power, resources); } public static void main(String[] args) { System.out.println("Wybierz język : \n1) Polski\n2) Angielski"); Scanner scanner = new Scanner(System.in); int value = scanner.nextInt(); Polish pol = new Polish(); switch(value) { case 1: lang = new Polish(); break; case 2: lang = new English(); break; default: { System.out.println("Brak takiej wartosci. Domsylnie wybrany polski"); lang = new Polish(); } } Random generator = new Random(); Resource res = new Resource(generator.nextInt(200) + 51 ); List processors = new ArrayList(); List transmissions = new ArrayList(); processors.add(createProcessor(generator.nextInt(200) + 20, res)); fillTransmissions(transmissions,generator.nextInt(500) + 100,generator.nextInt(50) + 1, generator.nextInt(200), generator.nextInt(10) + 2 ); boolean isFinished = false; while(!isFinished) { int i = 0; for(Transmission trans : transmissions) { if(i < processors.size()) { processors.get(i).addTransmission(trans); ++i; } else { i = 0; } } try { for(Procesor proc : processors) { proc.runProcessor(); } isFinished = true; } catch(OverDeadlineException e) { System.out.println( e.getMessage() ); processors.add(createProcessor(generator.nextInt(250) + 50, res)); } catch(InterruptedException ei) { System.out.println(ei.getMessage()); isFinished = true; } catch(ResourcesException er) { System.out.println(er.getMessage()); isFinished = true; } finally { for(Procesor pro : processors) { pro.clearProcesor(); } if(processors.size() > 10) { System.out.println(lang.getMesg(Language.Message.mesg2)); isFinished = true; } } } } } Processor ----------------------------------- package example.szeregowanie; import java.util.PriorityQueue; public class Procesor { private int id = 0; static private int nextId = 0; private double power; Resource resources; private double time = 0.0; private PriorityQueuetransmissions = new PriorityQueue(); public Procesor(double power, Resource resources) { id += nextId; this.power = power; this.resources = resources; } void addTransmission(Transmission transmission) { transmissions.add(transmission); System.out.println("zadanie dodano"); } void runProcessor() throws ResourcesException, OverDeadlineException, InterruptedException { Transmission tempTrans; while(!transmissions.isEmpty()) { tempTrans = transmissions.element(); if(resources.getSize() < tempTrans.getSize()) { throw new ResourcesException(); } else { if(tempTrans.getSize()/power > tempTrans.getTransmissionDeadline()) { throw new OverDeadlineException(tempTrans); } else { if(time <= tempTrans.getStartTime()) { double tempSize = resources.getSize() - tempTrans.getSize(); resources.setSize(tempSize); Thread.sleep( (long)(tempTrans.getSize()/power) * 100); System.out.println("Procesor: " + id + ", zadanie: " + tempTrans.getId()); } else { Thread.sleep(100); time += 0.1; } } } double tempSize = resources.getSize() + tempTrans.getSize(); resources.setSize(tempSize); transmissions.remove(); } } void clearProcesor() { this.transmissions.clear(); this.time = 0.0; } int countTransmissions() { return transmissions.size(); } boolean isEmpty() { return transmissions.isEmpty(); } } Resource ------------------------------ package example.szeregowanie; public class Resource { private double size; Resource(int size) { this.size = size; } void takeResource(double size) throws ResourcesException { double tempResource = this.size - size; if(tempResource < 0) { throw new ResourcesException(); } this.size = tempResource; } void releaseResource(double size) { this.size += size; } void setSize(double size) { this.size = size; } double getSize() { return size; } } Transmission ----------------------------------------------------------------- package example.szeregowanie; public class Transmission implements Comparable { static int nextId = 0; private int id; private double size; private double transmissionDeadline; private double startTime; Transmission(double size, double transmissionDeadline, double startTime) { this.size = size; this.transmissionDeadline = transmissionDeadline; this.startTime = startTime; id = ++nextId; } @Override public int compareTo(Transmission transmission) { if(this.startTime < transmission.startTime) { return 1; } if(this.startTime > transmission.startTime) { return 0; } if(this.startTime == transmission.startTime) { if(this.size > transmission.size) { return 1; } else return 0; } return 0; } double getTransmissionDeadline() { return transmissionDeadline; } double getSize() { return size; } double getId() { return id; } double getStartTime() { return startTime; } } Polish __________________________________ package example.szeregowanie; import java.util.HashMap; public class Polish extends Language { private HashMap map; Polish() { super("Polish"); map = new HashMap<>(); } void createMap() { map.put(Message.mesg1, "Przydotowywanie"); map.put(Message.mesg2, "Przydotowywanie"); map.put(Message.mesg3, "Przydotowywanie"); map.put(Message.mesg4, "Przydotowywanie"); map.put(Message.mesg5, "Przydotowywanie"); } @Override String getMesg(Message mes) { String message = map.get(mes); return message; } } OverDeadLineExcepion ___________________________________________ package example.szeregowanie; public class OverDeadlineException extends Exception { OverDeadlineException(Transmission transmission) { super("Przekroczony deadline transmisji numer : " + transmission.getId()); } OverDeadlineException() { super("Przekroczony deadline transmisji, nie znana transmisja "); } } class ResourcesException extends Exception { ResourcesException() { super("Brak wystarczajacych zasobów by wykonac zadanie"); } ResourcesException(String mesg) { super(mesg); } } English _________________________________________ package example.szeregowanie; import java.util.HashMap; public class English extends Language { private HashMap map; English() { super("English"); map = new HashMap<>(); } void createMap() { map.put(Message.mesg1, "Preparing"); map.put(Message.mesg2, "System Error. Too much processors"); map.put(Message.mesg3, "Preparing"); map.put(Message.mesg4, "Preparing"); map.put(Message.mesg5, "Preparing"); } @Override String getMesg(Message mes) { String message = map.get(mes); return message; } } Language _______________________________________________________ package example.szeregowanie; import java.util.HashMap; abstract public class Language { String language; public static enum Message { mesg1, mesg2, mesg3, mesg4, mesg5; } Language(String language) { this.language = language; } abstract void createMap(); abstract String getMesg(Message mes); }