Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.util.LinkedList;
- import java.util.Scanner;
- import java.util.ArrayList;
- class item implements Serializable {
- String vendorCode;
- String name;
- String cost;
- String quantity;
- void displayInfo() {
- System.out.printf("Vendor Code: %s \tName: %s \tQuantity: %s \t Cost: %s \r\n", vendorCode, name, quantity, cost);
- }
- void displaySubjects() {
- System.out.printf("Name: %s\tQuantity: %s\t Cost: %s\r\n", name, quantity, cost);
- }
- }
- public class Main {
- public static String addVendorCodeFromKeyboard() {
- return inputIndexOfVendorCode() + inputNumOfVendorCode();
- }
- public static String inputIndexOfVendorCode() {
- String str = null;
- boolean isNotCorrect;
- Scanner scan = new Scanner(System.in);
- do {
- System.out.println("Please, input the gender identifier (M - male, F - female, C - Child).");
- isNotCorrect = true;
- str = scan.nextLine().toUpperCase();
- if (str.charAt(0) == 'M' || str.charAt(0) == 'F' || str.charAt(0) == 'C') {
- isNotCorrect = false;
- } else {
- System.out.println("Input data isn't correct. Please, try again.");
- }
- } while (isNotCorrect);
- return str;
- }
- public static String inputNumOfVendorCode() {
- Scanner scanner = new Scanner(System.in);
- int number;
- do {
- System.out.println("Please enter a positive number. ( the length of number might be equal 8).");
- while (!scanner.hasNextInt()) {
- System.out.println("Please, check your value and repeat the input. ");
- scanner.next();
- }
- number = scanner.nextInt();
- } while (String.valueOf(number).length() != 8);
- return String.valueOf(number);
- }
- public static String inputCost() {
- Scanner scanner = new Scanner(System.in);
- float number;
- do {
- System.out.println("Please, enter a price ( value from 0,1 till 1000000).");
- while (!scanner.hasNextFloat()) {
- System.out.println("Please, check your value and repeat the input. ");
- scanner.next();
- }
- number = scanner.nextFloat();
- } while (!(number > 0.099f && number < 1_000_001f));
- return String.valueOf(number);
- }
- public static String inputQuantity() {
- Scanner scanner = new Scanner(System.in);
- int quantity;
- do {
- System.out.println("Please, enter shoes quantity ( from 1 to 1 000 000 000).");
- while (!scanner.hasNextInt()) {
- System.out.println("Please, check your value and repeat the input. ");
- scanner.next();
- }
- quantity = scanner.nextInt();
- } while (!(quantity > 0 && quantity < 1_000_000_001));
- return String.valueOf(quantity);
- }
- public static String inputName() {
- Scanner scanner = new Scanner(System.in);
- String name;
- do {
- System.out.println("Please, input name of shoes. ( The length name of shoes might be from 5 to 15 characters).");
- while (!scanner.hasNext()) {
- System.out.println("The length name of shoes might be from 5 to 15 characters. ");
- scanner.next();
- }
- name = scanner.nextLine();
- } while (!(name.length() > 4 && name.length() < 16));
- return name;
- }
- static void Save(LinkedList<item> Note) {
- Scanner in = new Scanner(System.in);
- boolean IsCorrect;
- Writer writer = null;
- do {
- IsCorrect = true;
- System.out.println("Enter file name:");
- String fileName = in.next();
- try {
- writer = new FileWriter(fileName);
- for (item record : Note) {
- writer.write(record.vendorCode + "\n" + record.name + "\n" + record.cost + "\n" + record.quantity);
- writer.write(System.getProperty("line.separator"));
- }
- writer.flush();
- System.out.println("Data saved successfully!");
- } catch (FileNotFoundException e) {
- System.out.println("A file with the same name was not found, try again. ");
- IsCorrect = false;
- } catch (IOException e) {
- System.out.println("File cannot be opened, try again. ");
- IsCorrect = false;
- } catch (NumberFormatException e) {
- System.out.println("Error! File contains invalid data. ");
- IsCorrect = false;
- } finally {
- if (writer != null) {
- try {
- writer.close();
- } catch (IOException ex) {
- System.out.println("Error");
- }
- }
- }
- } while (IsCorrect == false);
- }
- public static void showList(LinkedList<item> Note) {
- for (int i = 0; i <= (Note.size() - 1); i++) {
- item Student = Note.get(i);
- System.out.print((i + 1) + " ");
- Student.displayInfo();
- }
- }
- public static LinkedList<item> addNote(LinkedList<item> Note) {
- item Shoes = new item();
- System.out.println("Enter a vendor code: ");
- Shoes.vendorCode = addVendorCodeFromKeyboard();
- System.out.println("Enter name:");
- Shoes.name = inputName();
- System.out.println("Enter a quantity:");
- Shoes.quantity = inputQuantity();
- System.out.println("Enter cost: ");
- Shoes.cost = inputCost();
- Note.add(Shoes);
- showList(Note);
- return Note;
- }
- static void makeNote(LinkedList<item> note) {
- int choice;
- Scanner in = new Scanner(System.in);
- System.out.println("Choose one of the actions:" + "\r\n1:Add record." + "\r\n2:Save." + "\r\n3:Return to main menu.");
- do {
- while (!in.hasNextInt()) {
- System.out.println("Error! Repeat your input. ");
- in.next();
- }
- choice = in.nextInt();
- } while ((choice != 1) || (choice != 2) || (choice != 3));
- switch (choice) {
- case (1):
- System.out.println("Add.");
- note = addNote(note);
- makeNote(note);
- break;
- case (2):
- System.out.println("Save.");
- Save(note);
- note.clear();
- menu();
- break;
- case (3):
- note.clear();
- menu();
- break;
- }
- }
- public static int chooseActionForHelp() {
- int choice = 0;
- boolean isNotCorrect = true;
- Scanner in = new Scanner(System.in);
- while (isNotCorrect) {
- System.out.println("\r\nChoose one of the actions:" + "\r\n1:About program." + "\r\n2:About developer. " + "\r3:Return to main menu.");
- try {
- choice = in.nextInt();
- } catch (Exception e) {
- in.nextLine();
- System.out.println("Error! Repeat your input. ");
- }
- if ((choice == 1) || (choice == 2) || (choice == 3)) {
- isNotCorrect = false;
- }
- }
- return choice;
- }
- static void help() {
- switch (chooseActionForHelp()) {
- case (1):
- System.out.println("Shoe Database. This program can add, edit and delete records. You can also find shoes by article number.");
- menu();
- break;
- case (2):
- System.out.println("Zhenya Trubeko, group 951007");
- menu();
- break;
- case (3):
- menu();
- break;
- }
- }
- static LinkedList<item> readFile() {
- Scanner in = new Scanner(System.in);
- boolean IsCorrect;
- LinkedList<item> note = null;
- do {
- IsCorrect = true;
- System.out.println("Input file name:");
- String fileName = in.next();
- try {
- FileReader fileReader = new FileReader(fileName);
- BufferedReader reader = new BufferedReader(fileReader);
- String line;
- ArrayList<String> all = new ArrayList<String>();
- while ((line = reader.readLine()) != null) {
- all.add(line);
- }
- note = new LinkedList<>();
- for (int i = 0; i < all.size(); i++)
- if (i % (item.class.getDeclaredFields().length) == 0) {
- item readingRecord = new item();
- readingRecord.vendorCode = all.get(i);
- readingRecord.name = all.get(i + 1);
- readingRecord.cost = all.get(i + 2);
- readingRecord.quantity = all.get(i + 3);
- note.add(readingRecord);
- }
- } catch (FileNotFoundException e) {
- System.out.println("A file with the same name was not found, try again");
- IsCorrect = false;
- } catch (IOException e) {
- System.out.println("This file cannot be opened, try again");
- IsCorrect = false;
- } catch (NumberFormatException e) {
- System.out.println("Error! File contains invalid data.");
- IsCorrect = false;
- }
- } while (!IsCorrect);
- return note;
- }
- public static LinkedList<item> lineToChange(LinkedList<item> note) {
- Scanner in = new Scanner(System.in);
- int choice;
- int numberLine;
- System.out.println("Enter the line number you want to change.");
- do {
- while (in.hasNextInt()) {
- System.out.println("Incorrect entry! Re-enter!");
- in.next();
- }
- numberLine = in.nextInt();
- } while (!((numberLine > 0) && (numberLine < note.size() + 1)));
- item shoes = note.get(numberLine - 1);
- shoes.displayInfo();
- System.out.println("Choose one: " + "\r\nVendor code" + "\r\nName" + "\r\nQuantity" + "\r\nCost");
- do {
- while (!in.hasNextInt()) {
- System.out.println("Incorrect entry! Re-enter!");
- in.next();
- }
- choice = in.nextInt();
- } while ((choice != 1) || (choice != 2) || (choice != 3) || (choice != 4));
- changeItem(note, shoes, numberLine, choice);
- return note;
- }
- static void edit(LinkedList<item> Note) {
- Note = readFile();
- showList(Note);
- lineToChange(Note);
- showList(Note);
- Save(Note);
- menu();
- }
- public static LinkedList<item> changeItem(LinkedList<item> Note, item item, int NumberLine, int choice) {
- switch (choice) {
- case (1):
- System.out.println("Vendor Code.");
- System.out.println("Enter new value:");
- item.vendorCode = addVendorCodeFromKeyboard();
- Note.set(NumberLine - 1, item);
- break;
- case (2):
- System.out.println("Name.");
- System.out.println("Enter new value:");
- item.name = inputName();
- Note.set(NumberLine - 1, item);
- break;
- case (3):
- System.out.println("Quantity.");
- System.out.println("Enter new value:");
- item.quantity = inputQuantity();
- Note.set(NumberLine - 1, item);
- break;
- case (4):
- System.out.println("Cost.");
- System.out.println("Enter new value:");
- item.cost = inputCost();
- Note.set(NumberLine - 1, item);
- break;
- }
- return Note;
- }
- static void look(LinkedList<item> Note) {
- Scanner in = new Scanner(System.in);
- Note = readFile();
- showList(Note);
- Note.clear();
- int choice;
- System.out.println("Choose one of the following:" + "\r\n1: View entries." + "\r\n2: To the main menu.");
- do {
- while (!in.hasNextInt()) {
- in.next();
- System.out.println("Incorrect entry! Re-enter!");
- }
- choice = in.nextInt();
- } while (!(choice == 1) || (choice == 2));
- switch (choice) {
- case (1):
- Note.clear();
- look(Note);
- break;
- case (2):
- Note.clear();
- menu();
- break;
- }
- }
- static void switching(int choice, LinkedList<item> SortList, LinkedList<item> Note, String Symbol) {
- switch (choice) {
- case (1):
- for (int i = 0; i <= (Note.size() - 1); i++) {
- item Student = Note.get(i);
- if (Student.quantity.equals(Symbol)) SortList.add(Student);
- }
- break;
- }
- if (SortList.size() > 8) {
- while (SortList.size() > 8) SortList.pollLast();
- }
- showList(SortList);
- }
- public static LinkedList<item> addToList (LinkedList<item> shoes) {
- if ()
- if (code[0] == 'M') { // тута я хотел проверять первый символ и закидовать в свой лист, но что-то не пошло
- LinkedList<item> manShoes = new LinkedList<>();
- manShoes.add(addToList())
- }
- return ;
- }
- static void find(LinkedList<item> note) {
- note = readFile();
- int choice;
- System.out.println("Select your search mode:" +
- "\r\n1: Information on the availability and cost of shoes by article."
- + "\r\n2: Sorted assortment list of women's shoes.");
- Scanner in = new Scanner(System.in);
- do {
- while (in.hasNextInt()) {
- System.out.println("Error! Repeat entry.");
- in.next();
- }
- choice = in.nextInt();
- } while (choice != 1 || choice != 2);
- LinkedList<item> sortList = new LinkedList<>();
- // switching(choice, sortList, note, symbol); //
- System.out.println("Choose one of the following:" + "1: Go to search." + "2: Save." + "3: To the main menu.");
- do {
- while (in.hasNextInt()) {
- System.out.println("Error! Repeat entry.");
- in.next();
- }
- choice = in.nextInt();
- } while (choice != 1 || choice != 2 || choice != 3);
- switch (choice) {
- case (1):
- note.clear();
- sortList.clear();
- find(note);
- break;
- case (2):
- Save(sortList);
- note.clear();
- sortList.clear();
- menu();
- break;
- case (3):
- note.clear();
- sortList.clear();
- menu();
- break;
- }
- }
- public static int chooseAction() {
- int selection;
- Scanner in = new Scanner(System.in);
- do {
- while (!in.hasNextInt()) {
- System.out.println("Incorrect entry! Re-enter!");
- in.next();
- }
- selection = in.nextInt();
- } while (!(selection > 0 && selection < 7));
- return selection;
- }
- public static void menu() {
- System.out.println("\r\nChoose one of the actions:" + "\r\n1:Create." + "\r\n2:View" + "\r\n3:Find." +
- "\r\n4:Edit." + "\r\n5:Help." + "\r\n6:Exit.");
- LinkedList<item> note = new LinkedList<>();
- switch (chooseAction()) {
- case (1):
- System.out.println("Create.");
- makeNote(note);
- break;
- case (2):
- System.out.println("View.");
- look(note);
- break;
- case (3):
- System.out.println("Find.");
- find(note);
- break;
- case (4):
- System.out.println("Edit.");
- edit(note);
- break;
- case (5):
- System.out.println("Help.");
- help();
- break;
- case (6):
- System.out.println("Exit.");
- System.exit(0);
- break;
- }
- }
- public static void main(String[] args) {
- menu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement