Last active
October 2, 2019 04:37
-
-
Save andersonmo/1b7ed4fbe85155771bc950f7c1fd2773 to your computer and use it in GitHub Desktop.
Application to calculate the price of carpeting for retangular rooms.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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