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();
- public String toString(){
- return getName() + " " + getHeight()+ " " + getWidth();
- }
- }
- 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;
- }
- }
- package task.to.soft;
- import java.io.*;
- import java.util.*;
- @SuppressWarnings("serial")
- class FigureGeneralFilesFoundException extends RuntimeException {
- public FigureGeneralFilesFoundException() {
- }
- public FigureGeneralFilesFoundException(String message) {
- super(message);
- }
- }
- @SuppressWarnings("serial")
- class FigureGeneralFormatException extends RuntimeException {
- public FigureGeneralFormatException() {
- }
- public FigureGeneralFormatException(String message) {
- super(message);
- }
- }
- public class AbsFigure {
- static class DeepCompare implements Comparator<FigureGeneral> {
- AreaCompare areaCompare = new AreaCompare();
- @Override
- public int compare(FigureGeneral oneFigure, FigureGeneral twoFigure) {
- if ((areaCompare.compare(oneFigure, twoFigure)) == 0) {
- if (oneFigure instanceof Triangle
- && twoFigure instanceof Triangle) {
- return 0;
- }
- if (oneFigure instanceof Rectangle
- && twoFigure instanceof Rectangle) {
- return 0;
- }
- }
- return -1;
- }
- }
- static class AreaCompare implements Comparator<FigureGeneral> {
- @Override
- public int compare(FigureGeneral oneFigure, FigureGeneral twoFigure) {
- double firstValue = oneFigure.area();
- double secondValue = twoFigure.area();
- int result = 0;
- if (firstValue > secondValue)
- result = 1;
- else if (firstValue < secondValue)
- result = -1;
- else
- result = 0;
- return result;
- }
- }
- // write to file
- public void write(String fileName, List<FigureGeneral> figuresList) {
- try {
- PrintWriter out = new PrintWriter(
- new File(fileName).getAbsoluteFile());
- try {
- for (int i = 0; i < figuresList.size(); i++) {
- out.println(figuresList.get(i).toString());
- }
- } finally {
- out.close();
- }
- } catch (IOException e) {
- System.out.println("Cannot write to file!");
- }
- }
- // read from file
- public ArrayList<FigureGeneral> figureReader(String fileName)
- throws FigureGeneralFilesFoundException,
- FigureGeneralFormatException {
- String line = null;
- ArrayList<FigureGeneral> myListFigure = new ArrayList<FigureGeneral>();
- try {
- File myFile = new File(fileName);
- FileReader fileReader = new FileReader(myFile);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- while ((line = bufferedReader.readLine()) != null) {
- myListFigure.add(fromStringToFigureGeneral(line));
- }
- System.out.println(myListFigure);
- bufferedReader.close();
- if (!myFile.exists())
- throw new FigureGeneralFilesFoundException();
- } catch (FileNotFoundException e) {
- System.out.println("File which You loking for not found!");
- } catch (IOException e) {
- System.out.println("Wrong input!");
- }
- return myListFigure;
- }
- // change from String to FigureGeneral(Triangle or Rectangle)
- private FigureGeneral fromStringToFigureGeneral(String line) {
- String[] arrayLines = line.split(" ");
- FigureGeneral figures = null;
- if (arrayLines[0].equals("triangle")) {
- figures = new Triangle(Double.parseDouble(arrayLines[1]),
- Double.parseDouble(arrayLines[2]), "triangle");
- } else {
- figures = new Rectangle(Double.parseDouble(arrayLines[1]),
- Double.parseDouble(arrayLines[2]), "rectangle");
- }
- return figures;
- }
- // make random values
- protected FigureGeneral getRandomFigure() {
- Random randValues = new Random();
- double randomWidth = Math.abs(randValues.nextInt() % 10) + 1;
- double randomHeight = Math.abs(randValues.nextInt() % 10) + 1;
- int randomName = Math.abs(randValues.nextInt() % 2);
- if (randomName == 0) {
- return new Triangle(randomWidth, randomHeight, "triangle");
- } else {
- return new Rectangle(randomWidth, randomHeight, "rectangle");
- }
- }
- // make Collections
- @SuppressWarnings({ "unchecked", "rawtypes" })
- List<FigureGeneral> generateRandomFigures(int count) {
- ArrayList listOfFigures = new ArrayList();
- for (int i = 0; i < count; i++) {
- listOfFigures.add(getRandomFigure());
- }
- return listOfFigures;
- }
- public void launch(List<FigureGeneral> figuresList) {
- DeepCompare deepCompare = new DeepCompare();
- Collections.sort(figuresList, deepCompare);
- AbsFigure absFigure = new AbsFigure();
- try {
- absFigure.write("test.txt", figuresList);
- figureReader("test.txt");
- } catch (FigureGeneralFilesFoundException e) {
- e.printStackTrace();
- } catch (FigureGeneralFormatException e) {
- e.printStackTrace();
- }
- }
- /**
- * @param args
- * @throws IOException
- *
- */
- public static void main(String[] args) {
- AbsFigure absFigure = new AbsFigure();
- List<FigureGeneral> figuresList = absFigure.generateRandomFigures(5);
- absFigure.launch(figuresList);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement