Advertisement
nazar_art

AbsFigure Random Variant

Jan 3rd, 2013
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.87 KB | None | 0 0
  1. package task.to.soft;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. abstract class FigureGeneral {
  7.     private double width;
  8.     private double height;
  9.     private String name;
  10.    
  11.     FigureGeneral(double width, double height, String name){
  12.         this.width = width;
  13.         this.height = height;
  14.         this.name = name;
  15.     }
  16.    
  17.     double getWidth(){ return width; }
  18.     double getHeight(){ return height; }
  19.     void setWidth(double width){ this.width = width; }
  20.     void setHeight(double height){ this.height = height; }
  21.    
  22.     String getName(){ return name; }
  23.    
  24.     abstract public double area();
  25.    
  26.     public String toString(){
  27.         return getName() + " " + getHeight()+ " " + getWidth();
  28.     }
  29. }
  30.  
  31. class Triangle extends FigureGeneral {
  32.        
  33.     Triangle(double width, double height, String name) {
  34.         super(width, height, "triangle");
  35.     }
  36.  
  37.     public double area() {
  38.         return (getWidth() * getHeight()) / 2;
  39.     }
  40. }
  41.  
  42. class Rectangle extends FigureGeneral {
  43.    
  44.     Rectangle(double width, double height, String name) {
  45.         super(width, height, "rectangle");
  46.     }
  47.  
  48.     public double area() {
  49.         return getWidth() * getHeight();
  50.     }
  51. }
  52.  
  53. @SuppressWarnings("serial")
  54. class FigureGeneralFilesFoundException extends Exception {
  55.         public FigureGeneralFilesFoundException() {
  56.         }
  57.  
  58.         public FigureGeneralFilesFoundException(String message) {
  59.                 super(message);
  60.         }
  61. }
  62.  
  63. @SuppressWarnings("serial")
  64. class FigureGeneralNumberFormatException extends Exception {
  65.         public FigureGeneralNumberFormatException() {
  66.         }
  67.  
  68.         public FigureGeneralNumberFormatException(String message) {
  69.                 super(message);
  70.         }
  71. }
  72.  
  73. public class AbsFigure {
  74.  
  75.     class AreaCompare implements Comparator<FigureGeneral> {
  76.  
  77.     @Override
  78.     public int compare(FigureGeneral oneFigure, FigureGeneral twoFigure) {
  79.             double firstValue = oneFigure.area();
  80.             double secondValue = twoFigure.area();
  81.             int result = 0;
  82.  
  83.             if (firstValue > secondValue)
  84.                 result = 1;
  85.             else if (firstValue < secondValue)
  86.                 result = -1;
  87.             else
  88.                 result = 0;
  89.  
  90.             return result;
  91.         }
  92.     }
  93.  
  94.     public void write(String fileName, List<FigureGeneral> figuresLisr) {
  95.         try {
  96.             PrintWriter out = new PrintWriter(
  97.                     new File(fileName).getAbsoluteFile());
  98.             try {
  99.                 for (int i = 0; i < figuresLisr.size(); i++) {
  100.                     out.println(figuresLisr.get(i).toString());
  101.                 }
  102.             } finally {
  103.                 out.close();
  104.             }
  105.         } catch (IOException e) {
  106.             e.printStackTrace();
  107.             System.out.println("Cannot write to file!");
  108.         }
  109.     }
  110.    
  111.     // read from file
  112.     private FigureGeneral fromStringToObject(String fileName){
  113.         String line = null;
  114.         ArrayList<String> myList = new ArrayList<String>();
  115.         FigureGeneral shape = null;
  116.        
  117.         while (true) {
  118.         try{
  119.             File myFile = new File(fileName);
  120.             FileReader fileReader = new FileReader(myFile);
  121.             BufferedReader bufferedReader = new BufferedReader(fileReader);
  122.             while((line = bufferedReader.readLine()) != null){
  123.                 myList = new ArrayList<String>
  124.                 (Arrays.asList(line.split(" ")));
  125.             }
  126.             bufferedReader.close();
  127.             if(myList.get(0).equals("triangle")){
  128.                 shape = new Triangle(Double.parseDouble(myList.get(1)),
  129.                         Double.parseDouble(myList.get(2)), "triangle");
  130.             }else{
  131.                 shape = new Rectangle(Double.parseDouble(myList.get(1)),
  132.                         Double.parseDouble(myList.get(2)), "rectangle");
  133.             }
  134.             System.out.println(shape);
  135.             break;
  136.         }catch(FileNotFoundException e){
  137.             System.out.println("File which You loking for not found!");
  138.             break;
  139.         }catch(NumberFormatException e){
  140.             e.printStackTrace();
  141.             break;
  142.         }catch(IOException e){
  143.             System.out.println("Wrong input!");
  144.             break;
  145.         }
  146.         }
  147.        
  148.         return shape;
  149.     }
  150.     //}
  151.  
  152.     // make random values
  153.     protected FigureGeneral getRandomFigure() {
  154.         Random randValues = new Random();
  155.  
  156.         double randomWidth = Math.abs(randValues.nextInt() % 10)+1;
  157.         double randomHeight = Math.abs(randValues.nextInt() % 10)+1;
  158.         int randomName = Math.abs(randValues.nextInt() % 2);
  159.         if (randomName == 0) {
  160.             return new Triangle(randomWidth, randomHeight, "triangle");
  161.         } else {
  162.             return new Rectangle(randomWidth, randomHeight, "rectangle");
  163.         }
  164.     }
  165.  
  166.     // make Collections
  167.     @SuppressWarnings({ "unchecked", "rawtypes" })
  168.     List<FigureGeneral> generateRandomFigures(int count) {
  169.         ArrayList listOfFigures = new ArrayList();
  170.  
  171.         for (int i = 0; i < count; i++) {
  172.             listOfFigures.add(getRandomFigure());
  173.         }
  174.  
  175.         return listOfFigures;
  176.     }
  177.    
  178.     public void working(AbsFigure absFigure){
  179.         List<FigureGeneral> figuresList = absFigure.generateRandomFigures(5);
  180.  
  181.         AreaCompare areaCompare = absFigure.new AreaCompare();
  182.         Collections.sort(figuresList, areaCompare);
  183.  
  184.         absFigure.write("test.txt", figuresList);
  185.         fromStringToObject("test.txt");
  186.     }
  187.  
  188.     /**
  189.      * @param args
  190.      * @throws IOException
  191.      *            
  192.      */
  193.     public static void main(String[] args) {
  194.         AbsFigure absFigure = new AbsFigure();
  195.         absFigure.working(absFigure);
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement