CONSTRUCTOR CHALLENGE
MY APPROACH(BUGS)
public class Floor {
private double length;
private double width;
public double getLength(){
if(length<0)return 0;
else return length;
}
public double getWidth(){
if(width<0)return 0;
else return width;
}
public void setLength(double length){
this.length = length;
}
public void setWidth(double width){
this.width = width;
}
public double getArea(){
return (length * width);
}
public class Carpet {
private double cost;
public double getCost(){
if(cost<0)return 0;
else return cost;
}
public void setCost(double cost){
this.cost = cost;
}
}
public class Calculator {
private Floor floor;
private Carpet carpet;
public void setTotalCost(){
this.floor=floor;
this.carpet=carpet;
}
public double getTotalCost() {
return (floor.getArea() * carpet.getCost());
}
}
}
BEST APPROACH
public class Floor {
private double width;
private double length;
public Floor(double width,double length){
if(width<0) this.width = 0;
else this.width = width;
if(length<0) this.length = 0;
else this.length = length;
}
public double getArea(){
return (this.width*this.length);
}
public class Carpet {
private double cost;
public Carpet(double cost){
this.cost=(cost<0) ? 0 : cost;
}
public double getCost(){
return cost;
}
}
public class Calculator {
private Floor floor;
private Carpet carpet;
public Calculator(Floor floor,Carpet carpet){
this.floor = floor;
this.carpet = carpet;
}
public double getTotalCost(){
return (floor.getArea() * carpet.getCost());
}
}
}