Skip to content

Instantly share code, notes, and snippets.

@andersonmo
Last active October 2, 2019 04:37
Show Gist options
  • Save andersonmo/1b7ed4fbe85155771bc950f7c1fd2773 to your computer and use it in GitHub Desktop.
Save andersonmo/1b7ed4fbe85155771bc950f7c1fd2773 to your computer and use it in GitHub Desktop.
Application to calculate the price of carpeting for retangular rooms.
public class Calculator {
private Floor floor;
private Carpet carpet;
public Calculator(Floor floor, Carpet carpet){
this.floor = floor;
this.carpet = carpet;
}
public double getTotalCost(){
double totalCost;
totalCost = floor.getArea() * carpet.getCost();
return totalCost;
}
}
public class Carpet {
private double cost;
public Carpet (double cost){
if (cost<0){
this.cost = 0;
} else {
this.cost = cost;
}
}
public double getCost(){
return this.cost;
}
}
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.length * this.width;
}
}
public class Main {
public static void main(String[] args) {
// testing
Carpet carpet = new Carpet(3.5);
Floor floor = new Floor(2.75, 4.0);
Calculator calculator = new Calculator(floor, carpet);
System.out.println("total = " +calculator.getTotalCost()); // 38,5
carpet = new Carpet(1.5);
floor = new Floor(5.4, 4.5);
calculator = new Calculator(floor, carpet);
System.out.println("total = " +calculator.getTotalCost()); // 36,45
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment