Advertisement
Cinster

Untitled

Nov 9th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.Comparator;
  9.  
  10.  
  11. public class Q2_5_9{
  12.  
  13.  
  14.  
  15.  
  16. public static class FileSort implements Comparator<FileSort> {
  17.  
  18.  
  19.  
  20. private String date;
  21. private long volume;
  22.  
  23. public void set(String d, long v) {
  24.  
  25. this.date = d;
  26. this.volume = v;
  27. }
  28.  
  29.  
  30.  
  31. abstract class VolCompare{
  32. public long compare(FileSort obj1, FileSort obj2)
  33. {
  34. return obj2.volume - obj1.volume;
  35. }
  36.  
  37. }
  38.  
  39.  
  40.  
  41. public String getDate() {
  42. return this.date;
  43. }
  44.  
  45. public long getVolume() {
  46. return this.volume;
  47. }
  48.  
  49. @Override
  50. public String toString() {
  51. return "" + this.date + " " + this.volume;
  52. }
  53.  
  54. //compare method
  55. public int compare (FileSort obj1, FileSort obj2) {
  56.  
  57.  
  58. int flag = (int) (obj1.volume - obj2.volume);
  59.  
  60. if (flag == 0){
  61. flag = obj1.date.compareTo(obj2.date);
  62. }
  63.  
  64.  
  65. return flag;
  66. }
  67.  
  68. @SuppressWarnings({ "rawtypes", "unchecked" })
  69. public static void main(String[] args) throws IOException {
  70.  
  71. ArrayList arrayObjects = new ArrayList<>();
  72. FileSort fileObject= null;
  73. String[] splits= new String[2];
  74. BufferedReader br = null;
  75. PrintWriter outputStream = null;
  76.  
  77. try {
  78. br = new BufferedReader (new FileReader("Input.txt"));
  79. outputStream = new PrintWriter(new FileWriter("Output.txt"));
  80.  
  81. String line;
  82. System.out.println("Reading from input.txt ...");
  83.  
  84. //when there is a space in the file, split the data to create an object (String, long)
  85. while((line = br.readLine()) != null)
  86. {
  87. fileObject = new FileSort();
  88. splits = line.split(" ");
  89. fileObject.set(splits[0], Long.parseLong(splits[1]));
  90. arrayObjects.add(fileObject);
  91.  
  92. }
  93.  
  94. System.out.print("Unsorted File: ");
  95. System.out.println(arrayObjects);
  96.  
  97. //Sort the data based on the long
  98. System.out.println("Sorting the file ...");
  99. Collections.sort(arrayObjects, new FileSort());
  100. ArrayList arrayObject = new ArrayList<>();
  101. arrayObject = arrayObjects;
  102. System.out.println("Writting the sorted file to output.txt ...");
  103. for (Object cur : arrayObject) {
  104. outputStream.println(cur);
  105. }
  106. System.out.print("Sorted File: ");
  107. System.out.println(arrayObjects);
  108. }
  109.  
  110. finally {
  111. if (br != null) {
  112. br.close();
  113. }
  114. if (outputStream != null) {
  115. outputStream.close();
  116. }
  117. }
  118.  
  119. }
  120.  
  121.  
  122. }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement