我的程序读取一个形状列表,如果面积大于1000并且颜色串与绿色匹配,则打印形状。
示例数据如下:
矩形,宽度,高度,颜色-
圆,半径,颜色。
矩形68.01 77.63橙色
主类-初步尝试。
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");
}
}
}发布于 2019-09-10 03:13:09
您永远不会进入while循环,因为endof永远不会为false。
在解析(Shape_data)中,您需要根据部件决定要创建哪种类型的形状。
例如,输入“矩形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;
}请注意,这不会进行输入验证,以查看shape_data字符串是否包含足够的参数。
发布于 2019-09-10 06:05:43
带颜色的形状的界面:
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);
}
}Cercle实现:
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);
}
}形状工厂:
public class ColoredShapeFactory {
public ColoredShape createCircle(String[] shapeParts) {
//TODO implement validation of shapeParts array
double radius = Double.valueOf(shapeParts[1]);
String color = shapeParts[2];
return new Circle(color, radius);
}
public ColoredShape createRectangle(String[] shapeParts) {
//TODO implement validation of shapeParts array
double width = Double.valueOf(shapeParts[1]);
double height = Double.valueOf(shapeParts[2]);
String color = shapeParts[3];
return new Rectangle(color, width, height);
}
}最后是入口点类:
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");
}
}输入数据shape_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 greenhttps://stackoverflow.com/questions/57859759
复制相似问题