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();
- }
- }
- class Triangle extends FigureGeneral {
- Triangle(double width, double height, String name) {
- super(width, height, "triangle");
- }
- public double area() {
- return (getWidth() * getHeight()) / 2;
- }
- }
- class FigureReader {
- public ArrayList<FigureGeneral> readFromFile(String fileName) {
- ArrayList<FigureGeneral> list = new ArrayList<FigureGeneral>();
- try{
- File myFile = new File("test.txt");
- BufferedReader reader = new BufferedReader(new FileReader(myFile));
- String line = null;
- while((line = reader.readLine()) != null) {
- System.out.println(line);
- String[] lineElements = line.split(", ");
- FigureGeneral el;
- double x = Double.parseDouble(lineElements[1]);
- double y = Double.parseDouble(lineElements[2]);
- String name = lineElements[0];
- if((lineElements[0]).equals("triangle"))el =
- new Triangle(x, y, name);
- else el = new Rectangle(x, y, name);
- list.add(el);
- }
- reader.close();
- }catch(Exception exc){
- exc.printStackTrace();
- }
- return list;
- }
- public String fromFigureGeneralToString(FigureGeneral figure){
- return null;
- }
- }
- class CollectionToFile {
- public void saveToFile(List<FigureGeneral> figureList, String fileName){
- try {
- FileWriter writer = new FileWriter(new File(fileName));
- writer.write(figureList.toString() + "\n");
- writer.close();
- } catch (IOException ex) {
- System.out.println("Can't write to file!");
- ex.printStackTrace();
- }
- }
- }
- class CollectionFromFile {
- @SuppressWarnings({ "rawtypes", "unchecked", "resource" })
- public ArrayList<FigureGeneral> fromStringToFigureGeneral(String line){
- ArrayList<FigureGeneral> list = new ArrayList<FigureGeneral>();
- try {
- File myFile = new File(line);
- FileReader fileReader = new FileReader(myFile);
- BufferedReader reader = new BufferedReader(fileReader);
- String line1 = reader.readLine();
- line1 = line1.substring(1, line1.length() - 1);
- list = new ArrayList(Arrays.asList(line1.split(", ")));
- return list;
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- return list;
- }
- }
- public class AbsFigure {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) {
- FigureGeneral figures[] = new FigureGeneral[2];
- figures[0] = new Triangle(8.0, 12.0, "triangle right");
- figures[1] = new Rectangle(10, 4, "rectangle");
- for(int i = 0; i < figures.length; i++ ){
- System.out.print("Object is " + figures[i].getName());
- System.out.println(" Area is " + figures[i].area());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement