Advertisement
nazar_art

AbsFigure now

Jan 16th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 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. public String toString(){
  27. return getName() + " " + getHeight()+ " " + getWidth();
  28. }
  29. }
  30.  
  31. class Rectangle extends FigureGeneral {
  32.  
  33. Rectangle(double width, double height, String name) {
  34. super(width, height, "rectangle");
  35. }
  36.  
  37. public double area() {
  38. return getWidth() * getHeight();
  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.  
  53. package task.to.soft;
  54.  
  55. import java.io.*;
  56. import java.util.*;
  57.  
  58. @SuppressWarnings("serial")
  59. class FigureGeneralFilesFoundException extends RuntimeException {
  60. public FigureGeneralFilesFoundException() {
  61. }
  62.  
  63. public FigureGeneralFilesFoundException(String message) {
  64. super(message);
  65. }
  66. }
  67.  
  68. @SuppressWarnings("serial")
  69. class FigureGeneralFormatException extends RuntimeException {
  70. public FigureGeneralFormatException() {
  71. }
  72.  
  73. public FigureGeneralFormatException(String message) {
  74. super(message);
  75. }
  76. }
  77.  
  78. public class AbsFigure {
  79.  
  80. static class DeepCompare implements Comparator<FigureGeneral> {
  81. AreaCompare areaCompare = new AreaCompare();
  82.  
  83. @Override
  84. public int compare(FigureGeneral oneFigure, FigureGeneral twoFigure) {
  85. if ((areaCompare.compare(oneFigure, twoFigure)) == 0) {
  86. if (oneFigure instanceof Triangle
  87. && twoFigure instanceof Triangle) {
  88. return 0;
  89. }
  90. if (oneFigure instanceof Rectangle
  91. && twoFigure instanceof Rectangle) {
  92. return 0;
  93. }
  94. }
  95. return -1;
  96. }
  97. }
  98.  
  99. static class AreaCompare implements Comparator<FigureGeneral> {
  100.  
  101. @Override
  102. public int compare(FigureGeneral oneFigure, FigureGeneral twoFigure) {
  103. double firstValue = oneFigure.area();
  104. double secondValue = twoFigure.area();
  105. int result = 0;
  106.  
  107. if (firstValue > secondValue)
  108. result = 1;
  109. else if (firstValue < secondValue)
  110. result = -1;
  111. else
  112. result = 0;
  113.  
  114. return result;
  115. }
  116. }
  117.  
  118. // write to file
  119. public void write(String fileName, List<FigureGeneral> figuresList) {
  120. try {
  121. PrintWriter out = new PrintWriter(
  122. new File(fileName).getAbsoluteFile());
  123. try {
  124. for (int i = 0; i < figuresList.size(); i++) {
  125. out.println(figuresList.get(i).toString());
  126. }
  127. } finally {
  128. out.close();
  129. }
  130. } catch (IOException e) {
  131. System.out.println("Cannot write to file!");
  132. }
  133. }
  134.  
  135. // read from file
  136. public ArrayList<FigureGeneral> figureReader(String fileName)
  137. throws FigureGeneralFilesFoundException,
  138. FigureGeneralFormatException {
  139. String line = null;
  140. ArrayList<FigureGeneral> myListFigure = new ArrayList<FigureGeneral>();
  141.  
  142. try {
  143. File myFile = new File(fileName);
  144. FileReader fileReader = new FileReader(myFile);
  145. BufferedReader bufferedReader = new BufferedReader(fileReader);
  146.  
  147. while ((line = bufferedReader.readLine()) != null) {
  148. myListFigure.add(fromStringToFigureGeneral(line));
  149. }
  150. System.out.println(myListFigure);
  151. bufferedReader.close();
  152.  
  153. if (!myFile.exists())
  154. throw new FigureGeneralFilesFoundException();
  155.  
  156. } catch (FileNotFoundException e) {
  157. System.out.println("File which You loking for not found!");
  158. } catch (IOException e) {
  159. System.out.println("Wrong input!");
  160. }
  161.  
  162. return myListFigure;
  163. }
  164.  
  165. // change from String to FigureGeneral(Triangle or Rectangle)
  166. private FigureGeneral fromStringToFigureGeneral(String line) {
  167. String[] arrayLines = line.split(" ");
  168. FigureGeneral figures = null;
  169.  
  170. if (arrayLines[0].equals("triangle")) {
  171. figures = new Triangle(Double.parseDouble(arrayLines[1]),
  172. Double.parseDouble(arrayLines[2]), "triangle");
  173. } else {
  174. figures = new Rectangle(Double.parseDouble(arrayLines[1]),
  175. Double.parseDouble(arrayLines[2]), "rectangle");
  176. }
  177.  
  178. return figures;
  179. }
  180.  
  181. // make random values
  182. protected FigureGeneral getRandomFigure() {
  183. Random randValues = new Random();
  184.  
  185. double randomWidth = Math.abs(randValues.nextInt() % 10) + 1;
  186. double randomHeight = Math.abs(randValues.nextInt() % 10) + 1;
  187. int randomName = Math.abs(randValues.nextInt() % 2);
  188. if (randomName == 0) {
  189. return new Triangle(randomWidth, randomHeight, "triangle");
  190. } else {
  191. return new Rectangle(randomWidth, randomHeight, "rectangle");
  192. }
  193. }
  194.  
  195. // make Collections
  196. @SuppressWarnings({ "unchecked", "rawtypes" })
  197. List<FigureGeneral> generateRandomFigures(int count) {
  198. ArrayList listOfFigures = new ArrayList();
  199.  
  200. for (int i = 0; i < count; i++) {
  201. listOfFigures.add(getRandomFigure());
  202. }
  203.  
  204. return listOfFigures;
  205. }
  206.  
  207. public void launch(List<FigureGeneral> figuresList) {
  208. DeepCompare deepCompare = new DeepCompare();
  209. Collections.sort(figuresList, deepCompare);
  210.  
  211. AbsFigure absFigure = new AbsFigure();
  212. try {
  213. absFigure.write("test.txt", figuresList);
  214. figureReader("test.txt");
  215. } catch (FigureGeneralFilesFoundException e) {
  216. e.printStackTrace();
  217. } catch (FigureGeneralFormatException e) {
  218. e.printStackTrace();
  219. }
  220. }
  221.  
  222. /**
  223. * @param args
  224. * @throws IOException
  225. *
  226. */
  227. public static void main(String[] args) {
  228.  
  229. AbsFigure absFigure = new AbsFigure();
  230. List<FigureGeneral> figuresList = absFigure.generateRandomFigures(5);
  231. absFigure.launch(figuresList);
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement