Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.*;
- public class Main {
- static Scanner scanner = new Scanner(System.in);
- public static boolean exit = false;
- public static Queue queue = new Queue();
- public static void main(String[] args) throws IOException {
- System.out.println("The program demonstrate queue as a data structure.");
- while (!exit) {
- showInfo();
- chooseAction();
- }
- }
- public static void showInfo() {
- System.out.println("1 - build an empty queue");
- System.out.println("2 - add a new item to the end of the queue");
- System.out.println("3 - remove an item from the beginning of the queue");
- System.out.println("4 - show queue");
- System.out.println("5 - open queue from file");
- System.out.println("6 - save queue in file");
- System.out.println("7 - exit");
- }
- private static int inputData() {
- int n = 0;
- boolean isIncorrect;
- final int MIN_SIZE = 1;
- do {
- isIncorrect = false;
- try {
- n = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please enter an integer number");
- }
- if (n < MIN_SIZE && !isIncorrect) {
- System.out.println("Please enter a positive number");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return n;
- }
- public static void chooseAction() throws IOException {
- int chose;
- boolean isIncorrect;
- final int MAX = 7;
- System.out.println("Please, choose:");
- do {
- chose = inputData();
- isIncorrect = false;
- if ((chose > MAX) || (chose < 1)) {
- isIncorrect = true;
- System.out.println("Please enter the correct function number.");
- }
- } while (isIncorrect);
- switch (chose) {
- case 1 -> buildQueue();
- case 2 -> addNode();
- case 3 -> removeNode();
- case 4 -> showQueue();
- case 5 -> openFile();
- case 6 -> saveData();
- case 7 -> exit = true;
- }
- }
- public static void buildQueue() {
- queue.createQueue();
- System.out.println("Ready!");
- }
- private static int inputKey() {
- int n = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- n = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please enter an integer number");
- }
- } while (isIncorrect);
- return n;
- }
- public static void addNode () {
- System.out.println("Input node:");
- int node = inputKey();
- queue.addNode(node);
- System.out.println("Node \"" + node +"\" added.");
- System.out.println(); // просто пустая строка для удобства чтения
- }
- public static void removeNode () {
- if (queue.getRoot() != null) {
- int removedNode = queue.deleteNode();
- System.out.println("Node \"" + removedNode + "\" removed.");
- }else {
- System.out.println("Queue is empty.");
- }
- System.out.println(); // просто пустая строка для удобства чтения
- }
- public static String inputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- System.out.println("Input file path:");
- path = scanner.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.out.println("Wrong way to file.");
- isIncorrect = true;
- }
- if (!file.canRead() && file.exists()) {
- System.out.println("Impossible to read a file.");
- isIncorrect = true;
- }
- if (!file.canWrite() && file.canRead()) {
- System.out.println("Impossible to write a file.");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- public static void showQueue () {
- if (queue.getRoot() != null) {
- System.out.println(queue.showQueue());
- } else {
- System.out.println("Queue is empty.");
- }
- System.out.println(); // просто пустая строка для удобства чтения
- }
- public static String[] deleteSpacesInArray (String[] keys) {
- int counterOfSpaces = 0;
- for (int j = 0; j < keys.length; j++) {
- if (!Objects.equals(keys[j], "")) {
- counterOfSpaces++;
- }
- }
- int i = 0;
- String[] newArr = new String[counterOfSpaces];
- for (int j = 0; j < keys.length; j++) {
- if (!Objects.equals(keys[j], "")) {
- newArr[i] = keys[j];
- i++;
- }
- }
- return newArr;
- }
- public static boolean checkArrayOfStrings (String[] keys) {
- for (int j = 0; j < keys.length; j++){
- try {
- Integer.parseInt(keys[j]);
- } catch (Exception e) {
- System.out.println("Check file, it shouldn't contains smth except numbers.");
- return false;
- }
- }
- return true;
- }
- public static int[] getArrOfKeys (String[] keys) {
- int[] keysFromFile = new int[keys.length];
- for (int j = 0; j < keys.length; j++) {
- keysFromFile[j] = Integer.parseInt(keys[j]);
- }
- return keysFromFile;
- }
- public static void openFile() {
- System.out.println("Example of string with nodes:");
- System.out.println("node1 - node2 - node3 - ... - nodeN");
- System.out.println("P.S. after every position expected only space");
- String path = inputFilePath();
- String str;
- String[] mas;
- try {
- Scanner fileReader = new Scanner(new File(path));
- str = fileReader.nextLine();
- mas = str.split(" ");
- mas = deleteSpacesInArray(mas);
- if (checkArrayOfStrings(mas)) {
- int[] keysFromFile = getArrOfKeys(mas);
- for (int j = 0; j < keysFromFile.length; j++) {
- queue.addNode(keysFromFile[j]);
- }
- System.out.println("Ready!");
- }
- } catch (Exception q) {
- System.out.println("Something went wrong, check file.");
- }
- }
- public static void saveData () throws IOException {
- if (queue.getRoot() != null) {
- String path = inputFilePath();
- FileWriter writer = new FileWriter(path);
- writer.write(queue.showQueue());
- writer.close();
- System.out.println("Ready!");
- } else {
- System.out.println("Queue is empty.");
- }
- }
- }
- ======================================================class2======================================================
- public class Queue {
- private int key;
- private Queue nextNode;
- public Queue getRoot() {
- return nextNode;
- }
- public void createQueue() {
- nextNode = null;
- }
- public void addNode(int value) {
- Queue newNode = new Queue();
- newNode.key = value;
- if (this.nextNode == null) {
- this.nextNode = newNode;
- } else {
- Queue currentNode = this.nextNode;
- while (currentNode.nextNode != null) {
- currentNode = currentNode.nextNode;
- }
- currentNode.nextNode = newNode;
- }
- }
- public int deleteNode() {
- int nodeToDelete = this.nextNode.key; // сохраняем значение первого элемента
- this.nextNode = this.nextNode.nextNode; // переопределяем начало очереди
- return nodeToDelete; // возвращаем удаленный элемент
- }
- public String showQueue () {
- Queue currentNode = this.nextNode;
- String result = "[ ";
- while (currentNode != null) {
- result = result + currentNode.key ;
- if (currentNode.nextNode != null){
- result = result + " - ";
- }
- currentNode = currentNode.nextNode;
- }
- result = result + " ]";
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement