MaksNew

Untitled

Jan 24th, 2021
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.65 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. public class Main {
  7.     record Worker(String name, int detailsInMonday, int detailsInTuesday, int detailsInWednesday, int detailsInThursday, int detailsInFriday, int detailsInSaturday) implements Serializable
  8.     {}
  9.     static Scanner scan = new Scanner(System.in);
  10.  
  11.     static String myName()
  12.     {
  13.         System.out.println("Введи имя рабочего:");
  14.         return scan.nextLine();
  15.     }
  16.  
  17.     static int detailsInSomeDay(String day)
  18.     {
  19.         boolean IsNotCorrect;
  20.         int detailsInDay = 0;
  21.         do {
  22.             System.out.println("Введи количество деталей за " + day + " :");
  23.             IsNotCorrect = false;
  24.             try {
  25.                 detailsInDay = Integer.parseInt(scan.nextLine());
  26.             } catch (Exception e) {
  27.                 IsNotCorrect = true;
  28.                 System.out.println("Введите число!.");
  29.             }
  30.             if ((((detailsInDay < 0) || (detailsInDay > 10000))) && !IsNotCorrect) {
  31.                 System.out.println("Количество деталей должно быть в диапазоне от 0 до 10000");
  32.                 IsNotCorrect = true;
  33.             }
  34.         } while (IsNotCorrect);
  35.         return detailsInDay;
  36.     }
  37.  
  38.     static int enterSize()
  39.     {
  40.         int size = 0;
  41.         boolean IsNotCorrect;
  42.         do {
  43.             System.out.println("Введите количество рабочих: ");
  44.             IsNotCorrect = false;
  45.             try {
  46.                 size = Integer.parseInt(scan.nextLine());
  47.             } catch (Exception e) {
  48.                 IsNotCorrect = true;
  49.                 System.out.println("Количество рабочих должно быть числом.");
  50.             }
  51.             if ((((size < 1) || (size > 10000))) && !IsNotCorrect) {
  52.                 System.out.println("Количество рабочих должно быть в диапазоне от 1 до 10000");
  53.                 IsNotCorrect = true;
  54.             }
  55.         } while (IsNotCorrect);
  56.         return size;
  57.     }
  58.  
  59.     static Worker[] createArrayOfRecordsFromConsole()
  60.     {
  61.         int size = enterSize();
  62.         Worker[] worker = new Worker[size];
  63.         for(int i = 0; i < size; ++i)
  64.         {
  65.             worker[i] = new Worker (myName(), detailsInSomeDay("понедельник"), detailsInSomeDay("вторник"), detailsInSomeDay("среду"), detailsInSomeDay("четверг"), detailsInSomeDay("пятницу"), detailsInSomeDay("субботу"));
  66.         }
  67.         writeArrayOfRecords(worker);
  68.         return worker;
  69.     }
  70.  
  71.     static Worker[] writeArrayOfRecordsToFile(Worker[] worker, String path) throws IOException {
  72.         FileOutputStream fos = new FileOutputStream(path);
  73.         ObjectOutputStream oos = new ObjectOutputStream(fos);
  74.         oos.writeObject(worker);
  75.         oos.close();
  76.         fos.close();
  77.         return worker;
  78.     }
  79.  
  80.     static String readPath()
  81.     {
  82.         String path;
  83.         boolean isIncorrect;
  84.         isIncorrect = true;
  85.         do {
  86.             System.out.println("Введите абсолютный путь к файлу ");
  87.             path = scan.nextLine();
  88.             File fileOfRecords = new File(path);
  89.             if (!fileOfRecords.exists())
  90.                 System.out.println("Файл не найден");
  91.             else
  92.                 isIncorrect = false;
  93.         } while (isIncorrect);
  94.         return path;
  95.     }
  96.  
  97.  
  98.     static Worker[] readArrayOfRecordsFromFile() throws IOException, ClassNotFoundException {
  99.         String path = readPath();
  100.         FileInputStream fis = new FileInputStream(path);
  101.         ObjectInputStream ois = new ObjectInputStream(fis);
  102.         Worker[] bufar = (Worker[])ois.readObject();
  103.         fis.close();
  104.         ois.close();
  105.         writeArrayOfRecords(bufar);
  106.         return bufar;
  107.     }
  108.  
  109.     static void writeArrayOfRecords(Worker[] bufar)
  110.     {
  111.         for (Worker worker : bufar) {
  112.             System.out.println(worker);
  113.         }
  114.     }
  115.  
  116.     static int selectMenuItem()
  117.     {
  118.         int menuItem = 0;
  119.         boolean isNotCorrect;
  120.         do {
  121.             isNotCorrect = false;
  122.             try
  123.             {
  124.                 menuItem = Integer.parseInt(scan.nextLine());
  125.             } catch (Exception e) {
  126.                 System.out.println("Введите число!");
  127.                 isNotCorrect = true;
  128.             }
  129.             if ((menuItem != 1) && (menuItem != 2) && (menuItem != 3)) {
  130.                 System.out.println("Введите одно из предложенных чисел!");
  131.                 isNotCorrect = true;
  132.             }
  133.         }while(isNotCorrect);
  134.         return menuItem;
  135.     }
  136.  
  137.     static int showMenu()
  138.     {
  139.         int menuItem;
  140.         System.out.println("Выберете один из пунктов меню для продолжения:");
  141.         System.out.println("1. Загрузить запись из файла.");
  142.         System.out.println("2. Создать новую запись.");
  143.         System.out.println("3. О программе.");
  144.         menuItem = selectMenuItem();
  145.         return menuItem;
  146.     }
  147.  
  148.     static void selectMenuItem(int menuItem) throws IOException, ClassNotFoundException {
  149.         switch (menuItem) {
  150.             case 1 -> readArrayOfRecordsFromFile();
  151.             case 2 -> createArrayOfRecordsFromConsole();
  152.         }
  153.     }
  154.     public static void main(String[] args) throws IOException, ClassNotFoundException {
  155.         int menuItem = showMenu();
  156.         selectMenuItem(menuItem);
  157.     }
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment