Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.53 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 displayObjects() {
  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> findWomen_sShoes(LinkedList<item> note) {
  376. LinkedList<item> womensShoes = null;
  377. ArrayList<String> all = new ArrayList<String>();
  378. for (int i = 0; i < note.size() - 1; i++) {
  379. if (note.get(0).vendorCode.charAt(0) == 'F') {
  380. item product = new item();
  381. product.vendorCode = all.get(0);
  382. product.name = all.get(1);
  383. product.cost = all.get(2);
  384. product.quantity = all.get(3);
  385. }
  386.  
  387. }
  388. return womensShoes;
  389. }
  390.  
  391. public static String[] findByArticle(LinkedList<item> note, String vendorCode) {
  392. String[] all = new String[4];
  393. for (int i = 0; i < note.size() - 1; i++){
  394. if (note.get(i).vendorCode == vendorCode) {
  395. item product = new item();
  396. product.vendorCode = all[0];
  397. product.name = all[1];
  398. product.cost = all[2];
  399. product.quantity = all[3];
  400. }
  401. }
  402. return all;
  403. }
  404.  
  405. public static void sortWomenShoes(LinkedList<item> note) {
  406. LinkedList<item> womensShoes = findWomen_sShoes(note);
  407. item buf = new item();
  408. boolean isSorted = false;
  409. while (!isSorted) {
  410. isSorted = true;
  411. for (int i = 0; i < womensShoes.size() - 1; i++) {
  412. if ((womensShoes.get(i).vendorCode.charAt(1)) > (womensShoes.get(i + 1).vendorCode.charAt(1))) {
  413. isSorted = false;
  414. buf = womensShoes.get(i);
  415. womensShoes.set(i, womensShoes.get(i + 1));
  416. womensShoes.set(i + 1, buf);
  417. }
  418.  
  419. }
  420. }
  421. showList(womensShoes);
  422. }
  423.  
  424.  
  425. static void find(LinkedList<item> note) {
  426. int choice;
  427. System.out.println("Select your search mode:" +
  428. "\r\n1: Information on the availability and cost of shoes by article."
  429. + "\r\n2: Sorted assortment list of women's shoes.");
  430. Scanner in = new Scanner(System.in);
  431. do {
  432. while (!in.hasNextInt()) {
  433. System.out.println("Error! Repeat entry.");
  434. in.next();
  435. }
  436. choice = in.nextInt();
  437. } while (!(choice == 1 || choice == 2));
  438. switch (choice) {
  439. case (1):
  440. findByArticle(note, addVendorCodeFromKeyboard());
  441. break;
  442. case (2):
  443. sortWomenShoes(note);
  444. break;
  445. }
  446. LinkedList<item> sortList = new LinkedList<>();
  447. System.out.println("Choose one of the following:" + "\r\n1: Go to search." + "\r\n2: Save." + "\r\n3: To the main menu.");
  448. do {
  449. while (!in.hasNextInt()) {
  450. System.out.println("Error! Repeat entry.");
  451. in.next();
  452. }
  453. choice = in.nextInt();
  454. } while (!(choice == 1 || choice == 2 || choice == 3));
  455. switch (choice) {
  456. case (1):
  457. note.clear();
  458. sortList.clear();
  459. find(note);
  460. break;
  461. case (2):
  462. Save(sortList);
  463. note.clear();
  464. sortList.clear();
  465. menu();
  466. break;
  467. case (3):
  468. note.clear();
  469. sortList.clear();
  470. menu();
  471. break;
  472. }
  473. }
  474.  
  475. public static int chooseAction() {
  476. int selection;
  477. Scanner in = new Scanner(System.in);
  478. do {
  479. while (!in.hasNextInt()) {
  480. System.out.println("Incorrect entry! Re-enter!");
  481. in.next();
  482. }
  483. selection = in.nextInt();
  484. } while (!(selection > 0 && selection < 7));
  485. return selection;
  486. }
  487.  
  488. public static void menu() {
  489. System.out.println("\r\nChoose one of the actions:" + "\r\n1:Create." + "\r\n2:View" + "\r\n3:Find." +
  490. "\r\n4:Edit." + "\r\n5:Help." + "\r\n6:Exit.");
  491. LinkedList<item> note = new LinkedList<>();
  492. switch (chooseAction()) {
  493. case (1):
  494. System.out.println("Create.");
  495. makeNote(note);
  496. break;
  497. case (2):
  498. System.out.println("View.");
  499. look(note);
  500. break;
  501. case (3):
  502. System.out.println("Find.");
  503. find(note);
  504. break;
  505. case (4):
  506. System.out.println("Edit.");
  507. edit(note);
  508. break;
  509. case (5):
  510. System.out.println("Help.");
  511. help();
  512. break;
  513. case (6):
  514. System.out.println("Exit.");
  515. System.exit(0);
  516. break;
  517. }
  518. }
  519.  
  520. public static void main(String[] args) {
  521. menu();
  522. }
  523. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement