Advertisement
vladimirVenkov

Writer/Reader Fast Template

Jul 29th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class SomeClassNameHere {
  7.  
  8. //this Writer is fast! comparedTo sout, souf etc...
  9.  
  10. static class OutputWriter {
  11.         private final PrintWriter writer;
  12.  
  13.         OutputWriter() {
  14.             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  15.         }
  16.  
  17.         void print(Object... objects) {
  18.             for (int i = 0; i < objects.length; i++) {
  19.                 if (i != 0)
  20.                     writer.print(' ');
  21.                 writer.print(objects[i]);
  22.             }
  23.         }
  24.  
  25.         void printLine(Object... objects) {
  26.             print(objects);
  27.             writer.println();
  28.         }
  29.  
  30.         void close() {
  31.             writer.close();
  32.         }
  33.     }
  34.  
  35.     private static void fakeInput() {
  36.         String input = "FAKE_INPUT_HERE";
  37.         System.setIn(new ByteArrayInputStream(input.getBytes()));
  38.     }
  39.  
  40.     public static void main(String[] args) throws IOException {
  41.         fakeInput();
  42.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   //faster than scanner!
  43.         OutputWriter out = new OutputWriter();                                     // much faster than System.out.print(f/ln)
  44.         init();
  45.         run(br, out);       //need to be send as parameters
  46.         out.close();        //close writer
  47.     }
  48.  
  49.     private static void init() {
  50.        // Data Structures Initialization here
  51.     }
  52.  
  53.     //run method
  54.     private static void run(BufferedReader br, OutputWriter out) throws IOException {
  55.         String[] command = br.readLine().split(" ");
  56.         while (!Objects.equals(command[0], "end")) {
  57.             switch (command[0]) {
  58.                 case "add":
  59.                     ..
  60.                     handleAddCommand(.., out);
  61.                     break;
  62.                 case "find":
  63.                     ..
  64.                     handleFindCommand(.., out);
  65.                     break;
  66.                 case "next":
  67.                     ..
  68.                     handleNextCommand(.., out);
  69.                     break;
  70.                 ...
  71.                 default:
  72.                     break;
  73.             }
  74.             command = br.readLine().split(" ");
  75.         }
  76.     }
  77.  
  78.     private static void handleNextCommand(.., OutputWriter out) {
  79.         //much faster than souf!!! use StringBuilder!
  80.         StringBuilder builder = new StringBuilder();
  81.             builder.append(..);
  82.             builder.append("..");
  83.             ..
  84.         builder.deleteCharAt(builder.length() - 1);
  85.         ..
  86.         out.printLine(builder);
  87.     }
  88.  
  89.     private static void handleFindCommand( .., OutputWriter out) {
  90.         StringBuilder toPrint = new StringBuilder();
  91.         toPrint.append("..").append(typeToFind).append("..");
  92.         ..
  93.         out.print(toPrint);
  94.         ..
  95.     }
  96.  
  97.     private static void handleAddCommand(.., OutputWriter out) {
  98.        
  99.         StringBuilder toPrint = new StringBuilder();
  100.         ..
  101.         out.printLine(toPrint);
  102.     }
  103.  
  104.  
  105.     private static class Player implements Comparable<Player>{
  106.         private String name;
  107.         private  String type;
  108.         private int age;
  109.         private String playerAsString; //create one time -> use many times (carefull if it should change!)
  110.  
  111.         public Player(String name, String type, int age) {
  112.             setName(name);
  113.             setType(type);
  114.             setAge(age);
  115.             playerAsString = name + "(" + age + ")";
  116.         }
  117.  
  118.  
  119.         public String getName() {
  120.             return name;
  121.         }
  122.  
  123.         public void setName(String name) {
  124.             this.name = name;
  125.         }
  126.  
  127.         public String getType() {
  128.             return type;
  129.         }
  130.  
  131.         public void setType(String type) {
  132.             this.type = type;
  133.         }
  134.  
  135.         public int getAge() {
  136.             return age;
  137.         }
  138.  
  139.         public void setAge(int age) {
  140.             this.age = age;
  141.         }
  142.  
  143.         @Override
  144.         public String toString() {
  145.             return playerAsString;
  146.         }
  147.  
  148.         @Override
  149.         public int compareTo(Player other) {
  150.             int comparisonResult = getName().compareTo(other.getName());//ascending (first current then other)
  151.             if (comparisonResult == 0) {
  152.                 comparisonResult = Integer.compare(other.getAge(), getAge());//descending (first other then current)
  153.             }
  154.             return comparisonResult;
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement