Last active
August 29, 2015 14:24
-
-
Save henocdz/b4f84191780665d8bbe3 to your computer and use it in GitHub Desktop.
DevF Batch 5 Example for Encapsulation Exercise
This file contains hidden or 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
| // File: devf/Sensei.java | |
| package devf; | |
| import java.util.HashMap; | |
| public class Sensei{ | |
| private String type, firstName, lastName; | |
| private static HashMap<String, Double> types; | |
| private static double taxPercentage = 0.16; | |
| private double hours; | |
| static{ | |
| types = new HashMap<String, Double>(); | |
| types.put("blanca", new Double(50)); | |
| types.put("roja", new Double(10000)); | |
| types.put("negra", new Double(0)); | |
| } | |
| public Sensei(String firstName, String lastName, double hours){ | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| this.hours = hours; | |
| } | |
| public void setHours(double hours){ | |
| this.hours = hours; | |
| } | |
| public String getFullName(){ | |
| return String.format("%s %s", this.firstName, this.lastName); | |
| } | |
| public double getSalary(){ | |
| Double pricePerHour = (Double) types.get(this.type); | |
| return pricePerHour * this.hours; | |
| } | |
| public boolean setType(String type){ | |
| if( !this.types.containsKey(type) ){ | |
| return false; | |
| } | |
| this.type = type; | |
| return true; | |
| } | |
| public double getTaxes(){ | |
| return this.getSalary() * this.taxPercentage; | |
| } | |
| public double getNetSalary(){ | |
| return this.getSalary() - this.getTaxes(); | |
| } | |
| } | |
| // File: Main.java | |
| import devf.Sensei; | |
| public class Main{ | |
| public static void main(String[] args) { | |
| Sensei sergio = new Sensei("Sergio", "Malvaez", 10); | |
| Sensei rafa = new Sensei("Rafa", "Miranda", 40); | |
| if(sergio.setType("striper")){ | |
| System.out.printf("Sensei: %s \n\tSalario bruto: %.2f \n\t -Impuestos: %.2f \n\t Salario neto: %.2f", | |
| sergio.getFullName(), | |
| sergio.getSalary(), | |
| sergio.getTaxes(), | |
| sergio.getNetSalary()); | |
| }else{ | |
| System.err.println("Invalid Sensei Type"); | |
| } | |
| if(rafa.setType("roja")){ | |
| System.out.printf("Sensei: %s \n\tSalario bruto: %.2f \n\t -Impuestos: %.2f \n\t Salario neto: %.2f", | |
| rafa.getFullName(), | |
| rafa.getSalary(), | |
| rafa.getTaxes(), | |
| rafa.getNetSalary()); | |
| }else{ | |
| System.err.println("Invalid Sensei Type"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment