Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.io.OutputStream;
  3. import java.io.PrintWriter;
  4. import java.util.*;
  5.  
  6. public class DailyTemperatures {
  7. private List<Temperature> dnevnaTemp;
  8.  
  9. public DailyTemperatures(){
  10. this.dnevnaTemp =new ArrayList<>();
  11. }
  12.  
  13. public void addTemperature(Temperature t){
  14. dnevnaTemp.add(t);
  15. }
  16.  
  17. public void readTemperatures(InputStream inputStream){
  18. Scanner scan=new Scanner(inputStream);
  19. while(scan.hasNextLine()) {
  20. String s = scan.nextLine();
  21. String[] split = s.split("\\s");
  22. int day = Integer.parseInt(split[0]);
  23. Temperature temp = new Temperature(day);
  24. temp.setWhich(split[0].charAt(split.length - 1));
  25. temp.addTemp(Double.parseDouble(split[0].substring(0, split[0].length() - 1)));
  26. for (int i = 1; i < split.length; i++) {
  27. temp.addTemp(Double.parseDouble(split[i].substring(0, split[i].length() - 1)));
  28. }
  29. this.dnevnaTemp.add(temp);
  30.  
  31. }
  32. /*String s=scan.next();
  33. temp.setWhich(s.charAt(s.length()-1));
  34. temp.addTemp(Double.parseDouble(s.substring(0,s.length()-1)));
  35. while(scan.hasNext()){
  36. String m=scan.next();
  37. temp.addTemp(Double.parseDouble(m.substring(0,m.length()-1)));
  38. }
  39. this.dnevnaTemp.add(temp);
  40. scan.nextLine();
  41. }*/
  42. scan.close();
  43.  
  44. }
  45.  
  46. public void writeDailyStats(OutputStream outputStream, char scale){
  47. PrintWriter pw=new PrintWriter(outputStream);
  48. Collections.sort(dnevnaTemp);
  49. if(scale == 'F'){
  50. for(Temperature t:dnevnaTemp){
  51. if(t.getWhich() == 'F'){
  52. pw.print(t);
  53. }else{
  54. t.fromCelToFar();
  55. t.setWhich('F');
  56. pw.print(t);
  57. }
  58. }
  59. }else {
  60. for(Temperature t:dnevnaTemp){
  61. if(t.getWhich() == 'C'){
  62. pw.print(t);
  63. }else{
  64. t.fromFartoCel();
  65. t.setWhich('C');
  66. pw.print(t);
  67. }
  68. }
  69. }
  70. pw.flush();
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement