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 Triangle extends FigureGeneral {
- Triangle(double width, double height, String name) {
- super(width, height, "triangle");
- }
- public double area() {
- return (getWidth() * getHeight()) / 2;
- }
- }
- class Rectangle extends FigureGeneral {
- Rectangle(double width, double height, String name) {
- super(width, height, "rectangle");
- }
- public double area() {
- return getWidth() * getHeight();
- }
- }
- @SuppressWarnings("serial")
- class FigureGeneralFilesFoundException extends Exception {
- public FigureGeneralFilesFoundException() {
- }
- public FigureGeneralFilesFoundException(String message) {
- super(message);
- }
- }
- @SuppressWarnings("serial")
- class FigureGeneralNumberFormatException extends Exception {
- public FigureGeneralNumberFormatException() {
- }
- public FigureGeneralNumberFormatException(String message) {
- super(message);
- }
- }
- public class AbsFigure {
- 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;
- }
- }
- public void write(String fileName, List<FigureGeneral> figuresLisr) {
- try {
- PrintWriter out = new PrintWriter(
- new File(fileName).getAbsoluteFile());
- try {
- for (int i = 0; i < figuresLisr.size(); i++) {
- out.println(figuresLisr.get(i).toString());
- }
- } finally {
- out.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("Cannot write to file!");
- }
- }
- // read from file
- private FigureGeneral fromStringToObject(String fileName){
- String line = null;
- ArrayList<String> myList = new ArrayList<String>();
- FigureGeneral shape = null;
- while (true) {
- try{
- File myFile = new File(fileName);
- FileReader fileReader = new FileReader(myFile);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- while((line = bufferedReader.readLine()) != null){
- myList = new ArrayList<String>
- (Arrays.asList(line.split(" ")));
- }
- bufferedReader.close();
- if(myList.get(0).equals("triangle")){
- shape = new Triangle(Double.parseDouble(myList.get(1)),
- Double.parseDouble(myList.get(2)), "triangle");
- }else{
- shape = new Rectangle(Double.parseDouble(myList.get(1)),
- Double.parseDouble(myList.get(2)), "rectangle");
- }
- System.out.println(shape);
- break;
- }catch(FileNotFoundException e){
- System.out.println("File which You loking for not found!");
- break;
- }catch(NumberFormatException e){
- e.printStackTrace();
- break;
- }catch(IOException e){
- System.out.println("Wrong input!");
- break;
- }
- }
- return shape;
- }
- //}
- // 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 working(AbsFigure absFigure){
- List<FigureGeneral> figuresList = absFigure.generateRandomFigures(5);
- AreaCompare areaCompare = absFigure.new AreaCompare();
- Collections.sort(figuresList, areaCompare);
- absFigure.write("test.txt", figuresList);
- fromStringToObject("test.txt");
- }
- /**
- * @param args
- * @throws IOException
- *
- */
- public static void main(String[] args) {
- AbsFigure absFigure = new AbsFigure();
- absFigure.working(absFigure);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement