DevF Batch 5 Example for Encapsulation Exercise
// 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");
}
}
}