Advertisement
Guest User

Untitled

a guest
Jan 4th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package task;
  2.  
  3. import java.io.*;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.util.*;
  6.  
  7. abstract class FigureGeneral {
  8.     private double width;
  9.     private double height;
  10.     private String name;
  11.  
  12.     FigureGeneral(double width, double height, String name) {
  13.         this.width = width;
  14.         this.height = height;
  15.         this.name = name;
  16.     }
  17.  
  18.     Double getWidth() {
  19.         return width;
  20.     }
  21.  
  22.     Double getHeight() {
  23.         return height;
  24.     }
  25.  
  26.     void setWidth(double width) {
  27.         this.width = width;
  28.     }
  29.  
  30.     void setHeight(double height) {
  31.         this.height = height;
  32.     }
  33.  
  34.     String getName() {
  35.         return name;
  36.     }
  37.  
  38.     abstract public Double area();
  39.  
  40.     public String toString() {
  41.         return String.format(getName() + " " + "%.0f" + " " + "%.0f",
  42.                 getWidth(), getHeight());
  43.     }
  44.  
  45.     public void toFile() {
  46.         try {
  47.             File theDir = new File("./serialized");
  48.             if (!theDir.exists())
  49.                 theDir.mkdir();
  50.             String fileName = "./serialized/" + this.getClass().getSimpleName()
  51.                     + Calendar.getInstance().getTimeInMillis() + ".txt";
  52.             FileWriter writer = new FileWriter(new File(fileName));
  53.             BufferedWriter out = new BufferedWriter(writer);
  54.             writer.write(this.toString());
  55.             out.newLine();
  56.             writer.close();
  57.         } catch (IOException ex) {
  58.             System.out.println("Cannot write to file!");
  59.         }
  60.     }
  61.  
  62. }
  63.  
  64. class FiguresFiles {
  65.     class AreaCompare implements Comparator<FigureGeneral> {
  66.  
  67.         @Override
  68.         public int compare(FigureGeneral figure1, FigureGeneral figure2) {
  69.             return figure1.area().compareTo(figure2.area());
  70.         }
  71.  
  72.     }
  73.  
  74.     class WidthCompare implements Comparator<FigureGeneral> {
  75.  
  76.         @Override
  77.         public int compare(FigureGeneral figure1, FigureGeneral figure2) {
  78.             return figure1.getWidth().compareTo(figure2.getWidth());
  79.         }
  80.  
  81.     }
  82.  
  83.     class HeightCompare implements Comparator<FigureGeneral> {
  84.  
  85.         @Override
  86.         public int compare(FigureGeneral figure1, FigureGeneral figure2) {
  87.             return figure1.getHeight().compareTo(figure2.getHeight());
  88.         }
  89.  
  90.     }
  91.  
  92.     private ArrayList<FigureGeneral> process()
  93.             throws FigureGeneralFilesFoundException {
  94.         ArrayList<FigureGeneral> globalSb = new ArrayList<FigureGeneral>();
  95.         try {
  96.             String pathToScan = new File(".").getCanonicalPath()
  97.                     + "/serialized";
  98.             File folderToScan = new File(pathToScan);
  99.             File[] listOfFiles = folderToScan.listFiles();
  100.             for (int i = 0; i < listOfFiles.length; i++) {
  101.                 if (listOfFiles[i].isFile()) {
  102.                     String targetFile = listOfFiles[i].getName();
  103.                     if (targetFile.startsWith("Triangle")
  104.                             || targetFile.startsWith("Rectangle")) {
  105.                         globalSb.add(getObjectFromFile(targetFile));
  106.                     }
  107.                 }
  108.             }
  109.             if (listOfFiles.length == 0) {
  110.                 throw new FigureGeneralFilesFoundException(
  111.                         "No figure files of type  found.");
  112.             }
  113.         } catch (IOException e) {
  114.             System.out.println("Wrong input!");
  115.         }
  116.         return globalSb;
  117.     }
  118.  
  119.     private ArrayList<FigureGeneral> process(String order)
  120.             throws FigureGeneralFilesFoundException {
  121.         ArrayList<FigureGeneral> globalSb = new ArrayList<FigureGeneral>();
  122.         try {
  123.             String pathToScan = new File(".").getCanonicalPath()
  124.                     + "/serialized";
  125.             File folderToScan = new File(pathToScan);
  126.             File[] listOfFiles = folderToScan.listFiles();
  127.             for (int i = 0; i < listOfFiles.length; i++) {
  128.                 if (listOfFiles[i].isFile()) {
  129.                     String targetFile = listOfFiles[i].getName();
  130.                     if (targetFile.startsWith("Triangle")
  131.                             || targetFile.startsWith("Rectangle")) {
  132.                         globalSb.add(getObjectFromFile(targetFile));
  133.                     }
  134.                 }
  135.             }
  136.             if (listOfFiles.length == 0) {
  137.                 throw new FigureGeneralFilesFoundException(
  138.                         "No figure files of type  found.");
  139.             }
  140.         } catch (IOException e) {
  141.             System.out.println("Wrong input!");
  142.         }
  143.         if (order.equals("area")) {
  144.             AreaCompare areaCompare = new AreaCompare();
  145.             Collections.sort(globalSb, areaCompare);
  146.  
  147.         } else if (order.equals("width")) {
  148.             WidthCompare widthCompare = new WidthCompare();
  149.             Collections.sort(globalSb, widthCompare);
  150.  
  151.         } else if (order.equals("height")) {
  152.             HeightCompare heighCompare = new HeightCompare();
  153.             Collections.sort(globalSb, heighCompare);
  154.         }
  155.         return globalSb;
  156.     }
  157.  
  158.     private FigureGeneral getObjectFromFile(String fileName) {
  159.         String line = null;
  160.         ArrayList<String> myArr = new ArrayList<String>();
  161.         fileName = "./serialized/" + fileName;
  162.         FigureGeneral figure = null;
  163.         while (true) {
  164.             try {
  165.                 File myFile = new File(fileName);
  166.                 FileReader fileReader = new FileReader(myFile);
  167.                 BufferedReader reader = new BufferedReader(fileReader);
  168.                 while ((line = reader.readLine()) != null) {
  169.                     myArr = new ArrayList<String>(
  170.                             Arrays.asList(line.split(" ")));
  171.                 }
  172.                 reader.close();
  173.                 if (myArr.get(0).equals("Triangle")) {
  174.                     figure = new Triangle(Double.parseDouble(myArr.get(1)),
  175.                             Double.parseDouble(myArr.get(2)));
  176.                 } else {
  177.                     figure = new Rectangle(Double.parseDouble(myArr.get(1)),
  178.                             Double.parseDouble(myArr.get(2)));
  179.                 }
  180.  
  181.                 break;
  182.             } catch (FileNotFoundException ex) {
  183.                 System.out
  184.                         .println("File with specified name cannot be found, please try again "
  185.                                 + fileName);
  186.                 break;
  187.             } catch (IOException e) {
  188.                 System.out.println("Wrong input!");
  189.                 break;
  190.             } catch (IllegalArgumentException e) {
  191.                 e.printStackTrace();
  192.                 break;
  193.             } catch (SecurityException e) {
  194.                 e.printStackTrace();
  195.                 break;
  196.             }
  197.         }
  198.         return figure;
  199.     }
  200.  
  201.     public void execute() {
  202.         try {
  203.             System.out.println("Figures:");
  204.             for (Object figure : process()) {
  205.                 System.out.println(figure);
  206.             }
  207.         } catch (FigureGeneralFilesFoundException ex) {
  208.             System.out.println(ex.getMessage());
  209.         }
  210.     }
  211.  
  212.     public void execute(String order) {
  213.         try {
  214.             System.out.println("Figures:");
  215.             for (Object figure : process(order)) {
  216.                 System.out.println(figure);
  217.             }
  218.         } catch (FigureGeneralFilesFoundException ex) {
  219.             System.out.println(ex.getMessage());
  220.         }
  221.     }
  222. }
  223.  
  224. @SuppressWarnings("serial")
  225. class FigureGeneralFilesFoundException extends Exception {
  226.     public FigureGeneralFilesFoundException() {
  227.     }
  228.  
  229.     public FigureGeneralFilesFoundException(String message) {
  230.         super(message);
  231.     }
  232. }
  233.  
  234. @SuppressWarnings("serial")
  235. class FigureGeneralOperationException extends Exception {
  236.     public FigureGeneralOperationException() {
  237.     }
  238.  
  239.     public FigureGeneralOperationException(String message) {
  240.         super(message);
  241.     }
  242. }
  243.  
  244. class Triangle extends FigureGeneral {
  245.  
  246.     Triangle(Double width, Double height) {
  247.         super(width, height, "Triangle");
  248.     }
  249.  
  250.     public Double area() {
  251.         return (getWidth() * getHeight()) / 2;
  252.     }
  253. }
  254.  
  255. class Rectangle extends FigureGeneral {
  256.  
  257.     Rectangle(Double width, Double height) {
  258.         super(width, height, "Rectangle");
  259.     }
  260.  
  261.     public Double area() {
  262.         return getWidth() * getHeight();
  263.     }
  264.  
  265. }
  266.  
  267. public class AbsFigure {
  268.  
  269.     FiguresFiles figureFile = new FiguresFiles();
  270.  
  271.     public static void main(String[] args) {
  272.         AbsFigure absFigure = new AbsFigure();
  273.         absFigure.askUserOperstions();
  274.     }
  275.  
  276.     private void askUserNumbers() {
  277.         BufferedReader bufferedReader = new BufferedReader(
  278.                 new InputStreamReader(System.in));
  279.         double height, width;
  280.         Integer figureNum;
  281.         FigureGeneral figure = null;
  282.         while (true) {
  283.             try {
  284.                 System.out
  285.                 .println("Please enter a figure number, \n" +
  286.                         "1.Triangle\n" +
  287.                         "2.Rectangle");
  288.                 figureNum = Integer.parseInt(bufferedReader.readLine());
  289.                 if(figureNum != 1 & figureNum != 2) {
  290.                     throw new FigureGeneralOperationException(
  291.                             "Please choose number from the list!");
  292.                 }
  293.                 System.out.println("Please enter a height :");
  294.                 height = Double.parseDouble(bufferedReader.readLine());
  295.                 System.out.println("Please enter a width :");
  296.                 width = Double.parseDouble(bufferedReader.readLine());
  297.  
  298.                 if (figureNum.equals(1)) {
  299.                     figure = new Triangle(width, height);
  300.                 } else if (figureNum.equals(2)) {
  301.                     figure = new Rectangle(width, height);
  302.                 }
  303.                 figure.toFile();
  304.                 System.out.println("Thank you!");
  305.                 break;
  306.             } catch (NumberFormatException e) {
  307.                 System.out.println("Wrong number!");
  308.             } catch (IOException e) {
  309.                 System.out.println("Wrong input!");
  310.             } catch (FigureGeneralOperationException e) {
  311.                 System.out.println("Wrong type!");
  312.             }
  313.         }
  314.     }
  315.  
  316.     public void askUserOperstions() {
  317.         File theDir = new File("./serialized");
  318.         if (!theDir.exists())
  319.             theDir.mkdir();
  320.         Integer operationNum;
  321.         BufferedReader bufferedReader = new BufferedReader(
  322.                 new InputStreamReader(System.in));
  323.  
  324.         askUserOperstions:while (true) {
  325.             try {
  326.                 System.out
  327.                 .println("Please choose next operation: \n" +
  328.                         "1.Print figures\n" +
  329.                         "2.Create new figure\n" +
  330.                         "3.Exit program");
  331.                 operationNum = Integer.parseInt(bufferedReader.readLine());
  332.                 switch (operationNum) {
  333.                 case 1:  askUserSort();
  334.                          break;
  335.                 case 2: askUserNumbers();
  336.                          break;
  337.                 case 3:  break askUserOperstions;
  338.                 default: throw new FigureGeneralOperationException(
  339.                                             "Please choose number from the list!");
  340.             }
  341.             } catch (NumberFormatException e) {
  342.                 System.out.println("Wrong number!");
  343.             } catch (IOException e) {
  344.                 System.out.println("Wrong input!");
  345.             } catch (FigureGeneralOperationException e) {
  346.                 System.out.println("Wrong type!");
  347.             }
  348.         }
  349.     }
  350.  
  351.     private void askUserSort() {
  352.         Integer operationNum;
  353.         BufferedReader bufferedReader = new BufferedReader(
  354.                 new InputStreamReader(System.in));
  355.         askUserSort:while (true) {
  356.             try {
  357.                 System.out
  358.                  .println("Please choose next operation: \n" +
  359.                          "1.Print figures as is\n" +
  360.                          "2.Order by area \n"+
  361.                          "3.Order by width\n" +
  362.                          "4.Order by height\n" +
  363.                          "5.Exit");
  364.                 operationNum = Integer.parseInt(bufferedReader.readLine());
  365.                 switch (operationNum) {
  366.                 case 1:  figureFile.execute();
  367.                          break;
  368.                 case 2: figureFile.execute("area");
  369.                          break;
  370.                 case 3:  figureFile.execute("width");
  371.                          break;
  372.                 case 4:  figureFile.execute("height");
  373.                          break;
  374.                 case 5:  break askUserSort;
  375.                 default: throw new FigureGeneralOperationException(
  376.                                             "Please choose number from the list!");
  377.                 }
  378.             } catch (NumberFormatException e) {
  379.                 System.out.println("Wrong number!");
  380.             } catch (IOException e) {
  381.                 System.out.println("Wrong input!");
  382.             } catch (FigureGeneralOperationException e) {
  383.                 System.out.println("Wrong type!");
  384.             }
  385.         }
  386.     }
  387.  
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement