Advertisement
Vanya_Shestakov

Untitled

Mar 16th, 2021 (edited)
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.07 KB | None | 0 0
  1. package com.company;
  2.  
  3. import MyList.List;
  4. import MyQueue.Queue;
  5. import java.io.*;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.HashMap;
  9. import java.util.Scanner;
  10.  
  11.  
  12. //^(\{(\-?\d+\s?)+\})+$
  13. //^(\-?\d+\s?;?)+$
  14.  
  15. public class Main {
  16. static Scanner sysScan = new Scanner(System.in);
  17. static List<Queue<List<Integer>>> mainList;
  18.  
  19. public static void main(String[] args) {
  20. displayStartMenu();
  21. sysScan.close();
  22. List<Integer> aa = new List<>();
  23. }
  24.  
  25. static void displayMenu() {
  26. final int AMOUNT = 7;
  27. final int CREATE = 1;
  28. final int ADD = 2;
  29. final int DELETE = 3;
  30. final int DELETE_ALL = 4;
  31. final int VIEW = 5;
  32. final int READ = 6;
  33. final int SAVE = 7;
  34. final int CLOSE = 0;
  35. System.out.println("""
  36. ----------------------------
  37. LABA 5.1
  38. ----------------------------
  39. MAIN MENU
  40. ----------------------------
  41. 1. CREATE LIST
  42. 2. ADD ITEM
  43. 3. DELETE ITEM
  44. 4. DELETE ALL INCLUSIONS
  45. 5. VIEW LIST
  46. 6. READ DATA FROM FILE
  47. 7. SAVE LIST TO THE FILE
  48. 0. CLOSE
  49. """);
  50. int choice = inputNumber(AMOUNT);
  51. switch (choice) {
  52. case CREATE:
  53. createList();
  54. break;
  55. case ADD:
  56. addItem();
  57. break;
  58. case DELETE:
  59. deleteItem();
  60. break;
  61. case DELETE_ALL:
  62. deleteAll();
  63. break;
  64. case VIEW:
  65. showList();
  66. break;
  67. case READ:
  68. readDataFromFile();
  69. break;
  70. case SAVE:
  71. saveDataToFile();
  72. break;
  73. case CLOSE:
  74. break;
  75. }
  76. }
  77.  
  78. static void createList() {
  79. System.out.println("""
  80. -----------
  81. CREATE LIST
  82. -----------""");
  83. mainList = new List<>();
  84. System.out.println("\n*List created successfully!*");
  85. System.out.println("Press ENTER");
  86. sysScan.nextLine();
  87. displayMenu();
  88. }
  89.  
  90. static void addItem() {
  91. final int AMOUNT = 2;
  92. final int ADD_QUEUE = 1;
  93. final int ADD_LIST = 2;
  94. final int RETURN = 0;
  95. System.out.println("""
  96. --------
  97. ADD ITEM
  98. --------
  99. 1. ADD QUEUE TO MAIN LIST
  100. 2. ADD LIST TO QUEUE
  101. 0. RETURN TO MENU""");
  102. int choice = inputNumber(AMOUNT);
  103. switch (choice) {
  104. case ADD_QUEUE:
  105. addQueue();
  106. break;
  107. case ADD_LIST:
  108. addList();
  109. break;
  110. case RETURN:
  111. displayMenu();
  112. break;
  113. }
  114. }
  115.  
  116. static void deleteItem() {
  117. final int AMOUNT = 2;
  118. final int DELETE_QUEUE = 1;
  119. final int DELETE_LIST = 2;
  120. final int RETURN = 0;
  121. System.out.println("""
  122. -----------
  123. DELETE ITEM
  124. -----------
  125. 1. DELETE QUEUE FROM MAIN LIST
  126. 2. DELETE LIST FROM QUEUE
  127. 0. RETURN TO MENU""");
  128. int choice = inputNumber(AMOUNT);
  129. switch (choice) {
  130. case DELETE_QUEUE:
  131. deleteQueue();
  132. break;
  133. case DELETE_LIST:
  134. deleteList();
  135. break;
  136. case RETURN:
  137. displayMenu();
  138. break;
  139. }
  140. }
  141.  
  142. static void deleteAll() {
  143. System.out.println("""
  144. ---------------------
  145. DELETE ALL INCLUSIONS
  146. ---------------------""");
  147. System.out.println("Enter the value you want to remove:");
  148. int value = inputValue();
  149. for (int i = 0; i < mainList.size(); i++) {
  150. Queue<List<Integer>> tempQueue = new Queue<>();
  151. while (!mainList.get(i).isEmpty()) {
  152. List<Integer> tempList = mainList.get(i).poll();
  153. tempList.removeAll(value);
  154. tempQueue.push(tempList);
  155. }
  156. while (!tempQueue.isEmpty()) {
  157. mainList.get(i).push(tempQueue.poll());
  158. }
  159. }
  160. System.out.println("Press ENTER");
  161. sysScan.nextLine();
  162. displayMenu();
  163. }
  164.  
  165. static void showList() {
  166. System.out.println("""
  167. ---------
  168. MAIN LIST
  169. ---------""");
  170. System.out.println(mainList);
  171. System.out.println("Press ENTER");
  172. sysScan.nextLine();
  173. displayMenu();
  174. }
  175.  
  176. static void readDataFromFile() {
  177. System.out.println("""
  178. -------------------
  179. READ DATA FROM FILE
  180. -------------------""");
  181. System.out.println("Enter path of the file:");
  182. String path = inputPath();
  183. boolean isCorrect = true;
  184. try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
  185. String line;
  186. while (isCorrect && (line = reader.readLine()) != null) {
  187. if (line.matches("^(-?\\d+\\s?;?)+$")) {
  188. addDataFromFile(line);
  189. } else {
  190. System.out.println
  191. ("Incorrect data in the file!\n" +
  192. "Example of a valid file:\n" +
  193. "num1 num2;num3 num5 num6;num7\n" +
  194. "num1 num2;num3 num5 num6;num7\n" +
  195. "...");
  196. mainList.clear();
  197. isCorrect = false;
  198. }
  199. }
  200. } catch (IOException e) {
  201. System.out.println("Input error!");
  202. }
  203. String succMessage = isCorrect ? "Data read successfully!\n" : "";
  204. System.out.println(succMessage + "Press ENTER");
  205. sysScan.nextLine();
  206. displayMenu();
  207. }
  208.  
  209. static void saveDataToFile() {
  210. System.out.println("""
  211. -----------------
  212. SAVE DATA TO FILE
  213. -----------------""");
  214. System.out.println("Enter path of the file:");
  215. String path = inputPath();
  216. try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) {
  217. writer.append(mainList.toString());
  218. System.out.println("*Data saved successfully!*");
  219. } catch (IOException e) {
  220. System.out.println("Output error!");
  221. }
  222. System.out.println("Press ENTER");
  223. sysScan.nextLine();
  224. displayMenu();
  225. }
  226.  
  227. static void addQueue() {
  228. Queue<List<Integer>> queue = new Queue<>();
  229. mainList.add(queue);
  230. System.out.println("\n*Queue added successfully!*");
  231. System.out.println("Press ENTER");
  232. sysScan.nextLine();
  233. displayMenu();
  234. }
  235.  
  236. static void addList() {
  237. if (!mainList.isEmpty()) {
  238. final int MAX_SIZE = 15;
  239. List<Integer> list = new List<>();
  240. System.out.println("Choose the queue:");
  241. showQueues();
  242. int index = inputNumber(mainList.size() - 1);
  243. System.out.println("Enter size of the list:");
  244. int size = inputNumber(MAX_SIZE);
  245. for (int i = 0; i < size; i++) {
  246. System.out.println("Enter value " + (i + 1) + ": ");
  247. int value = inputValue();
  248. list.add(value);
  249. }
  250. mainList.get(index).push(list);
  251. System.out.println("\n*List added successfully!*");
  252. } else {
  253. System.out.println("Main list is empty! Add queues to main list firstly!");
  254. }
  255. System.out.println("Press ENTER");
  256. sysScan.nextLine();
  257. displayMenu();
  258. }
  259.  
  260. static void deleteQueue() {
  261. if (!mainList.isEmpty()) {
  262. System.out.println("Choose the queue:");
  263. showQueues();
  264. int index = inputNumber(mainList.size()) - 1;
  265. mainList.removeAt(index);
  266. System.out.println("\n*Queue deleted successfully!*");
  267.  
  268. } else {
  269. System.out.println("Main list is empty! Nothing to delete!");
  270. }
  271. System.out.println("Press ENTER");
  272. sysScan.nextLine();
  273. displayMenu();
  274. }
  275.  
  276. static void deleteList() {
  277. if (!mainList.isEmpty()) {
  278. System.out.println("Choose the queue:");
  279. showQueues();
  280. int index = inputNumber(mainList.size() - 1);
  281. if (!mainList.get(index).isEmpty()) {
  282. mainList.get(index).poll();
  283. System.out.println("\n*List deleted successfully!*");
  284. } else {
  285. System.out.println("This queue is empty! Nothing to delete!");
  286. }
  287. } else {
  288. System.out.println("Main list is empty! Nothing to delete!");
  289. }
  290. System.out.println("Press ENTER");
  291. sysScan.nextLine();
  292. displayMenu();
  293. }
  294.  
  295. static void showQueues(){
  296. for (int i = 0; i < mainList.size(); i++) {
  297. System.out.println((i)+ ". " + mainList.get(i).toString());
  298. }
  299. }
  300.  
  301. static void addDataFromFile(String line) {
  302. String[] queues = line.split(";");
  303. Queue<List<Integer>> currQueue = new Queue<>();
  304. for (String queue: queues) {
  305. String[] values = queue.split(" ");
  306. List<Integer> currList = new List<>();
  307. for (String value: values) {
  308. currList.add(Integer.parseInt(value));
  309. }
  310. currQueue.push(currList);
  311. }
  312. mainList.add(currQueue);
  313. }
  314.  
  315. static String inputPath() {
  316. boolean isNotExists;
  317. String path;
  318. do {
  319. isNotExists = false;
  320. path = sysScan.nextLine();
  321. File file = new File(path);
  322. if (!file.exists()) {
  323. System.err.println("File not found. Repeat enter!");
  324. isNotExists = true;
  325. }
  326. } while (isNotExists);
  327. return path;
  328. }
  329.  
  330. static int inputValue() {
  331. int value = 0;
  332. boolean isIncorrect;
  333. do {
  334. isIncorrect = false;
  335. try {
  336. value = Integer.parseInt(sysScan.nextLine());
  337. } catch (NumberFormatException e) {
  338. System.err.println("Enter the integer number!");
  339. isIncorrect = true;
  340. }
  341. } while (isIncorrect);
  342. return value;
  343. }
  344.  
  345. static int inputNumber(int range) {
  346. int number = 0;
  347. boolean isIncorrect;
  348. do {
  349. isIncorrect = false;
  350. try {
  351. number = Integer.parseInt(sysScan.nextLine());
  352. } catch (NumberFormatException e) {
  353. System.err.println("Enter the integer number!");
  354. isIncorrect = true;
  355. }
  356. if (!isIncorrect && (number < 0 || number > range)) {
  357. System.err.println("Enter the number in range [0; "+ range +"]");
  358. isIncorrect = true;
  359. }
  360. } while (isIncorrect);
  361. return number;
  362. }
  363.  
  364. static void displayStartMenu() {
  365. final int AMOUNT = 7;
  366. System.out.println("""
  367. ----------------------------
  368. LABA 5.1
  369. ----------------------------
  370. MAIN MENU
  371. ----------------------------
  372. 1. CREATE LIST
  373. 2. *ADD ITEM*
  374. 3. *DELETE ITEM*
  375. 4. *DELETE ALL INCLUSIONS*
  376. 5. *VIEW LIST*
  377. 6. *READ ITEMS FROM FILE*
  378. 7. *SAVE LIST TO THE FILE*
  379. 0. CLOSE
  380. """);
  381. int choice = inputNumber(AMOUNT);
  382. if (choice == 1) {
  383. createList();
  384. } else if (choice > 1 && choice < 8) {
  385. System.out.println("\nThe list does not exist, create it!");
  386. System.out.println("Press ENTER");
  387. sysScan.nextLine();
  388. displayStartMenu();
  389. }
  390. }
  391. }
  392.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement