Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- import java.util.regex.PatternSyntaxException;
- public class Main {
- private static List list = null;
- private static final Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- printHi();
- showMenu();
- }
- static void printHi() {
- System.out.println("\n\n Ку \nЭта программа создана для работы с двусвязными списками\n");
- }
- static void showMenu() {
- int input;
- do {
- System.out.print("|||МЕНЮ|||\n1. Создать новый список.\n2. Добавить элемент.\n3. Удалить элемент.\n4. Показать список.\n5. Перевернуть список.\n6. Считать список из файла.\n7. Сохранить список в файл.\n0. Выход из программы.\nВыберите пункт меню: ");
- input = takeInt(7);
- userChoice(input);
- } while (input != 0);
- }
- static int takeInt(final int MAX) {
- boolean isIncorrect;
- int num = -1;
- do {
- isIncorrect = false;
- try {
- num = Integer.parseInt(sc.nextLine());
- } catch (NumberFormatException e) {
- isIncorrect = true;
- }
- if (!isIncorrect) {
- isIncorrect = num > MAX || num < 0;
- }
- System.out.print(isIncorrect ? "\nНеверно! Введите число в диапазоне от 0 до " + MAX + ": " : "");
- } while (isIncorrect);
- return num;
- }
- static void userChoice(int input) {
- switch (input) {
- case 0:
- System.out.println("Работа программы завершена");
- break;
- case 1:
- createList();
- System.out.println("\n|||СПИСОК СОЗДАН|||\n");
- break;
- case 2:
- if (list == null) {
- System.out.println("\n|||СПИСОК НЕ СОЗДАН! ДОБАВЛЕНИЕ ЭЛЕМЕНТА НЕВОЗМОЖНО|||\n");
- } else {
- addElement();
- }
- break;
- case 3:
- if (list == null) {
- System.out.println("\n|||СПИСОК НЕ СОЗДАН! УДАЛЕНИЕ ЭЛЕМЕНТА НЕВОЗМОЖНО|||\n");
- } else {
- if (list.isEmpty()) {
- System.out.println("\n|||СПИСОК ПУСТ! УДАЛЕНИЕ НЕВОЗМОЖНО|||\n");
- } else {
- deleteElement();
- }
- }
- break;
- case 4:
- if (list == null) {
- System.out.println("\n|||СПИСОК НЕ СОЗДАН! ВЫВОД НА ЭКРАН НЕВОЗМОЖЕН|||\n");
- } else {
- if (list.isEmpty()) {
- System.out.println("\n|||СПИСОК ПУСТ! ВЫВОД НА ЭКРАН НЕВОЗМОЖЕН|||\n");
- } else {
- System.out.println(list.printList());
- }
- }
- break;
- case 5:
- if (list == null) {
- System.out.println("\n|||СПИСОК НЕ СОЗДАН! ПЕРЕВОРОТ СПИСКА НЕВОЗМОЖЕН|||\n");
- } else {
- if (list.isEmpty()) {
- System.out.println("\n|||СПИСОК ПУСТ! ПЕРЕВОРОТ СПИСКА НЕВОЗМОЖЕН|||\n");
- } else {
- reverseList();
- }
- }
- break;
- case 6:
- if (list == null) {
- System.out.println("\n|||СПИСОК НЕ СОЗДАН! ЧТЕНИЕ ИЗ ФАЙЛА НЕВОЗМОЖНО|||\n");
- } else {
- readListFromFile();
- }
- break;
- case 7:
- if (list == null) {
- System.out.println("\n|||СПИСОК НЕ СОЗДАН! СОХРАНЕНИЕ В ФАЙЛ НЕВОЗМОЖНО|||\n");
- } else {
- if (list.isEmpty()) {
- System.out.println("\n|||СПИСОК ПУСТ! СОХРАНЕНИЕ В ФАЙЛ НЕВОЗМОЖНО|||\n");
- } else {
- saveListToFile();
- }
- }
- }
- }
- static void saveListToFile() {
- String path = takeOutPath();
- try {
- BufferedWriter writer = new BufferedWriter(new FileWriter(path));
- try {
- writer.write(list.printList());
- System.out.println("\n|||СПИСОК УСПЕШНО СОХРАНЁН В ФАЙЛ|||\n");
- } catch (Throwable var5) {
- try {
- writer.close();
- } catch (Throwable var4) {
- var5.addSuppressed(var4);
- }
- throw var5;
- }
- writer.close();
- } catch (IOException var6) {
- System.out.println("\n|||ОШИБКА ДОСТУПА|||\n");
- }
- }
- private static void readListFromFile() {
- String path = takeInPath();
- int size = 0;
- final int MIN_SIZE = 1;
- int lineCounter = 0;
- final int SIZE_LINE_NUMBER = 1;
- final int LIST_LINE_NUMBER = 2;
- String line;
- String[] listString = null;
- boolean isCorrect = true;
- try {
- BufferedReader reader = new BufferedReader(new FileReader(path));
- while ((isCorrect) && ((line = reader.readLine()) != null)) {
- lineCounter++;
- if (lineCounter == SIZE_LINE_NUMBER) {
- try {
- size = Integer.parseInt(line);
- } catch (Exception e) {
- System.out.println("\n|||НЕКОРЕТНЫЕ ДАННЫЕ В ФАЙЛЕ! ОШИБКА В СТРОКЕ РАЗМЕРА|||\n");
- isCorrect = false;
- }
- if (isCorrect && (size < MIN_SIZE)) {
- System.out.println("\n|||НЕКОРЕТНЫЕ ДАННЫЕ В ФАЙЛЕ! НЕКОРЕКТНО УКАЗАН РАЗМЕР!|||\n");
- isCorrect = false;
- }
- }
- if (lineCounter == LIST_LINE_NUMBER) {
- try {
- listString = line.split(" ");
- } catch (PatternSyntaxException e) {
- System.out.println("\n|||НЕКОРЕТНЫЕ ДАННЫЕ В ФАЙЛЕ! ОШИБКА В СТРОКЕ СПИСКА!|||\n");
- isCorrect = false;
- }
- }
- }
- reader.close();
- } catch (IOException e) {
- System.out.println("\n|||ОШИБКА ВВОДА/ВЫВОДА!|||\n");
- isCorrect = false;
- }
- if ((isCorrect) && (((listString != null ? listString.length : 0) != size) || (lineCounter > LIST_LINE_NUMBER))) {
- System.out.println("\n|||НЕКОРЕТНЫЕ ДАННЫЕ В ФАЙЛЕ!|||\n");
- isCorrect = false;
- } else {
- for (int i = 0; i < size; i++) {
- assert listString != null;
- list.addElem(listString[i]);
- }
- }
- if (isCorrect) {
- System.out.println("\n|||СПИСОК УСПЕШНО СЧИТАН|||\n");
- }
- }
- static void reverseList() {
- System.out.println("\n|||СПИСОК УСПЕШНО ПЕРЕВЁРНУТ|||\n");
- list.reverse();
- }
- static void addElement() {
- System.out.println("\n|||ДОБАВЛЕНИЕ ЭЛЕМЕНТА|||\n");
- System.out.print("Введите элемент: ");
- list.addElem(sc.nextLine());
- System.out.println("\n|||ЭЛЕМЕНТ ДОБАВЛЕН|||\n");
- }
- static void deleteElement() {
- System.out.println("\n|||УДАЛЕНИЕ ЭЛЕМЕНТА|||\n");
- System.out.print("Введите индекс элемента: ");
- int index = takeInt(list.getLength() - 1);
- list.deleteElem(index);
- }
- private static void createList() {
- list = new List();
- }
- static String takeInPath() {
- String path;
- boolean isIncorrect;
- System.out.print("Введите адрес файла: ");
- do {
- isIncorrect = false;
- path = sc.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.out.print("Файл не найден\nВведите адрес файла: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (!path.endsWith(".txt"))) {
- System.out.print("Файл найден, но это не \".txt\" файл\nВведите адрес файла: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- static String takeOutPath() {
- String path;
- boolean isIncorrect;
- System.out.print("Введите адрес файла: ");
- do {
- isIncorrect = false;
- path = sc.nextLine();
- if (!path.endsWith(".txt")) {
- System.out.print("Это должен быть \".txt\" файл\nВведите адрес файла: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- }
Add Comment
Please, Sign In to add comment