Advertisement
lameski

Studentski dosiea

Aug 19th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.io.OutputStream;
  3. import java.io.PrintStream;
  4. import java.io.PrintWriter;
  5. import java.util.*;
  6.  
  7. /**
  8. * January 2016 Exam problem 1
  9. */
  10. public class StudentRecordsTest {
  11. public static void main(String[] args) {
  12. System.out.println("=== READING RECORDS ===");
  13. StudentRecords studentRecords = new StudentRecords();
  14. int total = studentRecords.readRecords(System.in);
  15. System.out.printf("Total records: %d\n", total);
  16. System.out.println("=== WRITING TABLE ===");
  17. studentRecords.writeTable(System.out);
  18. System.out.println("=== WRITING DISTRIBUTION ===");
  19. studentRecords.writeDistribution(System.out);
  20. }
  21. }
  22.  
  23. // your code here
  24.  
  25. class Student implements Comparable<Student> {
  26. String id;
  27. String branch;
  28. ArrayList<Integer> notes;
  29.  
  30. Student(String id, String branch, ArrayList<Integer> notes) {
  31. this.id = id;
  32. this.branch = branch;
  33. this.notes = notes;
  34. }
  35.  
  36. public long getNumTens() {
  37. return notes.stream().filter(i -> i.equals(10)).count();
  38. }
  39. public long getNumNines() {
  40. return notes.stream().filter(i -> i.equals(9)).count();
  41. }
  42. public long getNumEights() {
  43. return notes.stream().filter(i -> i.equals(8)).count();
  44. }
  45. public long getNumSevens() {
  46. return notes.stream().filter(i -> i.equals(7)).count();
  47. }
  48. public long getNumSixes() {
  49. return notes.stream().filter(i -> i.equals(6)).count();
  50. }
  51.  
  52.  
  53. public String getId() {
  54. return id;
  55. }
  56.  
  57. public Double getAvg() {
  58. return 1.0 * notes.stream().mapToInt(i -> i).sum() / notes.size();
  59. }
  60.  
  61.  
  62. public int compare(Student o1, Student o2) {
  63. // TODO Auto-generated method stub
  64. if (o1.branch.compareTo(o2.branch) == 0) {
  65. if (o1.getAvg().compareTo(o2.getAvg()) == 0) {
  66. return o1.getId().compareTo(o2.getId());
  67. }
  68. return o2.getAvg().compareTo(o1.getAvg());
  69. }
  70. return o1.branch.compareTo(o2.branch);
  71. }
  72.  
  73. @Override
  74. public int compareTo(Student arg0) {
  75. // TODO Auto-generated method stub
  76. return this.compare(this, arg0);
  77. }
  78.  
  79.  
  80. }
  81. class Branch// implements Comparable<Branch>
  82. {
  83. String branch;
  84. Map<Integer, Integer> notes;
  85.  
  86. Branch(String branch)
  87. {
  88. this.branch = branch;
  89. notes = new TreeMap<>();
  90. }
  91. public void addNote(int note)
  92. {
  93. notes.computeIfPresent(note, (key,val)->{
  94. val+=1;
  95. return val;
  96. });
  97. notes.putIfAbsent(note, 1);
  98. }
  99. public Integer getTens()
  100. {
  101. if(notes.get(10)==null)
  102. return 0;
  103. return notes.get(10);
  104. }
  105. @Override
  106. public String toString() {
  107. StringBuilder sb = new StringBuilder();
  108.  
  109. sb.append(branch).append("\n");
  110.  
  111. for(Integer i : notes.keySet())
  112. {
  113. int numStars = notes.get(i);
  114.  
  115. sb.append(String.format("%2d | ", i));
  116. if(numStars%10==0)
  117. for(int j=0; j<Math.round(numStars/10); j++)
  118. {
  119. sb.append("*");
  120. }
  121. else
  122. for(int j=0; j<Math.round(numStars/10)+1; j++)
  123. {
  124. sb.append("*");
  125. }
  126.  
  127. sb.append(String.format("(%d)\n", numStars));
  128. }
  129. return sb.toString();
  130. }
  131. /*@Override
  132. public int compareTo(Branch arg0) {
  133. return this.getTens().compareTo(arg0.getTens());
  134. }*/
  135. }
  136. class StudentRecords {
  137. TreeSet<Student> records;
  138. ArrayList<Branch> branches;
  139. public StudentRecords() {
  140. // TODO Auto-generated constructor stub
  141. records = new TreeSet<>();
  142. branches = new ArrayList<>();
  143. }
  144.  
  145. public void writeDistribution(OutputStream outputStream) {
  146. // TODO Auto-generated method stub
  147. PrintWriter pw = new PrintWriter(outputStream);
  148.  
  149. branches.stream().sorted(Comparator.comparing(Branch::getTens).reversed())
  150. .forEach(b -> {
  151. pw.print(b);
  152. });
  153. /*
  154. for(Branch b : branches)
  155. {
  156. pw.print(b);
  157. }*/
  158. pw.flush();
  159. }
  160.  
  161.  
  162. public int readRecords(InputStream inputStream) {
  163. Scanner in = new Scanner(inputStream);
  164. int j = 0;
  165. while (in.hasNextLine()) {
  166. String line[] = in.nextLine().split("\\s+");
  167.  
  168. String id = line[0];
  169. String branch = line[1];
  170. Branch b = new Branch(branch);
  171. boolean ifb = branches.stream().anyMatch(br -> br.branch.equals(branch));
  172. ArrayList<Integer> notes = new ArrayList<>();
  173. for (int i = 2; i < line.length; i++) {
  174. notes.add(Integer.parseInt(line[i]));
  175. if(ifb){
  176. final int temp =i;
  177. branches.stream().forEach(br ->
  178. {
  179. if(br.branch.equals(branch))
  180. {
  181. br.addNote(Integer.parseInt(line[temp]));
  182. }
  183.  
  184. });
  185. }
  186. else
  187. b.addNote(Integer.parseInt(line[i]));
  188. }
  189.  
  190.  
  191.  
  192.  
  193. Student st = new Student(id, branch, notes);
  194. records.add(st);
  195. if(!ifb)
  196. branches.add(b);
  197. j++;
  198.  
  199. }
  200. in.close();
  201. return j;
  202. }
  203.  
  204. public void writeTable(OutputStream outputStream) {
  205. PrintWriter pw = new PrintWriter(outputStream);
  206. StringBuilder sb = new StringBuilder();
  207. String check = "";
  208. for(Student s : records)
  209. {
  210. if(!check.equals(s.branch))
  211. {
  212. sb.append(s.branch);
  213. sb.append("\n");
  214. check = s.branch;
  215. }
  216. sb.append(String.format("%s %.2f\n", s.id, s.getAvg()));
  217. }
  218. pw.print(sb.toString());
  219. pw.flush();
  220. }
  221.  
  222.  
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement