Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.BufferOverflowException;
  3. import java.util.HashMap;
  4. import java.util.List;
  5.  
  6. public class HW01_4 {
  7.  
  8. public static void addStudents(File file){
  9. PrintWriter pw = null;
  10. BufferedReader bf = null;
  11. try {
  12. bf = new BufferedReader(new InputStreamReader(System.in));
  13. pw = new PrintWriter(new FileWriter(file));
  14. String line = null;
  15. System.out.println("Внесете Индекс, и оцена по предметите, разделени со запирка(,).");
  16. System.out.println("Студент | КРС | НРС | АОК");
  17. while ((line = bf.readLine()) != null){
  18. pw.println(line);
  19. }
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }finally {
  23. pw.flush();
  24. pw.close();
  25. try {
  26. bf.close();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32.  
  33. public static void writeAvg(File file, String path){
  34. double KPC = 0;
  35. double HPC = 0;
  36. double AOK = 0;
  37. int brojac = 0;
  38. double avg = 0;
  39. String line = null;
  40. BufferedReader bf = null;
  41. PrintWriter pw = null;
  42. File newFile = new File(path + "\\rezultati.tsv");
  43. try {
  44. if (!newFile.exists()){
  45. newFile.createNewFile();
  46. }
  47. bf = new BufferedReader(new FileReader(file));
  48. pw = new PrintWriter(new FileWriter(newFile));
  49.  
  50. while ((line = bf.readLine()) != null){
  51. if (line.length() != 0) {
  52. String[] niza = line.split(",");
  53. for (int i = 1; i < niza.length; i++) {
  54. avg += Double.parseDouble(niza[i]);
  55. }
  56. pw.println(String.format("%s : %.2f", niza[0], (double) (avg / 3)));
  57. KPC += Double.parseDouble(niza[1]);
  58. HPC += Double.parseDouble(niza[2]);
  59. AOK += Double.parseDouble(niza[3]);
  60.  
  61. brojac++;
  62. avg = 0.0;
  63. }
  64. }
  65. pw.println(String.format("KPC: %.2f", (double)(KPC/brojac)));
  66. pw.println(String.format("HPC: %.2f", (double)(HPC/brojac)));
  67. pw.println(String.format("AOK: %.2f", (double)(AOK/brojac)));
  68. } catch (FileNotFoundException e) {
  69. e.printStackTrace();
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. finally {
  74. pw.flush();
  75. pw.close();
  76. try {
  77. bf.close();
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. }
  83.  
  84. public static void main(String[] args) {
  85. BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  86. String path = null;
  87. System.out.println("Vnesete pateka:");
  88. File rezultatiCsv = null;
  89. PrintWriter pw = null;
  90. try {
  91. path = bf.readLine();
  92. rezultatiCsv = new File(path + "rezultati.csv");
  93. if (!rezultatiCsv.exists()){
  94. rezultatiCsv.createNewFile();
  95. }
  96. }
  97. catch (IOException e) {
  98. e.printStackTrace();
  99. }
  100.  
  101. try {
  102. pw = new PrintWriter(new FileWriter(rezultatiCsv));
  103. pw.println("Студент,КРС,НРС,АОК");
  104.  
  105. } catch (IOException e) {
  106. e.printStackTrace();
  107. }finally {
  108. pw.flush();
  109. pw.close();
  110. }
  111.  
  112. addStudents(rezultatiCsv);
  113. writeAvg(rezultatiCsv, path);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement