Advertisement
nazar_art

AbsFigure easier

Dec 28th, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 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.  
  27. class Rectangle extends FigureGeneral {
  28.    
  29.     Rectangle(double width, double height, String name) {
  30.         super(width, height, "rectangle");
  31.     }
  32.  
  33.     public double area() {
  34.         return getWidth() * getHeight();
  35.     }
  36.  
  37.     public String toString(){
  38.         return getName() + " " + getHeight()+ " " + getWidth();
  39.     }
  40. }
  41.  
  42. class Triangle extends FigureGeneral {
  43.        
  44.     Triangle(double width, double height, String name) {
  45.         super(width, height, "triangle");
  46.     }
  47.  
  48.     public double area() {
  49.         return (getWidth() * getHeight()) / 2;
  50.     }
  51.    
  52.     public String toString(){
  53.         return getName() + " " + getHeight()+ " " + getWidth();
  54.     }
  55. }
  56.  
  57.  
  58. @SuppressWarnings("serial")
  59. public class AbsFigure extends ArrayList<String> {
  60.     // Read file //as one line
  61.     public static String read(String fileName) {
  62.         StringBuilder strBuider = new StringBuilder();
  63.         try {
  64.             BufferedReader in = new BufferedReader(new FileReader
  65.                     (new File(fileName).getAbsoluteFile()));
  66.             try {
  67.                 String strInput;
  68.                 while((strInput = in.readLine()) != null) {
  69.                     strBuider.append(strInput);
  70.                     strBuider.append("\n");
  71.                 }
  72.             }
  73.             finally {
  74.                 in.close();
  75.             }
  76.         }catch(IOException e) {
  77.             e.printStackTrace();
  78.         }
  79.         return strBuider.toString();
  80.     }
  81.    
  82.     // write  file for one call from method
  83.     public static void write(String fileName, String text) {
  84.         try {
  85.             PrintWriter out = new PrintWriter
  86.                     (new File(fileName).getAbsoluteFile());
  87.             try {
  88.                 out.print(text);
  89.             }finally {
  90.                 out.close();
  91.             }
  92.         }catch(IOException e) {
  93.             e.printStackTrace();
  94.         }
  95.     }
  96.    
  97.     // read file with split at regular expression
  98.     @SuppressWarnings("unused")
  99.     public AbsFigure(String fileName, String splitter){
  100.         super(Arrays.asList(read(fileName).split(splitter)));
  101.         if(get(0).equals(""))remove(0);
  102.        
  103.        
  104.         double firstPart = Double.parseDouble(get(1));
  105.         double secondPart = Double.parseDouble(get(2));
  106.         String name = get(0);
  107.      
  108.         FigureGeneral element;
  109.         if((get(0)).equals("triangle"))
  110.         {
  111.             element = new Triangle(firstPart, secondPart, name);
  112.         }
  113.         else {
  114.              element = new Rectangle(firstPart, secondPart, name);
  115.              }
  116.     }
  117.    
  118.     // typical read for one line
  119.     public AbsFigure(String fileName) {
  120.         this(fileName, "\n");
  121.     }
  122.     public void write(String fileName) {
  123.         try{
  124.             PrintWriter out = new PrintWriter
  125.                     (new File(fileName).getAbsoluteFile());
  126.             try {
  127.                 for(String item : this)out.println(item);
  128.             }
  129.             finally
  130.             {
  131.                 out.close();
  132.             }
  133.         }catch(IOException e)
  134.         {
  135.             e.printStackTrace();
  136.         }
  137.     }
  138.  
  139.     /**
  140.      * @param args
  141.      * @throws IOException
  142.      */
  143.     public static void main(String[] args)  {
  144.        
  145.         String file = read("test.txt");
  146.         write("data.txt", file);
  147.        
  148.         AbsFigure text = new AbsFigure("data.txt");
  149.         text.write("data2.txt");
  150.        
  151.         ArrayList<String> shapes = new ArrayList<String>
  152.         (new AbsFigure("test.txt", ", "));
  153.         System.out.println(shapes);
  154.        
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement