Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package task.to.soft;
- import java.io.*;
- 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();
- }
- class Rectangle extends FigureGeneral {
- Rectangle(double width, double height, String name) {
- super(width, height, "rectangle");
- }
- public double area() {
- return getWidth() * getHeight();
- }
- public String toString(){
- return getName() + " " + getHeight()+ " " + getWidth();
- }
- }
- class Triangle extends FigureGeneral {
- Triangle(double width, double height, String name) {
- super(width, height, "triangle");
- }
- public double area() {
- return (getWidth() * getHeight()) / 2;
- }
- public String toString(){
- return getName() + " " + getHeight()+ " " + getWidth();
- }
- }
- @SuppressWarnings("serial")
- public class AbsFigure extends ArrayList<String> {
- // Read file //as one line
- public static String read(String fileName) {
- StringBuilder strBuider = new StringBuilder();
- try {
- BufferedReader in = new BufferedReader(new FileReader
- (new File(fileName).getAbsoluteFile()));
- try {
- String strInput;
- while((strInput = in.readLine()) != null) {
- strBuider.append(strInput);
- strBuider.append("\n");
- }
- }
- finally {
- in.close();
- }
- }catch(IOException e) {
- e.printStackTrace();
- }
- return strBuider.toString();
- }
- // write file for one call from method
- public static void write(String fileName, String text) {
- try {
- PrintWriter out = new PrintWriter
- (new File(fileName).getAbsoluteFile());
- try {
- out.print(text);
- }finally {
- out.close();
- }
- }catch(IOException e) {
- e.printStackTrace();
- }
- }
- // read file with split at regular expression
- @SuppressWarnings("unused")
- public AbsFigure(String fileName, String splitter){
- super(Arrays.asList(read(fileName).split(splitter)));
- if(get(0).equals(""))remove(0);
- double firstPart = Double.parseDouble(get(1));
- double secondPart = Double.parseDouble(get(2));
- String name = get(0);
- FigureGeneral element;
- if((get(0)).equals("triangle"))
- {
- element = new Triangle(firstPart, secondPart, name);
- }
- else {
- element = new Rectangle(firstPart, secondPart, name);
- }
- }
- // typical read for one line
- public AbsFigure(String fileName) {
- this(fileName, "\n");
- }
- public void write(String fileName) {
- try{
- PrintWriter out = new PrintWriter
- (new File(fileName).getAbsoluteFile());
- try {
- for(String item : this)out.println(item);
- }
- finally
- {
- out.close();
- }
- }catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) {
- String file = read("test.txt");
- write("data.txt", file);
- AbsFigure text = new AbsFigure("data.txt");
- text.write("data2.txt");
- ArrayList<String> shapes = new ArrayList<String>
- (new AbsFigure("test.txt", ", "));
- System.out.println(shapes);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement