Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- import java.io.File;
- import java.io.IOException;
- public class Main {
- record Worker(String name, int detailsInMonday, int detailsInTuesday, int detailsInWednesday, int detailsInThursday, int detailsInFriday, int detailsInSaturday) implements Serializable
- {}
- static Scanner scan = new Scanner(System.in);
- static String myName()
- {
- System.out.println("Введи имя рабочего:");
- return scan.nextLine();
- }
- static int detailsInSomeDay(String day)
- {
- boolean IsNotCorrect;
- int detailsInDay = 0;
- do {
- System.out.println("Введи количество деталей за " + day + " :");
- IsNotCorrect = false;
- try {
- detailsInDay = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- IsNotCorrect = true;
- System.out.println("Введите число!.");
- }
- if ((((detailsInDay < 0) || (detailsInDay > 10000))) && !IsNotCorrect) {
- System.out.println("Количество деталей должно быть в диапазоне от 0 до 10000");
- IsNotCorrect = true;
- }
- } while (IsNotCorrect);
- return detailsInDay;
- }
- static int enterSize()
- {
- int size = 0;
- boolean IsNotCorrect;
- do {
- System.out.println("Введите количество рабочих: ");
- IsNotCorrect = false;
- try {
- size = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- IsNotCorrect = true;
- System.out.println("Количество рабочих должно быть числом.");
- }
- if ((((size < 1) || (size > 10000))) && !IsNotCorrect) {
- System.out.println("Количество рабочих должно быть в диапазоне от 1 до 10000");
- IsNotCorrect = true;
- }
- } while (IsNotCorrect);
- return size;
- }
- static Worker[] createArrayOfRecordsFromConsole()
- {
- int size = enterSize();
- Worker[] worker = new Worker[size];
- for(int i = 0; i < size; ++i)
- {
- worker[i] = new Worker (myName(), detailsInSomeDay("понедельник"), detailsInSomeDay("вторник"), detailsInSomeDay("среду"), detailsInSomeDay("четверг"), detailsInSomeDay("пятницу"), detailsInSomeDay("субботу"));
- }
- writeArrayOfRecords(worker);
- return worker;
- }
- static Worker[] writeArrayOfRecordsToFile(Worker[] worker, String path) throws IOException {
- FileOutputStream fos = new FileOutputStream(path);
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(worker);
- oos.close();
- fos.close();
- return worker;
- }
- static String readPath()
- {
- String path;
- boolean isIncorrect;
- isIncorrect = true;
- do {
- System.out.println("Введите абсолютный путь к файлу ");
- path = scan.nextLine();
- File fileOfRecords = new File(path);
- if (!fileOfRecords.exists())
- System.out.println("Файл не найден");
- else
- isIncorrect = false;
- } while (isIncorrect);
- return path;
- }
- static Worker[] readArrayOfRecordsFromFile() throws IOException, ClassNotFoundException {
- String path = readPath();
- FileInputStream fis = new FileInputStream(path);
- ObjectInputStream ois = new ObjectInputStream(fis);
- Worker[] bufar = (Worker[])ois.readObject();
- fis.close();
- ois.close();
- writeArrayOfRecords(bufar);
- return bufar;
- }
- static void writeArrayOfRecords(Worker[] bufar)
- {
- for (Worker worker : bufar) {
- System.out.println(worker);
- }
- }
- static int selectMenuItem()
- {
- int menuItem = 0;
- boolean isNotCorrect;
- do {
- isNotCorrect = false;
- try
- {
- menuItem = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- System.out.println("Введите число!");
- isNotCorrect = true;
- }
- if ((menuItem != 1) && (menuItem != 2) && (menuItem != 3)) {
- System.out.println("Введите одно из предложенных чисел!");
- isNotCorrect = true;
- }
- }while(isNotCorrect);
- return menuItem;
- }
- static int showMenu()
- {
- int menuItem;
- System.out.println("Выберете один из пунктов меню для продолжения:");
- System.out.println("1. Загрузить запись из файла.");
- System.out.println("2. Создать новую запись.");
- System.out.println("3. О программе.");
- menuItem = selectMenuItem();
- return menuItem;
- }
- static void selectMenuItem(int menuItem) throws IOException, ClassNotFoundException {
- switch (menuItem) {
- case 1 -> readArrayOfRecordsFromFile();
- case 2 -> createArrayOfRecordsFromConsole();
- }
- }
- public static void main(String[] args) throws IOException, ClassNotFoundException {
- int menuItem = showMenu();
- selectMenuItem(menuItem);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment