Advertisement
Guest User

laba 12

a guest
May 22nd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.52 KB | None | 0 0
  1. 1.   package com.company;
  2. 2import java.io.*;
  3. 3import java.util.*;
  4. 4.   
  5. 5class Audience {
  6. 6.   
  7. 7.      private static List<Audience> audience = new ArrayList<>();
  8. 8.      private static File file = new File("data.bin");
  9. 9.      public static Scanner in = new Scanner(System.in);
  10. 10.  
  11. 11.     private String name;
  12. 12.     private int number;
  13. 13.     private int seats;
  14. 14.  
  15. 15.     public Audience() {
  16. 16.         System.out.print("Введите  наименование учебного корпуса: ");
  17. 17.         name = in.nextLine();
  18. 18.         System.out.print("Введите номер аудитории: ");
  19. 19.         number = Integer.parseInt(in.nextLine());
  20. 20.         System.out.print("Введите количество мест: ");
  21. 21.         seats = Integer.parseInt(in.nextLine());
  22. 22.         audience.add(this);
  23. 23.     }
  24. 24.  
  25. 25.     public static void outputEmployees() {
  26. 26.         int i = 1;
  27. 27.         for (Audience element : audience) {
  28. 28.             System.out.println((i) + ")" + element);
  29. 29.             if (element.seats >= 50) {
  30. 30.                 System.out.println("Аудитория подходит для лекций" + "\n");
  31. 31.             }
  32. 32.             i++;
  33. 33.         }
  34. 34.     }
  35. 35.  
  36. 36.     @Override
  37. 37.     public String toString() {
  38. 38.         return "НУК: " + name + "\nНомер: " + number + "\nКол-во мест: " + seats;
  39. 39.     }
  40. 40.  
  41. 41.     public static void setBinaryFile() {
  42. 42.         for (Audience emp : audience) {
  43. 43.             try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(file))) {
  44. 44.                 dos.writeUTF(emp.name);
  45. 45.                 dos.writeUTF(String.valueOf(emp.number));
  46. 46.                 dos.writeUTF(String.valueOf(emp.seats));
  47. 47.                 if (emp.seats >= 50) {
  48. 48.                     dos.writeUTF("Аудитория подходит для лекций" + "\n");
  49. 49.                 }
  50. 50.                 dos.close();
  51. 51.             } catch (IOException ex) {
  52. 52.                 System.out.println(ex.getMessage());
  53. 53.             }
  54. 54.         }
  55. 55.     }
  56. 56.  
  57. 57.     public static void getBinaryFile() throws IOException {
  58. 58.  
  59. 59.         boolean endOfFile = false;
  60. 60.         FileInputStream fStream = new FileInputStream(file);
  61. 61.         DataInputStream inputFile = new DataInputStream(fStream);
  62. 62.  
  63. 63.         while (!endOfFile) {
  64. 64.             try {
  65. 65.                 String name = inputFile.readUTF();
  66. 66.                 System.out.println(name);
  67. 67.             } catch (EOFException e) {
  68. 68.                 endOfFile = true;
  69. 69.             }
  70. 70.         }
  71. 71.         inputFile.close();
  72. 72.     }
  73. 73.  
  74. 74.     public static void setFile() throws IOException {
  75. 75.  
  76. 76.         FileWriter writer = new FileWriter("out.txt");
  77. 77.  
  78. 78.         String fName = " НУК: ";
  79. 79.         String num = " Номер аудитории: ";
  80. 80.         String seats = " Кол-во мест: ";
  81. 81.         int i = 1;
  82. 82.  
  83. 83.         for (Audience element : audience) {
  84. 84.             writer.append(i + ")" + fName + element.name + "\n");
  85. 85.             writer.append(num + element.number + "\n");
  86. 86.             writer.append(seats + element.seats + "\n");
  87. 87.             if (element.seats >= 50) {
  88. 88.                 writer.append(" Аудитория подходит для лекций" + "\n");
  89. 89.             }
  90. 90.             i++;
  91. 91.         }
  92. 92.         writer.flush();
  93. 93.         writer.close();
  94. 94.     }
  95. 95.  
  96. 96.     public static void deleteEmployees() {
  97. 97.  
  98. 98.         System.out.print("Введите НУК: ");
  99. 99.         String deleteName = in.nextLine();
  100. 100.     
  101. 101.            for (int i = 0; i < audience.size(); i++) {
  102. 102.                if (deleteName.equals(audience.get(i).name)) {
  103. 103.                    audience.remove(i);
  104. 104.                    break;
  105. 105.                }
  106. 106.            }
  107. 107.        }
  108. 108.     
  109. 109.        public static void sortList() {
  110. 110.            Collections.sort(audience, Comparator.comparing(Audience::toString));
  111. 111.        }
  112. 112.     
  113. 113.        public static void getFile() {
  114. 114.            try (FileReader fr = new FileReader("out.txt")) {
  115. 115.                int c;
  116. 116.                while ((c = fr.read()) != -1) {
  117. 117.                    System.out.print((char) c);
  118. 118.                }
  119. 119.                System.out.println();
  120. 120.            } catch (IOException ex) {
  121. 121.                System.out.println(ex.getMessage());
  122. 122.            }
  123. 123.        }
  124. 124.    }
  125. 125.     
  126. 126.    public class Main {
  127. 127.        public static void main(String[] args) throws IOException {
  128. 128.     
  129. 129.            int choose = 0;
  130. 130.     
  131. 131.            while (choose != 8) {
  132. 132.                System.out.println("1) Создать новую запись");
  133. 133.                System.out.println("2) Вывести на экран");
  134. 134.                System.out.println("3) Сохранить в бинарный файл");
  135. 135.                System.out.println("4) Загрузить из бинарного файла");
  136. 136.                System.out.println("5) Сохранить в текстовый файл");
  137. 137.                System.out.println("6) Загрузить из текстового файла");
  138. 138.                System.out.println("7) Удалить НУК");
  139. 139.                System.out.println("8) Выход");
  140. 140.     
  141. 141.                choose = Integer.parseInt(Audience.in.nextLine());
  142. 142.     
  143. 143.                switch (choose) {
  144. 144.                    case 1:
  145. 145.                        new Audience();
  146. 146.                        break;
  147. 147.                    case 2:
  148. 148.                        Audience.sortList();
  149. 149.                        Audience.outputEmployees();
  150. 150.                        break;
  151. 151.                    case 3:
  152. 152.                        Audience.setBinaryFile();
  153. 153.                        break;
  154. 154.                    case 4:
  155. 155.                        Audience.getBinaryFile();
  156. 156.                        break;
  157. 157.                    case 5:
  158. 158.                        Audience.setFile();
  159. 159.                        break;
  160. 160.                    case 6:
  161. 161.                        Audience.getFile();
  162. 162.                        break;
  163. 163.                    case 7:
  164. 164.                        Audience.deleteEmployees();
  165. 165.                        break;
  166. 166.                    case 8:
  167. 167.                        break;
  168. 168.                    default:
  169. 169.                        System.out.println("Неверно введено значение");
  170. 170.                        break;
  171. 171.                }
  172. 172.            }
  173. 173.        }
  174. 174.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement