Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.98 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.util.LinkedList;
  5. import java.util.Scanner;
  6. import java.util.ArrayList;
  7.  
  8. class item implements Serializable {
  9. String vendorCode;
  10. String name;
  11. String cost;
  12. String quantity;
  13.  
  14. void displayInfo() {
  15. System.out.printf("Vendor Code: %s \tName: %s \tQuantity: %s \t Cost: %s \r\n", vendorCode, name, quantity, cost);
  16. }
  17.  
  18. void displaySubjects() {
  19. System.out.printf("Name: %s\tQuantity: %s\t Cost: %s\r\n", name, quantity, cost);
  20. }
  21. }
  22.  
  23. public class Main {
  24.  
  25. public static String addVendorCodeFromKeyboard() {
  26. return inputIndexOfVendorCode() + inputNumOfVendorCode();
  27. }
  28.  
  29. public static String inputIndexOfVendorCode() {
  30. String str = null;
  31. boolean isNotCorrect;
  32. Scanner scan = new Scanner(System.in);
  33. do {
  34. System.out.println("Please, input the gender identifier (M - male, F - female, C - Child).");
  35. isNotCorrect = true;
  36. str = scan.nextLine().toUpperCase();
  37. if (str.charAt(0) == 'M' || str.charAt(0) == 'F' || str.charAt(0) == 'C') {
  38. isNotCorrect = false;
  39. } else {
  40. System.out.println("Input data isn't correct. Please, try again.");
  41. }
  42. } while (isNotCorrect);
  43. return str;
  44. }
  45.  
  46. public static String inputNumOfVendorCode() {
  47. Scanner scanner = new Scanner(System.in);
  48. int number;
  49. do {
  50. System.out.println("Please enter a positive number. ( the length of number might be equal 8).");
  51. while (!scanner.hasNextInt()) {
  52. System.out.println("Please, check your value and repeat the input. ");
  53. scanner.next();
  54. }
  55. number = scanner.nextInt();
  56. } while (String.valueOf(number).length() != 8);
  57. return String.valueOf(number);
  58. }
  59.  
  60. public static String inputCost() {
  61. Scanner scanner = new Scanner(System.in);
  62. float number;
  63. do {
  64. System.out.println("Please, enter a price ( value from 0,1 till 1000000).");
  65. while (!scanner.hasNextFloat()) {
  66. System.out.println("Please, check your value and repeat the input. ");
  67. scanner.next();
  68. }
  69. number = scanner.nextFloat();
  70. } while (!(number > 0.099f && number < 1_000_001f));
  71.  
  72. return String.valueOf(number);
  73. }
  74.  
  75. public static String inputQuantity() {
  76. Scanner scanner = new Scanner(System.in);
  77. int quantity;
  78. do {
  79. System.out.println("Please, enter shoes quantity ( from 1 to 1 000 000 000).");
  80. while (!scanner.hasNextInt()) {
  81. System.out.println("Please, check your value and repeat the input. ");
  82. scanner.next();
  83. }
  84. quantity = scanner.nextInt();
  85. } while (!(quantity > 0 && quantity < 1_000_000_001));
  86. return String.valueOf(quantity);
  87. }
  88.  
  89. public static String inputName() {
  90. Scanner scanner = new Scanner(System.in);
  91. String name;
  92. do {
  93. System.out.println("Please, input name of shoes. ( The length name of shoes might be from 5 to 15 characters).");
  94. while (!scanner.hasNext()) {
  95. System.out.println("The length name of shoes might be from 5 to 15 characters. ");
  96. scanner.next();
  97. }
  98. name = scanner.nextLine();
  99. } while (!(name.length() > 4 && name.length() < 16));
  100. return name;
  101. }
  102.  
  103.  
  104. static void Save(LinkedList<item> Note) {
  105. Scanner in = new Scanner(System.in);
  106. boolean IsCorrect;
  107. Writer writer = null;
  108. do {
  109. IsCorrect = true;
  110. System.out.println("Enter file name:");
  111. String fileName = in.next();
  112. try {
  113. writer = new FileWriter(fileName);
  114. for (item record : Note) {
  115. writer.write(record.vendorCode + "\n" + record.name + "\n" + record.cost + "\n" + record.quantity);
  116. writer.write(System.getProperty("line.separator"));
  117. }
  118. writer.flush();
  119. System.out.println("Data saved successfully!");
  120. } catch (FileNotFoundException e) {
  121. System.out.println("A file with the same name was not found, try again. ");
  122. IsCorrect = false;
  123. } catch (IOException e) {
  124. System.out.println("File cannot be opened, try again. ");
  125. IsCorrect = false;
  126. } catch (NumberFormatException e) {
  127. System.out.println("Error! File contains invalid data. ");
  128. IsCorrect = false;
  129. } finally {
  130. if (writer != null) {
  131. try {
  132. writer.close();
  133. } catch (IOException ex) {
  134. System.out.println("Error");
  135. }
  136. }
  137. }
  138. } while (IsCorrect == false);
  139. }
  140.  
  141. public static void showList(LinkedList<item> Note) {
  142. for (int i = 0; i <= (Note.size() - 1); i++) {
  143. item Student = Note.get(i);
  144. System.out.print((i + 1) + " ");
  145. Student.displayInfo();
  146. }
  147. }
  148.  
  149. public static LinkedList<item> addNote(LinkedList<item> Note) {
  150. item Shoes = new item();
  151. System.out.println("Enter a vendor code: ");
  152. Shoes.vendorCode = addVendorCodeFromKeyboard();
  153. System.out.println("Enter name:");
  154. Shoes.name = inputName();
  155. System.out.println("Enter a quantity:");
  156. Shoes.quantity = inputQuantity();
  157. System.out.println("Enter cost: ");
  158. Shoes.cost = inputCost();
  159. Note.add(Shoes);
  160. showList(Note);
  161. return Note;
  162. }
  163.  
  164. static void makeNote(LinkedList<item> note) {
  165. int choice;
  166. Scanner in = new Scanner(System.in);
  167. System.out.println("Choose one of the actions:" + "\r\n1:Add record." + "\r\n2:Save." + "\r\n3:Return to main menu.");
  168. do {
  169. while (!in.hasNextInt()) {
  170. System.out.println("Error! Repeat your input. ");
  171. in.next();
  172. }
  173. choice = in.nextInt();
  174. } while ((choice != 1) || (choice != 2) || (choice != 3));
  175. switch (choice) {
  176. case (1):
  177. System.out.println("Add.");
  178. note = addNote(note);
  179. makeNote(note);
  180. break;
  181. case (2):
  182. System.out.println("Save.");
  183. Save(note);
  184. note.clear();
  185. menu();
  186. break;
  187. case (3):
  188. note.clear();
  189. menu();
  190. break;
  191. }
  192. }
  193.  
  194. public static int chooseActionForHelp() {
  195. int choice = 0;
  196. boolean isNotCorrect = true;
  197. Scanner in = new Scanner(System.in);
  198. while (isNotCorrect) {
  199. System.out.println("\r\nChoose one of the actions:" + "\r\n1:About program." + "\r\n2:About developer. " + "\r3:Return to main menu.");
  200. try {
  201. choice = in.nextInt();
  202. } catch (Exception e) {
  203. in.nextLine();
  204. System.out.println("Error! Repeat your input. ");
  205. }
  206. if ((choice == 1) || (choice == 2) || (choice == 3)) {
  207. isNotCorrect = false;
  208. }
  209. }
  210. return choice;
  211. }
  212.  
  213. static void help() {
  214. switch (chooseActionForHelp()) {
  215. case (1):
  216. System.out.println("Shoe Database. This program can add, edit and delete records. You can also find shoes by article number.");
  217. menu();
  218. break;
  219. case (2):
  220. System.out.println("Zhenya Trubeko, group 951007");
  221. menu();
  222. break;
  223. case (3):
  224. menu();
  225. break;
  226. }
  227. }
  228.  
  229. static LinkedList<item> readFile() {
  230. Scanner in = new Scanner(System.in);
  231. boolean IsCorrect;
  232. LinkedList<item> note = null;
  233. do {
  234. IsCorrect = true;
  235. System.out.println("Input file name:");
  236. String fileName = in.next();
  237. try {
  238. FileReader fileReader = new FileReader(fileName);
  239. BufferedReader reader = new BufferedReader(fileReader);
  240. String line;
  241. ArrayList<String> all = new ArrayList<String>();
  242. while ((line = reader.readLine()) != null) {
  243. all.add(line);
  244. }
  245. note = new LinkedList<>();
  246. for (int i = 0; i < all.size(); i++)
  247. if (i % (item.class.getDeclaredFields().length) == 0) {
  248. item readingRecord = new item();
  249. readingRecord.vendorCode = all.get(i);
  250. readingRecord.name = all.get(i + 1);
  251. readingRecord.cost = all.get(i + 2);
  252. readingRecord.quantity = all.get(i + 3);
  253. note.add(readingRecord);
  254. }
  255. } catch (FileNotFoundException e) {
  256. System.out.println("A file with the same name was not found, try again");
  257. IsCorrect = false;
  258. } catch (IOException e) {
  259. System.out.println("This file cannot be opened, try again");
  260. IsCorrect = false;
  261. } catch (NumberFormatException e) {
  262. System.out.println("Error! File contains invalid data.");
  263. IsCorrect = false;
  264. }
  265. } while (!IsCorrect);
  266. return note;
  267. }
  268.  
  269. public static LinkedList<item> lineToChange(LinkedList<item> note) {
  270. Scanner in = new Scanner(System.in);
  271. int choice;
  272. int numberLine;
  273. System.out.println("Enter the line number you want to change.");
  274. do {
  275. while (in.hasNextInt()) {
  276. System.out.println("Incorrect entry! Re-enter!");
  277. in.next();
  278. }
  279. numberLine = in.nextInt();
  280. } while (!((numberLine > 0) && (numberLine < note.size() + 1)));
  281. item shoes = note.get(numberLine - 1);
  282. shoes.displayInfo();
  283. System.out.println("Choose one: " + "\r\nVendor code" + "\r\nName" + "\r\nQuantity" + "\r\nCost");
  284. do {
  285. while (!in.hasNextInt()) {
  286. System.out.println("Incorrect entry! Re-enter!");
  287. in.next();
  288. }
  289. choice = in.nextInt();
  290. } while ((choice != 1) || (choice != 2) || (choice != 3) || (choice != 4));
  291. changeItem(note, shoes, numberLine, choice);
  292. return note;
  293. }
  294.  
  295. static void edit(LinkedList<item> Note) {
  296. Note = readFile();
  297. showList(Note);
  298. lineToChange(Note);
  299. showList(Note);
  300. Save(Note);
  301. menu();
  302. }
  303.  
  304. public static LinkedList<item> changeItem(LinkedList<item> Note, item item, int NumberLine, int choice) {
  305. switch (choice) {
  306. case (1):
  307. System.out.println("Vendor Code.");
  308. System.out.println("Enter new value:");
  309. item.vendorCode = addVendorCodeFromKeyboard();
  310. Note.set(NumberLine - 1, item);
  311. break;
  312. case (2):
  313. System.out.println("Name.");
  314. System.out.println("Enter new value:");
  315. item.name = inputName();
  316. Note.set(NumberLine - 1, item);
  317. break;
  318. case (3):
  319. System.out.println("Quantity.");
  320. System.out.println("Enter new value:");
  321. item.quantity = inputQuantity();
  322. Note.set(NumberLine - 1, item);
  323. break;
  324. case (4):
  325. System.out.println("Cost.");
  326. System.out.println("Enter new value:");
  327. item.cost = inputCost();
  328. Note.set(NumberLine - 1, item);
  329. break;
  330. }
  331. return Note;
  332. }
  333.  
  334. static void look(LinkedList<item> Note) {
  335. Scanner in = new Scanner(System.in);
  336. Note = readFile();
  337. showList(Note);
  338. Note.clear();
  339. int choice;
  340. System.out.println("Choose one of the following:" + "\r\n1: View entries." + "\r\n2: To the main menu.");
  341. do {
  342. while (!in.hasNextInt()) {
  343. in.next();
  344. System.out.println("Incorrect entry! Re-enter!");
  345. }
  346. choice = in.nextInt();
  347. } while (!(choice == 1) || (choice == 2));
  348. switch (choice) {
  349. case (1):
  350. Note.clear();
  351. look(Note);
  352. break;
  353. case (2):
  354. Note.clear();
  355. menu();
  356. break;
  357. }
  358. }
  359.  
  360. static void switching(int choice, LinkedList<item> SortList, LinkedList<item> Note, String Symbol) {
  361. switch (choice) {
  362. case (1):
  363. for (int i = 0; i <= (Note.size() - 1); i++) {
  364. item Student = Note.get(i);
  365. if (Student.quantity.equals(Symbol)) SortList.add(Student);
  366. }
  367. break;
  368. }
  369. if (SortList.size() > 8) {
  370. while (SortList.size() > 8) SortList.pollLast();
  371. }
  372. showList(SortList);
  373. }
  374.  
  375. public static LinkedList<item> addToList (LinkedList<item> shoes) {
  376. if ()
  377. if (code[0] == 'M') { // тута я хотел проверять первый символ и закидовать в свой лист, но что-то не пошло
  378. LinkedList<item> manShoes = new LinkedList<>();
  379. manShoes.add(addToList())
  380. }
  381. return ;
  382. }
  383.  
  384. static void find(LinkedList<item> note) {
  385. note = readFile();
  386. int choice;
  387. System.out.println("Select your search mode:" +
  388. "\r\n1: Information on the availability and cost of shoes by article."
  389. + "\r\n2: Sorted assortment list of women's shoes.");
  390. Scanner in = new Scanner(System.in);
  391. do {
  392. while (in.hasNextInt()) {
  393. System.out.println("Error! Repeat entry.");
  394. in.next();
  395. }
  396. choice = in.nextInt();
  397. } while (choice != 1 || choice != 2);
  398. LinkedList<item> sortList = new LinkedList<>();
  399. // switching(choice, sortList, note, symbol); //
  400. System.out.println("Choose one of the following:" + "1: Go to search." + "2: Save." + "3: To the main menu.");
  401. do {
  402. while (in.hasNextInt()) {
  403. System.out.println("Error! Repeat entry.");
  404. in.next();
  405. }
  406. choice = in.nextInt();
  407. } while (choice != 1 || choice != 2 || choice != 3);
  408. switch (choice) {
  409. case (1):
  410. note.clear();
  411. sortList.clear();
  412. find(note);
  413. break;
  414. case (2):
  415. Save(sortList);
  416. note.clear();
  417. sortList.clear();
  418. menu();
  419. break;
  420. case (3):
  421. note.clear();
  422. sortList.clear();
  423. menu();
  424. break;
  425. }
  426. }
  427.  
  428. public static int chooseAction() {
  429. int selection;
  430. Scanner in = new Scanner(System.in);
  431. do {
  432. while (!in.hasNextInt()) {
  433. System.out.println("Incorrect entry! Re-enter!");
  434. in.next();
  435. }
  436. selection = in.nextInt();
  437. } while (!(selection > 0 && selection < 7));
  438. return selection;
  439. }
  440.  
  441. public static void menu() {
  442. System.out.println("\r\nChoose one of the actions:" + "\r\n1:Create." + "\r\n2:View" + "\r\n3:Find." +
  443. "\r\n4:Edit." + "\r\n5:Help." + "\r\n6:Exit.");
  444. LinkedList<item> note = new LinkedList<>();
  445. switch (chooseAction()) {
  446. case (1):
  447. System.out.println("Create.");
  448. makeNote(note);
  449. break;
  450. case (2):
  451. System.out.println("View.");
  452. look(note);
  453. break;
  454. case (3):
  455. System.out.println("Find.");
  456. find(note);
  457. break;
  458. case (4):
  459. System.out.println("Edit.");
  460. edit(note);
  461. break;
  462. case (5):
  463. System.out.println("Help.");
  464. help();
  465. break;
  466. case (6):
  467. System.out.println("Exit.");
  468. System.exit(0);
  469. break;
  470. }
  471. }
  472.  
  473. public static void main(String[] args) {
  474. menu();
  475. }
  476. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement