Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package task;
- import java.io.*;
- import java.lang.reflect.InvocationTargetException;
- import java.util.*;
- abstract class FigureGeneral {
- private double width;
- private double height;
- private String name;
- FigureGeneral(double width, double height, String name) {
- this.width = width;
- this.height = height;
- this.name = name;
- }
- Double getWidth() {
- return width;
- }
- Double getHeight() {
- return height;
- }
- void setWidth(double width) {
- this.width = width;
- }
- void setHeight(double height) {
- this.height = height;
- }
- String getName() {
- return name;
- }
- abstract public Double area();
- public String toString() {
- return String.format(getName() + " " + "%.0f" + " " + "%.0f",
- getWidth(), getHeight());
- }
- public void toFile() {
- try {
- File theDir = new File("./serialized");
- if (!theDir.exists())
- theDir.mkdir();
- String fileName = "./serialized/" + this.getClass().getSimpleName()
- + Calendar.getInstance().getTimeInMillis() + ".txt";
- FileWriter writer = new FileWriter(new File(fileName));
- BufferedWriter out = new BufferedWriter(writer);
- writer.write(this.toString());
- out.newLine();
- writer.close();
- } catch (IOException ex) {
- System.out.println("Cannot write to file!");
- }
- }
- }
- class FiguresFiles {
- class AreaCompare implements Comparator<FigureGeneral> {
- @Override
- public int compare(FigureGeneral figure1, FigureGeneral figure2) {
- return figure1.area().compareTo(figure2.area());
- }
- }
- class WidthCompare implements Comparator<FigureGeneral> {
- @Override
- public int compare(FigureGeneral figure1, FigureGeneral figure2) {
- return figure1.getWidth().compareTo(figure2.getWidth());
- }
- }
- class HeightCompare implements Comparator<FigureGeneral> {
- @Override
- public int compare(FigureGeneral figure1, FigureGeneral figure2) {
- return figure1.getHeight().compareTo(figure2.getHeight());
- }
- }
- private ArrayList<FigureGeneral> process()
- throws FigureGeneralFilesFoundException {
- ArrayList<FigureGeneral> globalSb = new ArrayList<FigureGeneral>();
- try {
- String pathToScan = new File(".").getCanonicalPath()
- + "/serialized";
- File folderToScan = new File(pathToScan);
- File[] listOfFiles = folderToScan.listFiles();
- for (int i = 0; i < listOfFiles.length; i++) {
- if (listOfFiles[i].isFile()) {
- String targetFile = listOfFiles[i].getName();
- if (targetFile.startsWith("Triangle")
- || targetFile.startsWith("Rectangle")) {
- globalSb.add(getObjectFromFile(targetFile));
- }
- }
- }
- if (listOfFiles.length == 0) {
- throw new FigureGeneralFilesFoundException(
- "No figure files of type found.");
- }
- } catch (IOException e) {
- System.out.println("Wrong input!");
- }
- return globalSb;
- }
- private ArrayList<FigureGeneral> process(String order)
- throws FigureGeneralFilesFoundException {
- ArrayList<FigureGeneral> globalSb = new ArrayList<FigureGeneral>();
- try {
- String pathToScan = new File(".").getCanonicalPath()
- + "/serialized";
- File folderToScan = new File(pathToScan);
- File[] listOfFiles = folderToScan.listFiles();
- for (int i = 0; i < listOfFiles.length; i++) {
- if (listOfFiles[i].isFile()) {
- String targetFile = listOfFiles[i].getName();
- if (targetFile.startsWith("Triangle")
- || targetFile.startsWith("Rectangle")) {
- globalSb.add(getObjectFromFile(targetFile));
- }
- }
- }
- if (listOfFiles.length == 0) {
- throw new FigureGeneralFilesFoundException(
- "No figure files of type found.");
- }
- } catch (IOException e) {
- System.out.println("Wrong input!");
- }
- if (order.equals("area")) {
- AreaCompare areaCompare = new AreaCompare();
- Collections.sort(globalSb, areaCompare);
- } else if (order.equals("width")) {
- WidthCompare widthCompare = new WidthCompare();
- Collections.sort(globalSb, widthCompare);
- } else if (order.equals("height")) {
- HeightCompare heighCompare = new HeightCompare();
- Collections.sort(globalSb, heighCompare);
- }
- return globalSb;
- }
- private FigureGeneral getObjectFromFile(String fileName) {
- String line = null;
- ArrayList<String> myArr = new ArrayList<String>();
- fileName = "./serialized/" + fileName;
- FigureGeneral figure = null;
- while (true) {
- try {
- File myFile = new File(fileName);
- FileReader fileReader = new FileReader(myFile);
- BufferedReader reader = new BufferedReader(fileReader);
- while ((line = reader.readLine()) != null) {
- myArr = new ArrayList<String>(
- Arrays.asList(line.split(" ")));
- }
- reader.close();
- if (myArr.get(0).equals("Triangle")) {
- figure = new Triangle(Double.parseDouble(myArr.get(1)),
- Double.parseDouble(myArr.get(2)));
- } else {
- figure = new Rectangle(Double.parseDouble(myArr.get(1)),
- Double.parseDouble(myArr.get(2)));
- }
- break;
- } catch (FileNotFoundException ex) {
- System.out
- .println("File with specified name cannot be found, please try again "
- + fileName);
- break;
- } catch (IOException e) {
- System.out.println("Wrong input!");
- break;
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- break;
- } catch (SecurityException e) {
- e.printStackTrace();
- break;
- }
- }
- return figure;
- }
- public void execute() {
- try {
- System.out.println("Figures:");
- for (Object figure : process()) {
- System.out.println(figure);
- }
- } catch (FigureGeneralFilesFoundException ex) {
- System.out.println(ex.getMessage());
- }
- }
- public void execute(String order) {
- try {
- System.out.println("Figures:");
- for (Object figure : process(order)) {
- System.out.println(figure);
- }
- } catch (FigureGeneralFilesFoundException ex) {
- System.out.println(ex.getMessage());
- }
- }
- }
- @SuppressWarnings("serial")
- class FigureGeneralFilesFoundException extends Exception {
- public FigureGeneralFilesFoundException() {
- }
- public FigureGeneralFilesFoundException(String message) {
- super(message);
- }
- }
- @SuppressWarnings("serial")
- class FigureGeneralOperationException extends Exception {
- public FigureGeneralOperationException() {
- }
- public FigureGeneralOperationException(String message) {
- super(message);
- }
- }
- class Triangle extends FigureGeneral {
- Triangle(Double width, Double height) {
- super(width, height, "Triangle");
- }
- public Double area() {
- return (getWidth() * getHeight()) / 2;
- }
- }
- class Rectangle extends FigureGeneral {
- Rectangle(Double width, Double height) {
- super(width, height, "Rectangle");
- }
- public Double area() {
- return getWidth() * getHeight();
- }
- }
- public class AbsFigure {
- FiguresFiles figureFile = new FiguresFiles();
- public static void main(String[] args) {
- AbsFigure absFigure = new AbsFigure();
- absFigure.askUserOperstions();
- }
- private void askUserNumbers() {
- BufferedReader bufferedReader = new BufferedReader(
- new InputStreamReader(System.in));
- double height, width;
- Integer figureNum;
- FigureGeneral figure = null;
- while (true) {
- try {
- System.out
- .println("Please enter a figure number, \n 1.Triangle\n2.Rectangle");
- figureNum = Integer.parseInt(bufferedReader.readLine());
- if(figureNum != 1 & figureNum != 2) {
- throw new FigureGeneralOperationException(
- "Please choose number from the list!");
- }
- System.out.println("Please enter a height :");
- height = Double.parseDouble(bufferedReader.readLine());
- System.out.println("Please enter a width :");
- width = Double.parseDouble(bufferedReader.readLine());
- if (figureNum.equals(1)) {
- figure = new Triangle(width, height);
- } else if (figureNum.equals(2)) {
- figure = new Rectangle(width, height);
- }
- figure.toFile();
- System.out.println("Thank you!");
- break;
- } catch (NumberFormatException e) {
- System.out.println("Wrong number!");
- } catch (IOException e) {
- System.out.println("Wrong input!");
- } catch (FigureGeneralOperationException e) {
- System.out.println("Wrong type!");
- }
- }
- }
- public void askUserOperstions() {
- File theDir = new File("./serialized");
- if (!theDir.exists())
- theDir.mkdir();
- Integer operationNum;
- BufferedReader bufferedReader = new BufferedReader(
- new InputStreamReader(System.in));
- while (true) {
- try {
- System.out
- .println("Please choose next operation: \n 1.Print figures\n2.Create new figure\n3.Exit program");
- operationNum = Integer.parseInt(bufferedReader.readLine());
- if (operationNum.equals(1)) {
- askUserSort();
- } else if (operationNum.equals(2)) {
- askUserNumbers();
- } else if (operationNum.equals(3)) {
- break;
- } else {
- throw new FigureGeneralOperationException(
- "Please choose number from the list!");
- }
- } catch (NumberFormatException e) {
- System.out.println("Wrong number!");
- } catch (IOException e) {
- System.out.println("Wrong input!");
- } catch (FigureGeneralOperationException e) {
- System.out.println("Wrong type!");
- }
- }
- }
- private void askUserSort() {
- Integer operationNum;
- BufferedReader bufferedReader = new BufferedReader(
- new InputStreamReader(System.in));
- while (true) {
- try {
- System.out
- .println("Please choose next operation: \n 1.Print figures as is \n2.Order by area \n"
- + "3.Order by width\n4.Order by height\n5.Exit");
- operationNum = Integer.parseInt(bufferedReader.readLine());
- if (operationNum == 1) {
- figureFile.execute();
- } else if (operationNum == 2) {
- figureFile.execute("area");
- } else if (operationNum == 3) {
- figureFile.execute("width");
- } else if (operationNum == 4) {
- figureFile.execute("height");
- } else if (operationNum == 5) {
- break;
- } else {
- throw new FigureGeneralOperationException(
- "Please choose number from the list!");
- }
- } catch (NumberFormatException e) {
- System.out.println("Wrong number!");
- } catch (IOException e) {
- System.out.println("Wrong input!");
- } catch (FigureGeneralOperationException e) {
- System.out.println("Wrong type!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement