Exemple #1
0
  public static void main(String[] args) {
    File file = new File("./shapesInput.txt");
    int ch;
    StringBuffer strContent = new StringBuffer("");
    FileInputStream fin = null;
    try {
      fin = new FileInputStream(file);
      while ((ch = fin.read()) != -1) {
        strContent.append((char) ch);
      }
      fin.close();
    } catch (FileNotFoundException e) {
      System.out.println("File" + file.getAbsolutePath() + " could not be found on filesystem");
    } catch (IOException ioe) {
      System.out.println("Exception while reading the file" + ioe);
    }
    String fileOutput = strContent.toString();
    String[] commands = fileOutput.split(";");
    List<Shape> shapes = new ArrayList<Shape>();
    List<Double> areas = new ArrayList<Double>();
    List<Double> areasD = new ArrayList<Double>();
    for (int i = 0; i <= commands.length - 1; i++) {
      String codeLine = commands[i];
      String shapeType = codeLine.substring(17, 20);
      switch (shapeType) {
        case "Cir":
          String rad = codeLine.substring(23, codeLine.length());
          rad = rad.replace("(", "");
          rad = rad.replace(")", "");
          int radius;
          radius = Integer.parseInt(rad);
          shapes.add(new Circle(radius));
          break;
        case "Rec":
          String len = codeLine.substring(26, codeLine.length());
          len = len.replace("(", "");
          len = len.replace(")", "");
          String[] params = len.split(",");
          String h = params[0];
          String l = params[1];
          int height = Integer.parseInt(h);
          int length = Integer.parseInt(l);
          shapes.add(new Rectangle(height, length));
          break;
        case "Rho":
          String stuff = codeLine.substring(24, codeLine.length());
          stuff = stuff.replace("(", "");
          stuff = stuff.replace(")", "");
          String[] parame = stuff.split(",");
          String first = parame[0];
          String second = parame[1];
          int fir = Integer.parseInt(first);
          int sec = Integer.parseInt(second);
          shapes.add(new Rhombus(fir, sec));
          break;
        case "Tra":
          String total = codeLine.substring(26, codeLine.length());
          total = total.replace("(", "");
          total = total.replace(")", "");
          String[] seperated = total.split(",");
          String onep = seperated[0];
          String twop = seperated[1];
          String threep = seperated[2];
          int op = Integer.parseInt(onep);
          int tp = Integer.parseInt(twop);
          int thp = Integer.parseInt(threep);
          shapes.add(new Trapezoid(op, tp, thp));
          break;
        case "Tri":
          String measures = codeLine.substring(25, codeLine.length());
          measures = measures.replace("(", "");
          measures = measures.replace(")", "");
          String[] listp = measures.split(",");
          String aaa = listp[0];
          String bbb = listp[1];
          int aa = Integer.parseInt(aaa);
          int bb = Integer.parseInt(bbb);
          shapes.add(new Triangle(aa, bb));
          break;
      }
    }
    for (int q = 0; q <= shapes.size(); q++) {
      for (int i = 0; i <= shapes.size() - 2; i++) {
        if (shapes.get(i).getArea() > shapes.get(i + 1).getArea()) {
          Shape temp;
          temp = shapes.get(i);
          shapes.set(i, shapes.get(i + 1));
          shapes.set(i + 1, temp);
        } else {

        }
      }
    }
    for (Shape s : shapes) {
      System.out.println("\nCalculating " + s.getShapeName() + " area:");
      System.out.println("Area = " + s.getArea());
      System.out.println("Printing " + s.getShapeName() + " description:");
      s.printDescription();
    }
  }