Java-计算面积,如果面积为>;1000和形状=绿色,打印形状列表

Java-计算面积,如果面积为>;1000和形状=绿色,打印形状列表,java,parsing,math,Java,Parsing,Math,我的程序读取一个形状列表,如果面积>1000且颜色字符串与绿色匹配,则打印形状 样本数据如下: 矩形、宽度、高度、颜色- 圆、半径、颜色 矩形68.01 77.63橙色 public static Shape parse(String shape_data) { Shape shape; // TODO: complete this method String[] parts = shape_data.split(" "); switch(parts[0])

我的程序读取一个形状列表,如果面积>1000且颜色字符串与绿色匹配,则打印形状

样本数据如下:

矩形、宽度、高度、颜色-

圆、半径、颜色

矩形68.01 77.63橙色

public static Shape parse(String shape_data) {
    Shape shape;

    // TODO: complete this method
    String[] parts = shape_data.split(" ");
    switch(parts[0])
    {
        case "rectangle":
        shape = new Rectangle(parts[1], parts[2], parts[3]);
        break;
       etc...
    }
    return shape;
}
主要课程-初步尝试


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.File;


public class Main {
    private static String SHAPE_DATA = "shapes.txt";

    public static boolean main(String[] args) throws Exception{

        List<Shape> shapes = ShapeParser.parseFile(SHAPE_DATA);
        for(int = 0 ; i < shapes.isValid() i++);
                System.out.print(shapes);
                //System.out.println("%s") shapes;
                private static boolean isValid (shapes) ; {
                    return shapes.getArea() > 1000 && shapes.getColour().equals("green");


                }
    }


}




导入java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
导入java.io.File;
公共班机{
私有静态字符串SHAPE_DATA=“shapes.txt”;
公共静态布尔主(字符串[]args)引发异常{
List shapes=ShapeParser.parseFile(SHAPE_数据);
对于(int=0;i1000&&shapes.getColor().equals(“绿色”);
}
}
}

您永远不会进入while循环,因为endof从不为false

在解析(shape_数据)时,需要根据零件[0]决定要创建哪种类型的形状

例如,输入“矩形68.01 77.63橙色”


请注意,这不会进行输入验证,以查看
shape\u数据
字符串是否实际包含足够的参数。

带有颜色的形状界面:

public interface ColoredShape {
    String getColor();
    double getArea();
}
基色形状类:

public abstract class BaseColoredShape implements ColoredShape {
    protected final String color;

    public BaseColoredShape(String color) {
        this.color = color;
    }
    @Override
    public String getColor() {
        return this.color;
    }
    @Override
    public String toString() {
        return this.color;
    }
}
矩形实现:

public class Rectangle extends BaseColoredShape {
    private final double width;
    private final double height;

    public Rectangle(String color, double width, double height) {
        super(color);
        this.width = width;
        this.height = height;
    }

    @Override
    public double getArea() {
        return width * height;
    }

    @Override
    public String toString() {
        return String.format("rectangle %s %s %s", this.width, this.height, this.color);
    }
}
public class Circle extends BaseColoredShape {
    private final double radius;

    public Circle(String colour, double radius) {
        super(colour);
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }

    @Override
    public String toString() {
        return String.format("circle %s %s", this.radius, this.color);
    }
}
Cercle实施:

public class Rectangle extends BaseColoredShape {
    private final double width;
    private final double height;

    public Rectangle(String color, double width, double height) {
        super(color);
        this.width = width;
        this.height = height;
    }

    @Override
    public double getArea() {
        return width * height;
    }

    @Override
    public String toString() {
        return String.format("rectangle %s %s %s", this.width, this.height, this.color);
    }
}
public class Circle extends BaseColoredShape {
    private final double radius;

    public Circle(String colour, double radius) {
        super(colour);
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }

    @Override
    public String toString() {
        return String.format("circle %s %s", this.radius, this.color);
    }
}
输入数据解析器(txt文件)实现:

public class ShapeDataParser {
    private final String filepath;
    private final ColoredShapeFactory factory;

    public ShapeDataParser(String filepath) {
        this.filepath = filepath;
        this.factory = new ColoredShapeFactory();
    }

    public Stream<ColoredShape> parse() throws IOException, URISyntaxException {
        Path path = Paths.get(getClass().getClassLoader().getResource(filepath).toURI());
        return Files.lines(path).map(this::stringToColoredShape);
    }

    private ColoredShape stringToColoredShape(String shapeStr) {
        String[] shapeParts = shapeStr.split(" ");
        String shapeType = shapeParts[0];
        if("circle".equals(shapeType)) {
            return factory.createCircle(shapeParts);
        }
        return factory.createRectangle(shapeParts);
    }
}
最后一个入口点类:

import java.io.IOException;
import java.net.URISyntaxException;

public class ShapeFilter {
    public static void main(String[] args) throws IOException, URISyntaxException {
        ShapeDataParser parser = new ShapeDataParser("shape_data.txt");
        parser.parse()
                .filter(ShapeFilter::isValid)
                .forEach(System.out::println);
    }

    private static boolean isValid(ColoredShape shape) {
        return shape.getArea() > 1000 && shape.getColor().equals("green");
    }
}
输入数据形状_data.txt:

rectangle 68.01 77.63 orange
circle 88.06 green
circle 18.29 green
circle 71.71 red
rectangle 17.91 8.75 orange
circle 2.16 white
rectangle 83.12 98.71 green
rectangle 37.27 35.93 green
rectangle 45.13 74.55 green
circle 36.62 white
circle 72.59 yellow    
输出:

circle 88.06 green
circle 18.29 green
rectangle 83.12 98.71 green
rectangle 37.27 35.93 green
rectangle 45.13 74.55 green

不确定为什么需要模式匹配。你是一时兴起才添加标签的吗?你能添加形状的样本数据吗?TXT你似乎对
parse(shape\u data)
函数感到困惑。此函数应一次获取一行文本并返回一个形状。您似乎让它生成一个新的矩形,然后生成许多圆形,然后返回创建的第一个矩形对象。