codewars034: TDD Area Calculations

Source

package codewars;
import java.math.BigDecimal;
public class Calculator{
    public double getTotalArea(Shape shape){
        return new BigDecimal(Double.toString(shape.getArea())).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
    }    
    public double getTotalArea(Shape... shapes){
        double total = 0;
        for(Shape x : shapes){
            total += x.getArea();
        }    
        return new BigDecimal(Double.toString(total)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
    }    
}

interface Shape{
    public double getArea();
}
class Square implements Shape{
    private double side;
    public Square(double side){
          this.side = side;
    }    
    @Override
    public double getArea(){
        return side * side;
    }        
}
class Rectangle implements Shape{
    private double height;
    private double width;
    public Rectangle(double height,double width){
        this.height = height;
        this.width = width;
    }    
    @Override
    public double getArea(){
        return height * width;
    }    
}

class Circle implements Shape{
    private double radius;
    public Circle(double radius){
        this.radius = radius;
    }    
    @Override
    public double getArea(){
        return Mathi.PI * radius * radius;
    }    
}

class Triangle implements Shape{
    private double base;
    private double height;
    public Triangle(double base,double height){
        this.base = base;
        this.height = height;
    }        
    @Override
    public double getArea(){
        return base * height / 2;
    }        
}

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...