Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.Scanner;
- public class Main {
- public enum ErrorsCode {
- CORRECT,
- INCORRECT_CHOICE,
- INCORRECT_BRAND,
- INCORRECT_MANUFACTURER,
- IS_NOT_TXT,
- IS_NOT_EXIST,
- IS_NOT_READABLE,
- IS_NOT_WRITEABLE,
- }
- static final String[] ERRORS = new String[]{
- "",
- "Incorrect choice!",
- "Incorrect brand!",
- "Incorrect manufacturer!",
- "The file extension is not .txt!",
- "Incorrect file path!",
- "The file is closed for reading!",
- "The file is closed for writing",
- "Extra data in the file!"
- };
- public static class Car {
- public int carNumber;
- public String carBrand;
- public String carManufacturer;
- public int carReleaseDate;
- public double carPrice;
- public double getCarPrice() {
- return carPrice;
- }
- }
- public enum Actions {
- Exit,
- AddCar,
- DeleteCar,
- EditCar,
- SearchCar,
- WriteFile,
- ShowCars,
- }
- static void writeError(ErrorsCode error) {
- System.err.println(ERRORS[error.ordinal()]);
- }
- static int chooseOptionWithinRange(int borderBottom, int borderTop) {
- ErrorsCode error;
- int option = 1;
- do {
- error = ErrorsCode.CORRECT;
- try {
- Scanner scanner = new Scanner(System.in);
- option = scanner.nextInt();
- } catch (Exception e) {
- error = ErrorsCode.INCORRECT_CHOICE;
- }
- if ((error == ErrorsCode.CORRECT) && ((option < borderBottom) || (option > borderTop)))
- error = ErrorsCode.INCORRECT_CHOICE;
- if (error != ErrorsCode.CORRECT) {
- writeError(error);
- System.out.print("Try again: ");
- }
- } while (error != ErrorsCode.CORRECT);
- return option;
- }
- static String readStringTypeCar(String message, String tempString, ErrorsCode probableError) {
- final int MAX_STRING_LEN = 20;
- ErrorsCode error;
- do {
- error = ErrorsCode.CORRECT;
- System.out.print(message + " [1.." + MAX_STRING_LEN + "]: ");
- Scanner scanner = new Scanner(System.in);
- tempString = scanner.nextLine();
- if (tempString.equals("") || tempString.length() > MAX_STRING_LEN) {
- error = probableError;
- writeError(error);
- }
- } while (error != ErrorsCode.CORRECT);
- return tempString;
- }
- static Car readCar() {
- ErrorsCode error;
- Car car = new Car();
- do {
- error = ErrorsCode.CORRECT;
- car.carBrand = readStringTypeCar("Enter car brand", car.carBrand, ErrorsCode.INCORRECT_BRAND);
- if (error == ErrorsCode.CORRECT)
- car.carManufacturer = readStringTypeCar("Enter manufacturer (country)", car.carManufacturer, ErrorsCode.INCORRECT_MANUFACTURER);
- if (error == ErrorsCode.CORRECT) {
- System.out.print("Enter release date (year): ");
- Scanner scanner = new Scanner(System.in);
- car.carReleaseDate = scanner.nextInt();
- }
- if (error == ErrorsCode.CORRECT) {
- System.out.print("Enter price: ");
- Scanner scanner = new Scanner(System.in);
- car.carPrice = scanner.nextDouble();
- }
- if (error != ErrorsCode.CORRECT)
- writeError(error);
- } while (error != ErrorsCode.CORRECT);
- return car;
- }
- static void sortCars(ArrayList<Car> carsList) {
- carsList.sort(Comparator.comparing(car -> car.carBrand));
- }
- static void fillCarsNumber(ArrayList<Car> carsList) {
- for (int i = 0; i < carsList.size(); i++) {
- Car tempCar = carsList.get(i);
- tempCar.carNumber = i + 1;
- carsList.set(i, tempCar);
- }
- }
- static ErrorsCode isReadable(String filePath) {
- ErrorsCode error = ErrorsCode.CORRECT;
- try {
- new BufferedReader(new FileReader(filePath)).close();
- } catch (Exception e) {
- error = ErrorsCode.IS_NOT_READABLE;
- }
- return error;
- }
- static ErrorsCode isWriteable(String filePath) {
- ErrorsCode error = ErrorsCode.CORRECT;
- try {
- new BufferedWriter(new FileWriter(filePath)).close();
- } catch (Exception e) {
- error = ErrorsCode.IS_NOT_WRITEABLE;
- }
- return error;
- }
- static String readIOFilePath(char ioMode) {
- ErrorsCode error;
- String filePath;
- do {
- error = ErrorsCode.CORRECT;
- System.out.print("Write path to file: ");
- filePath = new Scanner(System.in).nextLine();
- if (!filePath.endsWith(".txt"))
- error = ErrorsCode.IS_NOT_TXT;
- if (error == ErrorsCode.CORRECT && !new File(filePath).exists())
- error = ErrorsCode.IS_NOT_EXIST;
- if (error == ErrorsCode.CORRECT) {
- switch (ioMode) {
- case 'i':
- error = isReadable(filePath);
- break;
- case 'o':
- error = isWriteable(filePath);
- break;
- }
- }
- if (error != ErrorsCode.CORRECT)
- writeError(error);
- } while (error != ErrorsCode.CORRECT);
- return filePath;
- }
- static void writeTable(PrintWriter writer, ArrayList<Car> carsList) {
- Car car;
- writer.println("Cars:");
- for (int j = 0; j < 90; j++) writer.print("-");
- writer.printf("\n| %2s| %20s| %20s| %20s| %10s| \n", "#", "Brand", "Manufacturer", "Release date", "Price");
- for (int i = 0; i < carsList.size(); i++) {
- for (int j = 0; j < 90; j++) writer.print("-");
- car = carsList.get(i);
- writer.printf("\n| %2d| %20s| %20s| %20s| %20s| \n",
- car.carNumber, car.carBrand, car.carManufacturer, car.carReleaseDate, car.carPrice);
- }
- for (int j = 0; j < 90; j++) writer.print("-");
- writer.println();
- writer.flush();
- }
- static void writeCFData(char cfMode, ArrayList<Car> carsList) {
- PrintWriter writer = null;
- if (carsList.size() == 0)
- System.out.println("You haven't added any cars yet!");
- else {
- switch (cfMode) {
- case 'c':
- writer = new PrintWriter(System.out);
- break;
- case 'f':
- try {
- writer = new PrintWriter(new FileWriter(readIOFilePath('o')));
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- }
- writeTable(writer, carsList);
- }
- }
- static void addCar(ArrayList<Car> carsList) {
- Car tempCar;
- tempCar = readCar();
- tempCar.carNumber = carsList.size() + 1;
- carsList.add(tempCar);
- sortCars(carsList);
- fillCarsNumber(carsList);
- }
- static void deleteCar(ArrayList<Car> carsList) {
- int numberOfDeletedCar;
- if (carsList.size() == 0)
- System.out.println("You haven't added any cars yet!");
- else {
- writeCFData('c', carsList);
- System.out.print("Enter the number of the car to be deleted: ");
- numberOfDeletedCar = chooseOptionWithinRange(1, carsList.size());
- System.out.print("Are you sure you want to delete the car?:\n" +
- "1 - yes\n" +
- "2 - no\n" +
- "Your choice: ");
- if (chooseOptionWithinRange(1, 2) == 1) {
- carsList.remove(numberOfDeletedCar - 1);
- fillCarsNumber(carsList);
- }
- }
- }
- static void editCar(ArrayList<Car> carsList) {
- int numberOfEditedCar;
- if (carsList.size() == 0)
- System.out.println("You haven't added any cars yet!");
- else {
- writeCFData('c', carsList);
- System.out.print("Enter the number of the cars to be edited: ");
- numberOfEditedCar = chooseOptionWithinRange(1, carsList.size());
- carsList.set(numberOfEditedCar - 1, readCar());
- sortCars(carsList);
- fillCarsNumber(carsList);
- }
- }
- static void searchCar(ArrayList<Car> carsList) {
- if (carsList.size() == 0)
- System.out.println("You haven't added any cars yet!");
- else {
- System.out.print("Enter the year you are interested in: ");
- Scanner scanner = new Scanner(System.in);
- int year = scanner.nextInt();
- carsList.stream()
- .filter(car -> car.carReleaseDate == year)
- .sorted(Comparator.comparing(Car::getCarPrice).reversed())
- .forEach(car -> System.out.printf("Brand: %s\nManufacturer: %s\nYear: %d\nPrice: %.2f\n",
- car.carBrand, car.carManufacturer, car.carReleaseDate, car.carPrice));
- }
- }
- static void writeFile(ArrayList<Car> carsList) {
- writeCFData('f', carsList);
- }
- static void showCars(ArrayList<Car> carsList) {
- writeCFData('c', carsList);
- }
- static void exit(ArrayList<Car> carsList) {
- boolean isSaved = false;
- if (!isSaved) {
- System.out.print("You didn't save the file, do you want to save the file before exiting?\n" +
- "1 - yes\n" +
- "2 - no\n" +
- "Your choice: ");
- if (chooseOptionWithinRange(1, 2) == 1)
- writeFile(carsList);
- }
- }
- static void chooseAction(ArrayList<Car> carsList) {
- Actions action;
- boolean isSaved = false;
- do {
- System.out.print("Choose one of the following actions:\n" +
- "1 - add car(max 100)\n" +
- "2 - delete car\n" +
- "3 - edit car\n" +
- "4 - search cars\n" +
- "5 - save file\n" +
- "6 - show cars\n" +
- "0 - exit\n" +
- "Your choice: ");
- action = Actions.values()[chooseOptionWithinRange(0, Actions.values().length - 1)];
- switch (action) {
- case AddCar:
- addCar(carsList);
- break;
- case DeleteCar:
- deleteCar(carsList);
- break;
- case EditCar:
- editCar(carsList);
- break;
- case SearchCar:
- searchCar(carsList);
- break;
- case WriteFile:
- writeFile(carsList);
- break;
- case ShowCars:
- showCars(carsList);
- break;
- case Exit:
- exit(carsList);
- break;
- }
- } while (action != Actions.Exit);
- }
- public static void main(String[] args) {
- ArrayList<Car> carsList = new ArrayList<>();
- chooseAction(carsList);
- }
- }
Add Comment
Please, Sign In to add comment