Advertisement
N_Damyanov

Main & ManagerImpl

Apr 13th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.18 KB | None | 0 0
  1. package cresla;
  2.  
  3. import cresla.Manager.ManagerImpl;
  4. import cresla.interfaces.Manager;
  5. import cresla.io.Reader;
  6. import cresla.io.Writer;
  7.  
  8. import java.util.Arrays;
  9. import java.util.List;
  10. import java.util.stream.Collectors;
  11.  
  12. public class Main {
  13.     public static void main(String[] args) {
  14.         Writer writer = new Writer();
  15.         Reader reader = new Reader();
  16.  
  17.         Manager manager = new ManagerImpl();
  18.  
  19.         whileloop:
  20.         while (true) {
  21.             String[] tokens = reader.readLine().split("\\s+");
  22.             String command = tokens[0];
  23.             List<String> arguments = Arrays.stream(tokens).skip(1).collect(Collectors.toList());
  24.  
  25.             String result = "";
  26.  
  27.             switch (command) {
  28.                 case "Reactor":
  29.                     result = manager.reactorCommand(arguments);
  30.                     break;
  31.                 case "Module":
  32.                     result = manager.moduleCommand(arguments);
  33.                     break;
  34.                 case "Report":
  35.                     result = manager.reportCommand(arguments);
  36.                     break;
  37.                 case "Exit":
  38.                     result = manager.exitCommand(arguments);
  39.                     break whileloop;
  40.             }
  41.             if (!result.isEmpty()) {
  42.                 writer.writeLine(result);
  43.             }
  44.         }
  45.     }
  46. }
  47.  
  48. ___________________________________________________________________________________________________________________
  49.  
  50. package cresla.Manager;
  51.  
  52. import cresla.entities.modules.CooldownSystem;
  53. import cresla.entities.modules.CryogenRod;
  54. import cresla.entities.modules.HeatProcessor;
  55. import cresla.entities.reactors.CryoReactor;
  56. import cresla.entities.reactors.HeatReactor;
  57. import cresla.interfaces.AbsorbingModule;
  58. import cresla.interfaces.EnergyModule;
  59. import cresla.interfaces.Module;
  60. import cresla.interfaces.Reactor;
  61.  
  62. import java.util.HashMap;
  63. import java.util.List;
  64. import java.util.Map;
  65.  
  66. public class ManagerImpl implements cresla.interfaces.Manager {
  67.  
  68.     private int id = 0;
  69.     private Map<Integer, Reactor> reactors;
  70.     private Map<Integer, Module> modules;
  71.  
  72.     public ManagerImpl() {
  73.         this.id = 1;
  74.         this.reactors = new HashMap<>();
  75.         this.modules = new HashMap<>();
  76.     }
  77.  
  78.     @Override
  79.     public String reactorCommand(List<String> arguments) {
  80.         String type = arguments.get(0);
  81.         int additionalParameter = Integer.parseInt(arguments.get(1));
  82.         int moduleCapacity = Integer.parseInt(arguments.get(2));
  83.  
  84.         Reactor reactor = null;
  85.  
  86.         if (type.equalsIgnoreCase("Cryo")) {
  87.             reactor = new CryoReactor(this.id, additionalParameter, moduleCapacity);
  88.         } else {
  89.             reactor = new HeatReactor(this.id, additionalParameter, moduleCapacity);
  90.         }
  91.         reactors.putIfAbsent(this.id, reactor);
  92.  
  93.  
  94.         return String.format("Created %s Reactor - %d", type, this.id++);
  95.     }
  96.  
  97.     @Override
  98.     public String moduleCommand(List<String> arguments) {
  99.         int reactorId = Integer.parseInt(arguments.get(0));
  100.         String type = arguments.get(1);
  101.         int additionalParameter = Integer.parseInt(arguments.get(2));
  102.  
  103.         Module module = null;
  104.  
  105.         if (type.equalsIgnoreCase("CryogenRod")) {
  106.             module = new CryogenRod(this.id, additionalParameter);
  107.             reactors.get(reactorId).addEnergyModule((EnergyModule) module);
  108.         } else if (type.equalsIgnoreCase("HeatProcessor")) {
  109.             module = new HeatProcessor(this.id, additionalParameter);
  110.             reactors.get(reactorId).addAbsorbingModule((AbsorbingModule) module);
  111.         } else if (type.equalsIgnoreCase("CooldownSystem")) {
  112.             module = new CooldownSystem(this.id, additionalParameter);
  113.             reactors.get(reactorId).addAbsorbingModule((AbsorbingModule) module);
  114.         }
  115.         modules.putIfAbsent(this.id, module);
  116.  
  117.         return String.format("Added %s - %d to Reactor - %d", type, this.id++, reactorId);
  118.     }
  119.  
  120.     @Override
  121.     public String reportCommand(List<String> arguments) {
  122.         return null;
  123.     }
  124.  
  125.     @Override
  126.     public String exitCommand(List<String> arguments) {
  127.         return null;
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement